videorate: don't hold the reference to the buffer in drop-only mode

Pushing the buffer via gst_pad_push () in transform_ip () function
causes downstream elements to process the buffer with a reference
count > 1. This leads to performance issue if there are downstream
elements which modify the buffer memory.

However, in drop-only mode this reference is not required.

So, let GstBaseTransform push the buffer in drop-only mode.

Fixes #4258

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/9532>
This commit is contained in:
Andrey Khamukhin 2025-08-11 10:08:40 +03:00 committed by GStreamer Marge Bot
parent 9f8885c19c
commit e94830c79d
2 changed files with 63 additions and 11 deletions

View File

@ -784,6 +784,12 @@ gst_video_rate_push_buffer (GstVideoRate * videorate, GstBuffer * outbuf,
/* We do not need to update time in VFR (variable frame rate) mode */
if (!videorate->drop_only) {
GST_BUFFER_PTS (outbuf) = push_ts;
} else {
/* In drop-only mode, we want GstBaseTransform to push the buffer.
* This avoids storing extra reference to the buffer. */
gst_buffer_unref (outbuf);
return res;
}
if (videorate->drop_out_of_segment
@ -1895,17 +1901,8 @@ gst_video_rate_transform_ip (GstBaseTransform * trans, GstBuffer * buffer)
if (videorate->drop_only) {
if ((videorate->segment.rate > 0.0 && in_ts >= videorate->next_ts) ||
(videorate->segment.rate < 0.0 && in_ts <= videorate->next_ts)) {
GstFlowReturn r;
/* The buffer received from basetransform is guaranteed to be writable.
* It just needs to be reffed so the buffer won't be consumed once pushed and
* GstBaseTransform can get its reference back. */
if ((r = gst_video_rate_push_buffer (videorate,
gst_buffer_ref (buffer), FALSE,
GST_CLOCK_TIME_NONE, FALSE)) != GST_FLOW_OK) {
res = r;
goto done;
}
res = gst_video_rate_push_buffer (videorate,
gst_buffer_ref (buffer), FALSE, GST_CLOCK_TIME_NONE, FALSE);
} else {
videorate->drop++;
}

View File

@ -2171,6 +2171,60 @@ GST_START_TEST (test_segment_update)
GST_END_TEST;
static GstFlowReturn
gst_check_drop_only_ref_chain_func (GstPad * pad, GstObject * parent,
GstBuffer * buffer)
{
GST_DEBUG_OBJECT (pad, "chain_func: received buffer %p", buffer);
ASSERT_BUFFER_REFCOUNT (buffer, "buf", 1);
buffers = g_list_append (buffers, buffer);
g_mutex_lock (&check_mutex);
g_cond_signal (&check_cond);
g_mutex_unlock (&check_mutex);
return GST_FLOW_OK;
}
GST_START_TEST (test_drop_only_ref_count)
{
GstElement *videorate;
GstBuffer *buf;
GstCaps *caps;
GstSegment segment;
/* Create a videorate that outputs at most 1 frame per second. */
videorate = gst_check_setup_element ("videorate");
mysrcpad = gst_check_setup_src_pad (videorate, &srctemplate);
mysinkpad = gst_check_setup_sink_pad (videorate, &sinktemplate);
gst_pad_set_chain_function (mysinkpad, gst_check_drop_only_ref_chain_func);
gst_pad_set_active (mysrcpad, TRUE);
gst_pad_set_active (mysinkpad, TRUE);
g_object_set (videorate, "drop-only", TRUE, NULL);
fail_unless (gst_element_set_state (videorate,
GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
"could not set to playing");
caps = gst_caps_from_string (VIDEO_CAPS_STRING);
gst_check_setup_events (mysrcpad, videorate, caps, GST_FORMAT_TIME);
gst_caps_unref (caps);
/* Segment starts at 0 */
gst_segment_init (&segment, GST_FORMAT_TIME);
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
buf = gst_buffer_new_and_alloc (4);
GST_BUFFER_PTS (buf) = 0;
fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK);
fail_unless_equals_int (g_list_length (buffers), 1);
/* cleanup */
cleanup_videorate (videorate);
}
GST_END_TEST;
static Suite *
videorate_suite (void)
{
@ -2202,6 +2256,7 @@ videorate_suite (void)
tcase_add_test (tc_chain, test_segment_update_same);
tcase_add_test (tc_chain, test_segment_update_average_period);
tcase_add_test (tc_chain, test_segment_update);
tcase_add_test (tc_chain, test_drop_only_ref_count);
return s;
}