KPipewire

libvpxvp9encoder.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: 2023 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 "libvpxvp9encoder_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
26LibVpxVp9Encoder::LibVpxVp9Encoder(PipeWireProduce *produce)
27 : SoftwareEncoder(produce)
28{
29}
30
31bool LibVpxVp9Encoder::initialize(const QSize &size)
32{
33 createFilterGraph(size);
34
35 auto codec = avcodec_find_encoder_by_name("libvpx-vp9");
36 if (!codec) {
37 qCWarning(PIPEWIRERECORD_LOGGING) << "libvpx-vp9 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_YUV420P;
51 m_avCodecContext->time_base = AVRational{1, 1000};
52
53 AVDictionary *options = nullptr;
54
55 applyEncodingPreference(options);
56
57 const auto area = size.width() * size.height();
58 // m_avCodecContext->framerate is not set, so we use m_produce->maxFramerate() instead.
59 const auto maxFramerate = m_produce->maxFramerate();
60 const auto fps = qreal(maxFramerate.numerator) / std::max(quint32(1), maxFramerate.denominator);
61
62 m_avCodecContext->gop_size = fps * 2;
63
64 // TODO: Make bitrate depend on the framerate? More frames is more data.
65 // maxFramerate can apparently be changed while recording, so keep that in mind.
66 m_avCodecContext->bit_rate = std::round(area * 2);
67 m_avCodecContext->rc_min_rate = std::round(area);
68 m_avCodecContext->rc_max_rate = std::round(area * 3);
69
70 m_avCodecContext->rc_buffer_size = m_avCodecContext->bit_rate;
71
72 m_avCodecContext->thread_count = QThread::idealThreadCount();
73
74 if (int result = avcodec_open2(m_avCodecContext, codec, &options); result < 0) {
75 qCWarning(PIPEWIRERECORD_LOGGING) << "Could not open codec" << av_err2str(result);
76 return false;
77 }
78
79 return true;
80}
81
82int LibVpxVp9Encoder::percentageToAbsoluteQuality(const std::optional<quint8> &quality)
83{
84 if (!quality) {
85 return -1;
86 }
87
88 constexpr int MinQuality = 63;
89 return std::max(1, int(MinQuality - (m_quality.value() / 100.0) * MinQuality));
90}
91
92void LibVpxVp9Encoder::applyEncodingPreference(AVDictionary *options)
93{
94 // We're probably capturing a screen
95 av_dict_set(&options, "tune-content", "screen", 0);
96
97 // Lower crf is higher quality. Max 0, min 63. libvpx-vp9 doesn't use global_quality.
98 int crf = 31;
99 if (m_quality) {
100 crf = percentageToAbsoluteQuality(m_quality);
101 }
102 m_avCodecContext->qmin = std::clamp(crf / 2, 0, crf);
103 m_avCodecContext->qmax = std::clamp(qRound(crf * 1.5), crf, 63);
104 av_dict_set_int(&options, "crf", crf, 0);
105
106 // 0-4 are for Video-On-Demand with the good or best deadline.
107 // Don't use best, it's not worth it.
108 // 5-8 are for streaming with the realtime deadline.
109 // Lower is higher quality.
110 int cpuUsed = 5 + std::max(1, int(3 - std::round(m_quality.value_or(50) / 100.0 * 3)));
111 av_dict_set_int(&options, "cpu-used", cpuUsed, 0);
112 av_dict_set(&options, "deadline", "realtime", 0);
113
114 // The value is interpreted as being equivalent to log2(realNumberOfColumns),
115 // so 3 is 8 columns. 6 is the max amount of columns. 2 is the max amount of rows.
116 av_dict_set(&options, "tile-columns", "6", 0);
117 av_dict_set(&options, "tile-rows", "2", 0);
118
119 // This should make things faster, but it only seems to consume 100MB more RAM.
120 // av_dict_set(&options, "row-mt", "1", 0);
121 av_dict_set(&options, "frame-parallel", "1", 0);
122}
int height() const const
bool isEmpty() const const
int width() const const
int idealThreadCount()
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.