diff --git a/src/lib/kdynamicwallpaperwriter.cpp b/src/lib/kdynamicwallpaperwriter.cpp index 53129de..11827e2 100644 --- a/src/lib/kdynamicwallpaperwriter.cpp +++ b/src/lib/kdynamicwallpaperwriter.cpp @@ -38,6 +38,7 @@ class KDynamicWallpaperWriterPrivate QList metaData; std::optional speed; std::optional maxThreadCount; + avifCodecChoice codecChoice = AVIF_CODEC_CHOICE_AUTO; }; KDynamicWallpaperWriterPrivate::KDynamicWallpaperWriterPrivate() @@ -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]() { @@ -206,6 +208,38 @@ QList 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. diff --git a/src/lib/kdynamicwallpaperwriter.h b/src/lib/kdynamicwallpaperwriter.h index d875055..ed48ffc 100644 --- a/src/lib/kdynamicwallpaperwriter.h +++ b/src/lib/kdynamicwallpaperwriter.h @@ -60,6 +60,9 @@ class KDYNAMICWALLPAPER_EXPORT KDynamicWallpaperWriter void setImages(const QList &views); QList images() const; + bool setCodecName(const QString &codecName); + QString codecName() const; + bool flush(QIODevice *device); bool flush(const QString &fileName); diff --git a/src/tools/builder/main.cpp b/src/tools/builder/main.cpp index bd74d4d..d0b3491 100644 --- a/src/tools/builder/main.cpp +++ b/src/tools/builder/main.cpp @@ -34,6 +34,10 @@ 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(); @@ -41,6 +45,7 @@ int main(int argc, char **argv) parser.addOption(outputOption); parser.addOption(maxThreadsOption); parser.addOption(speedOption); + parser.addOption(codecOption); parser.process(app); if (parser.positionalArguments().count() != 1) @@ -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");