[PATCH] Tweak encoding options for VP9
authorArjen Hiemstra <ahiemstra@heimr.nl>
Thu, 20 Mar 2025 13:31:24 +0000 (14:31 +0100)
committerAurélien COUDERC <coucouf@debian.org>
Sun, 18 May 2025 22:58:37 +0000 (00:58 +0200)
Don't set both bitrate and CRF, as they are conflicting settings.
Instead only set CRF. Also enable row-mt and avoid modifying the codec
context in a function that is about options.

Gbp-Pq: Name upstream_123ae0d2_Tweak-encoding-options-for-VP9.patch

src/libvpxvp9encoder.cpp

index d8770237459effc9d87a9c43ea644946f34eaa57..a9220f404d9bd03e7389ecc15531622076346e7d 100644 (file)
@@ -60,16 +60,6 @@ bool LibVpxVp9Encoder::initialize(const QSize &size)
 
     m_avCodecContext->gop_size = fps * 2;
 
-    // TODO: Make bitrate depend on the framerate? More frames is more data.
-    // maxFramerate can apparently be changed while recording, so keep that in mind.
-    m_avCodecContext->bit_rate = std::round(area * 2);
-    m_avCodecContext->rc_min_rate = std::round(area);
-    m_avCodecContext->rc_max_rate = std::round(area * 3);
-
-    m_avCodecContext->rc_buffer_size = m_avCodecContext->bit_rate;
-
-    m_avCodecContext->thread_count = QThread::idealThreadCount();
-
     if (int result = avcodec_open2(m_avCodecContext, codec, &options); result < 0) {
         qCWarning(PIPEWIRERECORD_LOGGING) << "Could not open codec" << av_err2str(result);
         return false;
@@ -100,8 +90,9 @@ AVDictionary *LibVpxVp9Encoder::buildEncodingOptions()
     if (m_quality) {
         crf = percentageToAbsoluteQuality(m_quality);
     }
-    m_avCodecContext->qmin = std::clamp(crf / 2, 0, crf);
-    m_avCodecContext->qmax = std::clamp(qRound(crf * 1.5), crf, 63);
+
+    av_dict_set_int(&options, "qmin", std::clamp(crf / 2, 0, crf), 0);
+    av_dict_set_int(&options, "qmax", std::clamp(int(crf * 1.5), crf, 63), 0);
     av_dict_set_int(&options, "crf", crf, 0);
 
     // 0-4 are for Video-On-Demand with the good or best deadline.
@@ -117,8 +108,7 @@ AVDictionary *LibVpxVp9Encoder::buildEncodingOptions()
     av_dict_set(&options, "tile-columns", "6", 0);
     av_dict_set(&options, "tile-rows", "2", 0);
 
-    // This should make things faster, but it only seems to consume 100MB more RAM.
-    // av_dict_set(&options, "row-mt", "1", 0);
+    av_dict_set(&options, "row-mt", "1", 0);
     av_dict_set(&options, "frame-parallel", "1", 0);
 
     return options;