From 5de00b9ffb3959966cbb08f456cc68c071397cd7 Mon Sep 17 00:00:00 2001 From: Julien Isorce Date: Fri, 11 Oct 2019 09:51:24 -0700 Subject: [PATCH] msdkenc: close encoder upon bitrate change Upon bitrate change, make sure to close the encoder otherwise the encoder is not re-initialized and the target bitrate is never reached, and the encoder was flushed at each frame from this moment. Regression introduced in f2b35abcab which replaced the call that was closing the encoder by an early return to avoid re-initialization. --- sys/msdk/gstmsdkenc.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/sys/msdk/gstmsdkenc.c b/sys/msdk/gstmsdkenc.c index 843c5b564d..63489dbd8f 100644 --- a/sys/msdk/gstmsdkenc.c +++ b/sys/msdk/gstmsdkenc.c @@ -145,6 +145,8 @@ ensure_bitrate_control (GstMsdkEnc * thiz) mfxExtCodingOption2 *option2 = &thiz->option2; mfxExtCodingOption3 *option3 = &thiz->option3; + GST_DEBUG_OBJECT (thiz, "set target bitrate: %u kbit/sec", thiz->bitrate); + mfx->RateControlMethod = thiz->rate_control; /* No effect in CQP varient algorithms */ if ((mfx->RateControlMethod != MFX_RATECONTROL_CQP) && @@ -246,8 +248,10 @@ gst_msdkenc_init_encoder (GstMsdkEnc * thiz) guint i; gboolean need_vpp = TRUE; - if (thiz->initialized) + if (thiz->initialized) { + GST_DEBUG_OBJECT (thiz, "Already initialized"); return TRUE; + } if (!thiz->context) { GST_WARNING_OBJECT (thiz, "No MSDK Context"); @@ -875,6 +879,8 @@ gst_msdkenc_flush_frames (GstMsdkEnc * thiz, gboolean discard) if (!thiz->tasks) return; + GST_DEBUG_OBJECT (thiz, "flush frames"); + session = gst_msdk_context_get_session (thiz->context); for (;;) { @@ -1347,8 +1353,17 @@ gst_msdkenc_handle_frame (GstVideoEncoder * encoder, GstVideoCodecFrame * frame) MsdkSurface *surface; if (thiz->reconfig) { + mfxInfoMFX *mfx = NULL; + + GST_INFO_OBJECT (encoder, "Adjust encoder bitrate before current frame"); gst_msdkenc_flush_frames (thiz, FALSE); + gst_msdkenc_close_encoder (thiz); + + // This will reinitialized the encoder but keep same input format. gst_msdkenc_set_format (encoder, NULL); + + mfx = &thiz->param.mfx; + GST_INFO_OBJECT (encoder, "New target bitrate: %u", mfx->TargetKbps); } if (G_UNLIKELY (thiz->context == NULL)) @@ -1525,6 +1540,8 @@ gst_msdkenc_flush (GstVideoEncoder * encoder) { GstMsdkEnc *thiz = GST_MSDKENC (encoder); + GST_DEBUG_OBJECT (encoder, "flush and close encoder"); + gst_msdkenc_flush_frames (thiz, TRUE); gst_msdkenc_close_encoder (thiz); gst_msdkenc_dequeue_all_frames (thiz);