From bc1a0323a9ba39caf6fa6585abda7d4a5296e7b7 Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Mon, 29 Jun 2020 13:23:12 -0400 Subject: [PATCH] v4l2slh264dec: Factor out bitstream allocation No functional changes. Part-of: --- sys/v4l2codecs/gstv4l2codech264dec.c | 50 +++++++++++++++++----------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/sys/v4l2codecs/gstv4l2codech264dec.c b/sys/v4l2codecs/gstv4l2codech264dec.c index 81fcdb3bde..eec5c44eb2 100644 --- a/sys/v4l2codecs/gstv4l2codech264dec.c +++ b/sys/v4l2codecs/gstv4l2codech264dec.c @@ -723,6 +723,34 @@ gst_v4l2_codec_h264_dec_new_sequence (GstH264Decoder * decoder, return TRUE; } +static gboolean +gst_v4l2_codec_h264_dec_ensure_bitstream (GstV4l2CodecH264Dec * self) +{ + if (self->bitstream) + goto done; + + self->bitstream = gst_v4l2_codec_allocator_alloc (self->sink_allocator); + + if (!self->bitstream) { + GST_ELEMENT_ERROR (self, RESOURCE, NO_SPACE_LEFT, + ("Not enough memory to decode H264 stream."), (NULL)); + return FALSE; + } + + if (!gst_memory_map (self->bitstream, &self->bitstream_map, GST_MAP_WRITE)) { + GST_ELEMENT_ERROR (self, RESOURCE, WRITE, + ("Could not access bitstream memory for writing"), (NULL)); + g_clear_pointer (&self->bitstream, gst_memory_unref); + return FALSE; + } + +done: + /* We use this field to track how much we have written */ + self->bitstream_map.size = 0; + + return TRUE; +} + static gboolean gst_v4l2_codec_h264_dec_start_picture (GstH264Decoder * decoder, GstH264Picture * picture, GstH264Slice * slice, GstH264Dpb * dpb) @@ -733,26 +761,8 @@ gst_v4l2_codec_h264_dec_start_picture (GstH264Decoder * decoder, if (!self->sink_allocator) return FALSE; - /* Ensure we have a bitstream to write into */ - if (!self->bitstream) { - self->bitstream = gst_v4l2_codec_allocator_alloc (self->sink_allocator); - - if (!self->bitstream) { - GST_ELEMENT_ERROR (decoder, RESOURCE, NO_SPACE_LEFT, - ("Not enough memory to decode H264 stream."), (NULL)); - return FALSE; - } - - if (!gst_memory_map (self->bitstream, &self->bitstream_map, GST_MAP_WRITE)) { - GST_ELEMENT_ERROR (decoder, RESOURCE, WRITE, - ("Could not access bitstream memory for writing"), (NULL)); - g_clear_pointer (&self->bitstream, gst_memory_unref); - return FALSE; - } - } - - /* We use this field to track how much we have written */ - self->bitstream_map.size = 0; + if (!gst_v4l2_codec_h264_dec_ensure_bitstream (self)) + return FALSE; gst_v4l2_codec_h264_dec_fill_pps (self, slice->header.pps); gst_v4l2_codec_h264_dec_fill_scaling_matrix (self, slice->header.pps);