From f2c75d548d1ab02e7015adb55cd9db0e6e9282af Mon Sep 17 00:00:00 2001 From: Josep Torra Date: Mon, 7 Oct 2019 08:39:06 -0700 Subject: [PATCH] msdkenc: handle bitrate changes in playing state When the bitrate is changed in playing state the encoder issues a reconfig that drains and recreates the underlaying hw encoder instance. With this set of changes we ensure that all this work is only made when the bitrate did actually change. It also tries to reuse the vpp buffer pool and fixes the pool leak spotted when testing this feature. --- sys/msdk/gstmsdkenc.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/sys/msdk/gstmsdkenc.c b/sys/msdk/gstmsdkenc.c index eb7271aee7..843c5b564d 100644 --- a/sys/msdk/gstmsdkenc.c +++ b/sys/msdk/gstmsdkenc.c @@ -1136,6 +1136,28 @@ gst_msdkenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state) info->height); caps = gst_video_info_to_caps (&nv12_info); + /* If there's an existing pool try to reuse it when is compatible */ + if (thiz->msdk_converted_pool) { + GstStructure *config; + GstCaps *pool_caps; + gboolean is_pool_compatible = FALSE; + + config = gst_buffer_pool_get_config (thiz->msdk_converted_pool); + gst_buffer_pool_config_get_params (config, &pool_caps, NULL, NULL, NULL); + if (caps && pool_caps) + is_pool_compatible = gst_caps_is_equal (caps, pool_caps); + gst_structure_free (config); + + /* If caps are the same then we are done */ + if (is_pool_compatible) { + gst_caps_unref (caps); + goto done; + } + /* Release current pool because we are going to create a new one */ + gst_object_replace ((GstObject **) & thiz->msdk_converted_pool, NULL); + } + + /* Otherwise create a new pool */ pool = gst_msdkenc_create_buffer_pool (thiz, caps, thiz->num_surfaces, FALSE); @@ -1143,6 +1165,7 @@ gst_msdkenc_set_format (GstVideoEncoder * encoder, GstVideoCodecState * state) gst_caps_unref (caps); } +done: return TRUE; } @@ -1683,9 +1706,16 @@ gst_msdkenc_set_common_property (GObject * object, guint prop_id, thiz->rate_control = g_value_get_enum (value); break; case GST_MSDKENC_PROP_BITRATE: - thiz->bitrate = g_value_get_uint (value); - thiz->reconfig = TRUE; + { + guint bitrate = g_value_get_uint (value); + /* Ensure that bitrate changed before triggering a reconfig */ + if (bitrate != thiz->bitrate) { + thiz->bitrate = bitrate; + thiz->reconfig = TRUE; + GST_DEBUG_OBJECT (thiz, "changed bitrate to %u", bitrate); + } break; + } case GST_MSDKENC_PROP_MAX_FRAME_SIZE: thiz->max_frame_size = g_value_get_uint (value); break;