nvh264dec,nvh265dec: Don't realloc bitstream buffer per slice

Allocated memory size has not been updated which results in
realloc per slice. Fixing it and also release bitstream buffer
on ::close(), not finalize.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2379>
This commit is contained in:
Seungha Yang 2022-05-04 23:36:30 +09:00 committed by Tim-Philipp Müller
parent 222963bd5a
commit 4448cb64c8
2 changed files with 26 additions and 34 deletions

View file

@ -125,7 +125,6 @@ struct _GstNvH264DecClass
G_DEFINE_TYPE (GstNvH264Dec, gst_nv_h264_dec, GST_TYPE_H264_DECODER);
static void gst_nv_h264_decoder_dispose (GObject * object);
static void gst_nv_h264_decoder_finalize (GObject * object);
static void gst_nv_h264_dec_set_context (GstElement * element,
GstContext * context);
static gboolean gst_nv_h264_dec_open (GstVideoDecoder * decoder);
@ -171,7 +170,6 @@ gst_nv_h264_dec_class_init (GstNvH264DecClass * klass)
*/
object_class->dispose = gst_nv_h264_decoder_dispose;
object_class->finalize = gst_nv_h264_decoder_finalize;
element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_h264_dec_set_context);
@ -224,17 +222,6 @@ gst_nv_h264_decoder_dispose (GObject * object)
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gst_nv_h264_decoder_finalize (GObject * object)
{
GstNvH264Dec *self = GST_NV_H264_DEC (object);
g_free (self->bitstream_buffer);
g_free (self->slice_offsets);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_nv_h264_dec_set_context (GstElement * element, GstContext * context)
{
@ -304,6 +291,12 @@ gst_nv_h264_dec_close (GstVideoDecoder * decoder)
gst_clear_object (&self->decoder);
gst_clear_object (&self->context);
g_clear_pointer (&self->bitstream_buffer, g_free);
g_clear_pointer (&self->slice_offsets, g_free);
self->bitstream_buffer_alloc_size = 0;
self->slice_offsets_alloc_len = 0;
return TRUE;
}
@ -821,8 +814,10 @@ gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
GST_LOG_OBJECT (self, "Decode slice, nalu size %u", slice->nalu.size);
if (self->slice_offsets_alloc_len < self->num_slices + 1) {
self->slice_offsets_alloc_len = 2 * (self->num_slices + 1);
self->slice_offsets = (guint *) g_realloc_n (self->slice_offsets,
self->num_slices + 1, sizeof (guint));
self->slice_offsets_alloc_len, sizeof (guint));
}
self->slice_offsets[self->num_slices] = self->bitstream_buffer_offset;
GST_LOG_OBJECT (self, "Slice offset %u for slice %d",
@ -832,8 +827,10 @@ gst_nv_h264_dec_decode_slice (GstH264Decoder * decoder,
new_size = self->bitstream_buffer_offset + slice->nalu.size + 3;
if (self->bitstream_buffer_alloc_size < new_size) {
self->bitstream_buffer =
(guint8 *) g_realloc (self->bitstream_buffer, new_size);
self->bitstream_buffer_alloc_size = 2 * new_size;
self->bitstream_buffer = (guint8 *) g_realloc (self->bitstream_buffer,
self->bitstream_buffer_alloc_size);
}
self->bitstream_buffer[self->bitstream_buffer_offset] = 0;

View file

@ -119,7 +119,6 @@ struct _GstNvH265DecClass
#define gst_nv_h265_dec_parent_class parent_class
G_DEFINE_TYPE (GstNvH265Dec, gst_nv_h265_dec, GST_TYPE_H265_DECODER);
static void gst_nv_h265_decoder_finalize (GObject * object);
static void gst_nv_h265_dec_set_context (GstElement * element,
GstContext * context);
static gboolean gst_nv_h265_dec_open (GstVideoDecoder * decoder);
@ -148,7 +147,6 @@ static GstFlowReturn gst_nv_h265_dec_end_picture (GstH265Decoder * decoder,
static void
gst_nv_h265_dec_class_init (GstNvH265DecClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
GstVideoDecoderClass *decoder_class = GST_VIDEO_DECODER_CLASS (klass);
GstH265DecoderClass *h265decoder_class = GST_H265_DECODER_CLASS (klass);
@ -159,8 +157,6 @@ gst_nv_h265_dec_class_init (GstNvH265DecClass * klass)
* Since: 1.18
*/
object_class->finalize = gst_nv_h265_decoder_finalize;
element_class->set_context = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_set_context);
decoder_class->open = GST_DEBUG_FUNCPTR (gst_nv_h265_dec_open);
@ -194,17 +190,6 @@ gst_nv_h265_dec_init (GstNvH265Dec * self)
{
}
static void
gst_nv_h265_decoder_finalize (GObject * object)
{
GstNvH265Dec *self = GST_NV_H265_DEC (object);
g_free (self->bitstream_buffer);
g_free (self->slice_offsets);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_nv_h265_dec_set_context (GstElement * element, GstContext * context)
{
@ -258,6 +243,12 @@ gst_nv_h265_dec_close (GstVideoDecoder * decoder)
gst_clear_object (&self->decoder);
gst_clear_object (&self->context);
g_clear_pointer (&self->bitstream_buffer, g_free);
g_clear_pointer (&self->slice_offsets, g_free);
self->bitstream_buffer_alloc_size = 0;
self->slice_offsets_alloc_len = 0;
return TRUE;
}
@ -870,8 +861,10 @@ gst_nv_h265_dec_decode_slice (GstH265Decoder * decoder,
GST_LOG_OBJECT (self, "Decode slice, nalu size %u", slice->nalu.size);
if (self->slice_offsets_alloc_len < self->num_slices + 1) {
self->slice_offsets_alloc_len = 2 * (self->num_slices + 1);
self->slice_offsets = (guint *) g_realloc_n (self->slice_offsets,
self->num_slices + 1, sizeof (guint));
self->slice_offsets_alloc_len, sizeof (guint));
}
self->slice_offsets[self->num_slices] = self->bitstream_buffer_offset;
GST_LOG_OBJECT (self, "Slice offset %u for slice %d",
@ -881,8 +874,10 @@ gst_nv_h265_dec_decode_slice (GstH265Decoder * decoder,
new_size = self->bitstream_buffer_offset + slice->nalu.size + 3;
if (self->bitstream_buffer_alloc_size < new_size) {
self->bitstream_buffer =
(guint8 *) g_realloc (self->bitstream_buffer, new_size);
self->bitstream_buffer_alloc_size = 2 * new_size;
self->bitstream_buffer = (guint8 *) g_realloc (self->bitstream_buffer,
self->bitstream_buffer_alloc_size);
}
self->bitstream_buffer[self->bitstream_buffer_offset] = 0;