vkencoder-private: avoid GstVulkanEncoderPicture allocation

By using it as apart of the encoder picture structure that has to initialized
and uninitalized.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8007>
This commit is contained in:
Víctor Manuel Jáquez Leal 2024-09-06 11:23:40 +02:00 committed by GStreamer Marge Bot
parent c1e364ecdc
commit d9e9d2ff72
4 changed files with 51 additions and 58 deletions

View file

@ -372,31 +372,29 @@ gst_vulkan_encoder_new_video_session_parameters (GstVulkanEncoder * self,
} }
/** /**
* gst_vulkan_encoder_picture_new: * gst_vulkan_encode_picture_init:
* @pic: the #GstVulkanEncoderPicture to initialize
* @self: the #GstVulkanEncoder with the pool's configuration. * @self: the #GstVulkanEncoder with the pool's configuration.
* @in_buffer: (transfer none): the input #GstBuffer. * @in_buffer: (transfer none): the input #GstBuffer.
* @size: size of the output buffer * @size: size of the output buffer
* *
* Create a new vulkan encode picture from the input buffer. * Initialize @pic structure.
*
* Returns: a new #GstVulkanEncoderPicture.
* *
* Returns: %TRUE if @pic was initialized correctly; otherwise %FALSE
*/ */
GstVulkanEncoderPicture * gboolean
gst_vulkan_encoder_picture_new (GstVulkanEncoder * self, GstBuffer * in_buffer, gst_vulkan_encoder_picture_init (GstVulkanEncoderPicture * pic,
gsize size) GstVulkanEncoder * self, GstBuffer * in_buffer, gsize size)
{ {
GstVulkanEncoderPicture *pic;
GstVulkanEncoderPrivate *priv; GstVulkanEncoderPrivate *priv;
gsize size_aligned; gsize size_aligned;
g_return_val_if_fail (GST_IS_VULKAN_ENCODER (self), NULL); g_return_val_if_fail (pic != NULL, FALSE);
g_return_val_if_fail (GST_IS_BUFFER (in_buffer), NULL); g_return_val_if_fail (GST_IS_VULKAN_ENCODER (self), FALSE);
g_return_val_if_fail (GST_IS_BUFFER (in_buffer), FALSE);
priv = gst_vulkan_encoder_get_instance_private (self); priv = gst_vulkan_encoder_get_instance_private (self);
pic = g_new0 (GstVulkanEncoderPicture, 1);
size_aligned = GST_ROUND_UP_N (size, size_aligned = GST_ROUND_UP_N (size,
priv->caps.caps.minBitstreamBufferSizeAlignment); priv->caps.caps.minBitstreamBufferSizeAlignment);
@ -409,10 +407,8 @@ gst_vulkan_encoder_picture_new (GstVulkanEncoder * self, GstBuffer * in_buffer,
g_assert (GST_IS_BUFFER_POOL (priv->dpb_pool)); g_assert (GST_IS_BUFFER_POOL (priv->dpb_pool));
ret = ret =
gst_buffer_pool_acquire_buffer (priv->dpb_pool, &pic->dpb_buffer, NULL); gst_buffer_pool_acquire_buffer (priv->dpb_pool, &pic->dpb_buffer, NULL);
if (ret != GST_FLOW_OK) { if (ret != GST_FLOW_OK)
gst_vulkan_encoder_picture_free (pic); return FALSE;
return NULL;
}
} }
pic->in_buffer = gst_buffer_ref (in_buffer); pic->in_buffer = gst_buffer_ref (in_buffer);
pic->out_buffer = pic->out_buffer =
@ -431,18 +427,17 @@ gst_vulkan_encoder_picture_new (GstVulkanEncoder * self, GstBuffer * in_buffer,
priv->layered_dpb, FALSE, NULL); priv->layered_dpb, FALSE, NULL);
} }
return pic; return TRUE;
} }
/** /**
* gst_vulkan_encoder_picture_free: * gst_vulkan_encoder_picture_clear:
* @pic: the #GstVulkanEncoderPicture to free. * @pic: the #GstVulkanEncoderPicture to free.
* *
* Free the #GstVulkanEncoderPicture. * Release data of @pic.
*
*/ */
void void
gst_vulkan_encoder_picture_free (GstVulkanEncoderPicture * pic) gst_vulkan_encoder_picture_clear (GstVulkanEncoderPicture * pic)
{ {
g_return_if_fail (pic != NULL); g_return_if_fail (pic != NULL);
@ -455,8 +450,6 @@ gst_vulkan_encoder_picture_free (GstVulkanEncoderPicture * pic)
gst_vulkan_image_view_unref (pic->dpb_view); gst_vulkan_image_view_unref (pic->dpb_view);
pic->dpb_view = NULL; pic->dpb_view = NULL;
g_free (pic);
} }
/** /**

View file

@ -164,8 +164,9 @@ gboolean gst_vulkan_encoder_caps (GstVulkanEncode
GST_VULKAN_API GST_VULKAN_API
GstCaps * gst_vulkan_encoder_profile_caps (GstVulkanEncoder * self); GstCaps * gst_vulkan_encoder_profile_caps (GstVulkanEncoder * self);
GST_VULKAN_API GST_VULKAN_API
GstVulkanEncoderPicture* gst_vulkan_encoder_picture_new (GstVulkanEncoder * self, gboolean gst_vulkan_encoder_picture_init (GstVulkanEncoderPicture * pic,
GstVulkanEncoder * self,
GstBuffer * in_buffer, GstBuffer * in_buffer,
gsize size); gsize size);
GST_VULKAN_API GST_VULKAN_API
void gst_vulkan_encoder_picture_free (GstVulkanEncoderPicture * pic); void gst_vulkan_encoder_picture_clear (GstVulkanEncoderPicture * pic);

View file

@ -45,7 +45,7 @@ static GstVideoInfo out_info;
typedef struct typedef struct
{ {
GstVulkanEncoderPicture *picture; GstVulkanEncoderPicture picture;
gboolean is_ref; gboolean is_ref;
gint pic_num; gint pic_num;
@ -65,13 +65,14 @@ typedef struct
} GstVulkanH264EncodeFrame; } GstVulkanH264EncodeFrame;
static GstVulkanH264EncodeFrame * static GstVulkanH264EncodeFrame *
_h264_encode_frame_new (GstVulkanEncoderPicture * picture, gboolean is_ref) _h264_encode_frame_new (GstVulkanEncoder * enc, GstBuffer * img_buffer,
gsize size, gboolean is_ref)
{ {
GstVulkanH264EncodeFrame *frame; GstVulkanH264EncodeFrame *frame;
g_return_val_if_fail (picture, NULL);
frame = g_new (GstVulkanH264EncodeFrame, 1); frame = g_new (GstVulkanH264EncodeFrame, 1);
frame->picture = picture; fail_unless (gst_vulkan_encoder_picture_init (&frame->picture, enc,
img_buffer, size));
frame->is_ref = is_ref; frame->is_ref = is_ref;
return frame; return frame;
@ -81,7 +82,8 @@ static void
_h264_encode_frame_free (gpointer pframe) _h264_encode_frame_free (gpointer pframe)
{ {
GstVulkanH264EncodeFrame *frame = pframe; GstVulkanH264EncodeFrame *frame = pframe;
g_clear_pointer (&frame->picture, gst_vulkan_encoder_picture_free);
gst_vulkan_encoder_picture_clear (&frame->picture);
g_free (frame); g_free (frame);
} }
@ -361,10 +363,8 @@ allocate_frame (GstVulkanEncoder * enc, int width,
upload_buffer_to_image(img_pool, in_buffer, &img_buffer); upload_buffer_to_image(img_pool, in_buffer, &img_buffer);
frame = _h264_encode_frame_new (gst_vulkan_encoder_picture_new (enc, frame = _h264_encode_frame_new (enc, img_buffer, width * height * 3, is_ref);
img_buffer, width * height * 3), is_ref);
fail_unless (frame); fail_unless (frame);
fail_unless (frame->picture);
gst_buffer_unref (in_buffer); gst_buffer_unref (in_buffer);
gst_buffer_unref (img_buffer); gst_buffer_unref (img_buffer);
@ -387,7 +387,7 @@ encode_frame (GstVulkanEncoder * enc, GstVulkanH264EncodeFrame * frame,
guint qp_i = 26; guint qp_i = 26;
guint qp_p = 26; guint qp_p = 26;
guint qp_b = 26; guint qp_b = 26;
GstVulkanEncoderPicture *picture = frame->picture; GstVulkanEncoderPicture *picture = &frame->picture;
GST_DEBUG ("Encoding frame num:%d", frame_num); GST_DEBUG ("Encoding frame num:%d", frame_num);
@ -544,12 +544,12 @@ encode_frame (GstVulkanEncoder * enc, GstVulkanH264EncodeFrame * frame,
picture->codec_dpb_slot_info = &frame->dpb_slot_info; picture->codec_dpb_slot_info = &frame->dpb_slot_info;
for (i = 0; i < list0_num; i++) { for (i = 0; i < list0_num; i++) {
ref_pics[i] = list0[i]->picture; ref_pics[i] = &list0[i]->picture;
frame->ref_list_info.RefPicList0[0] = list0[i]->picture->slotIndex; frame->ref_list_info.RefPicList0[0] = list0[i]->picture.slotIndex;
} }
for (i = 0; i < list1_num; i++) { for (i = 0; i < list1_num; i++) {
ref_pics[i + list0_num] = list1[i]->picture; ref_pics[i + list0_num] = &list1[i]->picture;
frame->ref_list_info.RefPicList1[i] = list1[i]->picture->slotIndex; frame->ref_list_info.RefPicList1[i] = list1[i]->picture.slotIndex;
} }
fail_unless (gst_vulkan_encoder_encode (enc, &in_info, picture, ref_pics_num, fail_unless (gst_vulkan_encoder_encode (enc, &in_info, picture, ref_pics_num,
@ -762,12 +762,12 @@ check_encoded_frame (GstVulkanH264EncodeFrame * frame,
GstH264NalUnitType nal_type) GstH264NalUnitType nal_type)
{ {
GstMapInfo info; GstMapInfo info;
fail_unless (frame->picture->out_buffer != NULL); fail_unless (frame->picture.out_buffer != NULL);
gst_buffer_map (frame->picture->out_buffer, &info, GST_MAP_READ); gst_buffer_map (frame->picture.out_buffer, &info, GST_MAP_READ);
fail_unless (info.size); fail_unless (info.size);
GST_MEMDUMP ("out buffer", info.data, info.size); GST_MEMDUMP ("out buffer", info.data, info.size);
check_h264_nalu (info.data, info.size, nal_type); check_h264_nalu (info.data, info.size, nal_type);
gst_buffer_unmap (frame->picture->out_buffer, &info); gst_buffer_unmap (frame->picture.out_buffer, &info);
} }
/* Greater than the maxDpbSlots == 16*/ /* Greater than the maxDpbSlots == 16*/
@ -905,7 +905,6 @@ GST_START_TEST (test_encoder_h264_i_p_b)
/* Encode 1st picture as an IDR-Frame */ /* Encode 1st picture as an IDR-Frame */
frame = allocate_frame (enc, width, height, TRUE); frame = allocate_frame (enc, width, height, TRUE);
fail_unless (frame->picture != NULL);
encode_frame (enc, frame, STD_VIDEO_H264_SLICE_TYPE_I, encode_frame (enc, frame, STD_VIDEO_H264_SLICE_TYPE_I,
frame_num, NULL, 0, NULL, 0, sps_id, pps_id); frame_num, NULL, 0, NULL, 0, sps_id, pps_id);
check_encoded_frame (frame, GST_H264_NAL_SLICE_IDR); check_encoded_frame (frame, GST_H264_NAL_SLICE_IDR);

View file

@ -47,7 +47,7 @@ static GstVideoInfo in_info;
static GstVideoInfo out_info; static GstVideoInfo out_info;
typedef struct typedef struct
{ {
GstVulkanEncoderPicture *picture; GstVulkanEncoderPicture picture;
gboolean is_ref; gboolean is_ref;
gint pic_num; gint pic_num;
@ -70,13 +70,14 @@ typedef struct
static GstVulkanH265EncodeFrame * static GstVulkanH265EncodeFrame *
_h265_encode_frame_new (GstVulkanEncoderPicture * picture, gboolean is_ref) _h265_encode_frame_new (GstVulkanEncoder * enc, GstBuffer * img_buffer,
gsize size, gboolean is_ref)
{ {
GstVulkanH265EncodeFrame *frame; GstVulkanH265EncodeFrame *frame;
g_return_val_if_fail (picture, NULL);
frame = g_new (GstVulkanH265EncodeFrame, 1); frame = g_new (GstVulkanH265EncodeFrame, 1);
frame->picture = picture; fail_unless (gst_vulkan_encoder_picture_init (&frame->picture, enc,
img_buffer, size));
frame->is_ref = is_ref; frame->is_ref = is_ref;
return frame; return frame;
@ -86,7 +87,8 @@ static void
_h265_encode_frame_free (gpointer pframe) _h265_encode_frame_free (gpointer pframe)
{ {
GstVulkanH265EncodeFrame *frame = pframe; GstVulkanH265EncodeFrame *frame = pframe;
g_clear_pointer (&frame->picture, gst_vulkan_encoder_picture_free);
gst_vulkan_encoder_picture_clear (&frame->picture);
g_free (frame); g_free (frame);
} }
@ -368,10 +370,8 @@ allocate_frame (GstVulkanEncoder * enc, int width,
/* get a Vulkan image buffer out of the input buffer */ /* get a Vulkan image buffer out of the input buffer */
upload_buffer_to_image(img_pool, in_buffer, &img_buffer); upload_buffer_to_image(img_pool, in_buffer, &img_buffer);
frame = _h265_encode_frame_new (gst_vulkan_encoder_picture_new (enc, frame = _h265_encode_frame_new (enc, img_buffer, width * height * 3, is_ref);
img_buffer, width * height * 3), is_ref);
fail_unless (frame); fail_unless (frame);
fail_unless (frame->picture);
gst_buffer_unref (in_buffer); gst_buffer_unref (in_buffer);
gst_buffer_unref (img_buffer); gst_buffer_unref (img_buffer);
@ -398,7 +398,7 @@ encode_frame (GstVulkanEncoder * enc, GstVulkanH265EncodeFrame * frame,
guint qp_i = 26; guint qp_i = 26;
guint qp_p = 26; guint qp_p = 26;
guint qp_b = 26; guint qp_b = 26;
GstVulkanEncoderPicture *picture = frame->picture; GstVulkanEncoderPicture *picture = &frame->picture;
gint picture_type = PICTURE_TYPE(slice_type, frame->is_ref); gint picture_type = PICTURE_TYPE(slice_type, frame->is_ref);
GST_DEBUG ("Encoding frame num: %d", frame_num); GST_DEBUG ("Encoding frame num: %d", frame_num);
@ -610,12 +610,12 @@ encode_frame (GstVulkanEncoder * enc, GstVulkanH265EncodeFrame * frame,
picture->codec_dpb_slot_info = &frame->dpb_slot_info; picture->codec_dpb_slot_info = &frame->dpb_slot_info;
for (i = 0; i < list0_num; i++) { for (i = 0; i < list0_num; i++) {
ref_pics[i] = list0[i]->picture; ref_pics[i] = &list0[i]->picture;
frame->ref_list_info.RefPicList0[0] = list0[i]->picture->slotIndex; frame->ref_list_info.RefPicList0[0] = list0[i]->picture.slotIndex;
} }
for (i = 0; i < list1_num; i++) { for (i = 0; i < list1_num; i++) {
ref_pics[i + list0_num] = list1[i]->picture; ref_pics[i + list0_num] = &list1[i]->picture;
frame->ref_list_info.RefPicList1[i] = list1[i]->picture->slotIndex; frame->ref_list_info.RefPicList1[i] = list1[i]->picture.slotIndex;
} }
fail_unless (gst_vulkan_encoder_encode (enc, &in_info, picture, ref_pics_num, fail_unless (gst_vulkan_encoder_encode (enc, &in_info, picture, ref_pics_num,
@ -933,12 +933,12 @@ check_encoded_frame (GstVulkanH265EncodeFrame * frame,
GstH265NalUnitType nal_type) GstH265NalUnitType nal_type)
{ {
GstMapInfo info; GstMapInfo info;
fail_unless (frame->picture->out_buffer != NULL); fail_unless (frame->picture.out_buffer != NULL);
gst_buffer_map (frame->picture->out_buffer, &info, GST_MAP_READ); gst_buffer_map (frame->picture.out_buffer, &info, GST_MAP_READ);
fail_unless (info.size); fail_unless (info.size);
GST_MEMDUMP ("out buffer", info.data, info.size); GST_MEMDUMP ("out buffer", info.data, info.size);
check_h265_nalu (info.data, info.size, nal_type); check_h265_nalu (info.data, info.size, nal_type);
gst_buffer_unmap (frame->picture->out_buffer, &info); gst_buffer_unmap (frame->picture.out_buffer, &info);
} }
/* Greater than the maxDpbSlots == 16*/ /* Greater than the maxDpbSlots == 16*/