plugin: encode: extract the allowed caps maker as a helper function.

Extract all logic about making caps for encode's sink as a standalone
helper function. It can be reused.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-vaapi/-/merge_requests/315>
This commit is contained in:
He Junyan 2020-04-10 21:27:32 +08:00 committed by Víctor Manuel Jáquez Leal
parent 2b1809e9d3
commit 3583a4b86c
3 changed files with 70 additions and 36 deletions

View file

@ -358,13 +358,9 @@ static gboolean
ensure_allowed_sinkpad_caps (GstVaapiEncode * encode)
{
GstCaps *out_caps = NULL;
GstCaps *raw_caps = NULL;
GstCaps *va_caps, *dma_caps;
GArray *formats = NULL;
gboolean ret = FALSE;
GArray *profiles = NULL;
guint i, size;
GstStructure *structure;
gint min_width, min_height, max_width, max_height;
guint mem_types;
@ -385,34 +381,10 @@ ensure_allowed_sinkpad_caps (GstVaapiEncode * encode)
if (!formats)
goto failed_get_attributes;
raw_caps = gst_vaapi_video_format_new_template_caps_from_list (formats);
if (!raw_caps)
goto failed_create_raw_caps;
/* Set the width/height info to caps */
size = gst_caps_get_size (raw_caps);
for (i = 0; i < size; i++) {
structure = gst_caps_get_structure (raw_caps, i);
if (!structure)
continue;
gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, min_width,
max_width, "height", GST_TYPE_INT_RANGE, min_height, max_height, NULL);
}
out_caps = gst_caps_copy (raw_caps);
va_caps = gst_caps_copy (raw_caps);
gst_caps_set_features_simple (va_caps,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE));
gst_caps_append (out_caps, va_caps);
if (gst_vaapi_mem_type_supports (mem_types,
GST_VAAPI_BUFFER_MEMORY_TYPE_DMA_BUF)) {
dma_caps = gst_caps_copy (raw_caps);
gst_caps_set_features_simple (dma_caps,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_DMABUF));
gst_caps_append (out_caps, dma_caps);
}
out_caps = gst_vaapi_build_caps_from_formats (formats, min_width, min_height,
max_width, max_height, mem_types);
if (!out_caps)
goto failed_create_caps;
gst_caps_replace (&encode->allowed_sinkpad_caps, out_caps);
GST_INFO_OBJECT (encode, "Allowed sink caps %" GST_PTR_FORMAT,
@ -428,8 +400,6 @@ bail:
g_array_unref (profiles);
if (out_caps)
gst_caps_unref (out_caps);
if (raw_caps)
gst_caps_unref (raw_caps);
if (formats)
g_array_unref (formats);
return ret;
@ -439,9 +409,9 @@ failed_get_attributes:
GST_WARNING_OBJECT (encode, "failed to get surface attributes");
goto bail;
}
failed_create_raw_caps:
failed_create_caps:
{
GST_WARNING_OBJECT (encode, "failed to create raw sink caps");
GST_WARNING_OBJECT (encode, "failed to create sink caps");
goto bail;
}
failed_get_profiles:

View file

@ -24,6 +24,7 @@
#include "gstcompat.h"
#include "gstvaapivideocontext.h"
#include <gst/vaapi/gstvaapiprofilecaps.h>
#if USE_DRM
# include <gst/vaapi/gstvaapidisplay_drm.h>
#endif
@ -1079,3 +1080,61 @@ gst_vaapi_h26x_encoder_get_profiles_from_caps (GstCaps * caps,
return profiles;
}
/**
* gst_vaapi_build_caps_from_formats:
* @formats: an array of supported #GstVideoFormat
* @min_width: the min supported width
* @min_height: the min supported height
* @max_width: the max supported width
* @max_height: the max supported height
* @mem_types: the supported VA mem types
*
* This function generates a #GstCaps based on the information such as
* formats, width and height.
*
* Return: A #GstCaps.
**/
GstCaps *
gst_vaapi_build_caps_from_formats (GArray * formats, gint min_width,
gint min_height, gint max_width, gint max_height, guint mem_types)
{
GstCaps *out_caps = NULL;
GstCaps *raw_caps = NULL;
GstCaps *va_caps, *dma_caps;
guint i, size;
GstStructure *structure;
raw_caps = gst_vaapi_video_format_new_template_caps_from_list (formats);
if (!raw_caps)
return NULL;
/* Set the width/height info to caps */
size = gst_caps_get_size (raw_caps);
for (i = 0; i < size; i++) {
structure = gst_caps_get_structure (raw_caps, i);
if (!structure)
continue;
gst_structure_set (structure, "width", GST_TYPE_INT_RANGE, min_width,
max_width, "height", GST_TYPE_INT_RANGE, min_height, max_height, NULL);
}
out_caps = gst_caps_copy (raw_caps);
va_caps = gst_caps_copy (raw_caps);
gst_caps_set_features_simple (va_caps,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_VAAPI_SURFACE));
gst_caps_append (out_caps, va_caps);
if (gst_vaapi_mem_type_supports (mem_types,
GST_VAAPI_BUFFER_MEMORY_TYPE_DMA_BUF)) {
dma_caps = gst_caps_copy (raw_caps);
gst_caps_set_features_simple (dma_caps,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_DMABUF));
gst_caps_append (out_caps, dma_caps);
}
gst_caps_unref (raw_caps);
return out_caps;
}

View file

@ -160,4 +160,9 @@ GArray *
gst_vaapi_h26x_encoder_get_profiles_from_caps (GstCaps * caps,
GstVaapiStrToProfileFunc func);
G_GNUC_INTERNAL
GstCaps *
gst_vaapi_build_caps_from_formats (GArray * formats, gint min_width,
gint min_height, gint max_width, gint max_height, guint mem_type);
#endif /* GST_VAAPI_PLUGIN_UTIL_H */