KPipewire

libwebpencoder.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
3 SPDX-FileCopyrightText: 2023 Marco Martin <mart@kde.org>
4 SPDX-FileCopyrightText: 2023 Arjen Hiemstra <ahiemstra@heimr.nl>
5 SPDX-FileCopyrightText: 2024 Noah Davis <noahadvs@gmail.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8*/
9
10#include "libwebpencoder_p.h"
11
12#include "pipewireproduce_p.h"
13
14#include <QSize>
15#include <QThread>
16
17extern "C" {
18#include <libavcodec/avcodec.h>
19#include <libavfilter/buffersink.h>
20#include <libavfilter/buffersrc.h>
21#include <libavutil/pixfmt.h>
22}
23
24#include "logging_record.h"
25
26LibWebPEncoder::LibWebPEncoder(PipeWireProduce *produce)
27 : SoftwareEncoder(produce)
28{
29}
30
31bool LibWebPEncoder::initialize(const QSize &size)
32{
33 createFilterGraph(size);
34
35 auto codec = avcodec_find_encoder_by_name("libwebp");
36 if (!codec) {
37 qCWarning(PIPEWIRERECORD_LOGGING) << "libwebp codec not found";
38 return false;
39 }
40
41 m_avCodecContext = avcodec_alloc_context3(codec);
42 if (!m_avCodecContext) {
43 qCWarning(PIPEWIRERECORD_LOGGING) << "Could not allocate video codec context";
44 return false;
45 }
46
47 Q_ASSERT(!size.isEmpty());
48 m_avCodecContext->width = size.width();
49 m_avCodecContext->height = size.height();
50 m_avCodecContext->pix_fmt = AV_PIX_FMT_YUVA420P;
51 m_avCodecContext->time_base = AVRational{1, 1000};
52
53 AVDictionary *options = nullptr;
54
55 applyEncodingPreference(options);
56
57 if (int result = avcodec_open2(m_avCodecContext, codec, &options); result < 0) {
58 qCWarning(PIPEWIRERECORD_LOGGING) << "Could not open codec" << av_err2str(result);
59 return false;
60 }
61
62 return true;
63}
64
65int LibWebPEncoder::percentageToAbsoluteQuality(const std::optional<quint8> &quality)
66{
67 return quality.value_or(-1); // Already 0-100. -1 resets to default.
68}
69
70void LibWebPEncoder::applyEncodingPreference([[maybe_unused]] AVDictionary *options)
71{
72}
int height() const const
bool isEmpty() const const
int width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.