From ac3e8c7c40874e70c8d1dc94d382307099d6dddf Mon Sep 17 00:00:00 2001 From: He Junyan Date: Fri, 6 Dec 2019 14:21:33 +0800 Subject: [PATCH] libs: decoder: context: remove surfaces binding from context. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The vaCreateContext do not need to specify the surfaces for the context creation now. So we do not need to bind any surface to the context anymore. Surfaces should be the resource belong to display and just be used in encoder/decoder context. The previous manner has big limitation for decoder. The context's surface number is decided by dpb size. All the surfaces in dpb will be attached to a gstbuffer and be pushed to down stream, and the decoder need to wait down stream free the surface and go on if not enough surface available. For more and more use cases, this causes deadlock. For example, gst-launch-1.0 filesrc location=a.h264 ! h264parse ! vaapih264dec ! x264enc ! filesink location=./output.h264 will cause deadlock and make the whole pipeline hang. the x264enc encoder need to cache more than dpb size surfaces. The best solution is seperating the surfaces number and the dpb size. dpb and dpb size shoule be virtual concepts maintained by the decoder. And let the surfaces_pool in context maintain the re-use of all surfaces. For encoder, the situation is better, all the surfaces are just used as reference frame and no need to be pushed to down stream. We can just reserve and set the capacity of the surfaces_pool to meet the request. Fix: #147 Fix: #88 Co-Author: Víctor Manuel Jáquez Leal Part-of: --- gst-libs/gst/vaapi/gstvaapicontext.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/gst-libs/gst/vaapi/gstvaapicontext.c b/gst-libs/gst/vaapi/gstvaapicontext.c index ea6d763218..8a361f3f96 100644 --- a/gst-libs/gst/vaapi/gstvaapicontext.c +++ b/gst-libs/gst/vaapi/gstvaapicontext.c @@ -165,7 +165,7 @@ context_ensure_surfaces (GstVaapiContext * context) const guint num_surfaces = cip->ref_frames + SCRATCH_SURFACES_COUNT; GstVaapiSurface *surface; GstVideoFormat format; - guint i; + guint i, capacity; format = get_preferred_format (context); for (i = context->surfaces->len; i < num_surfaces; i++) { @@ -182,7 +182,9 @@ context_ensure_surfaces (GstVaapiContext * context) if (!gst_vaapi_video_pool_add_object (context->surfaces_pool, surface)) return FALSE; } - gst_vaapi_video_pool_set_capacity (context->surfaces_pool, num_surfaces); + + capacity = cip->usage == GST_VAAPI_CONTEXT_USAGE_DECODE ? 0 : num_surfaces; + gst_vaapi_video_pool_set_capacity (context->surfaces_pool, capacity); return TRUE; } @@ -219,10 +221,12 @@ context_create (GstVaapiContext * context) GstVaapiDisplay *const display = GST_VAAPI_CONTEXT_DISPLAY (context); VAContextID context_id; VASurfaceID surface_id; + VASurfaceID *surfaces_data = NULL; VAStatus status; GArray *surfaces = NULL; gboolean success = FALSE; guint i; + gint num_surfaces = 0; if (!context->surfaces && !context_create_surfaces (context)) goto cleanup; @@ -240,12 +244,22 @@ context_create (GstVaapiContext * context) surface_id = GST_VAAPI_SURFACE_ID (surface); g_array_append_val (surfaces, surface_id); } + g_assert (surfaces->len == context->surfaces->len); + /* vaCreateContext() doesn't really need an array of VASurfaceIDs (see + * https://lists.01.org/pipermail/intel-vaapi-media/2017-July/000052.html and + * https://github.com/intel/libva/issues/251); pass a dummy list of valid + * (non-null) IDs until the signature gets updated. */ + if (cip->usage != GST_VAAPI_CONTEXT_USAGE_DECODE) { + surfaces_data = (VASurfaceID *) surfaces->data; + num_surfaces = surfaces->len; + } + GST_VAAPI_DISPLAY_LOCK (display); status = vaCreateContext (GST_VAAPI_DISPLAY_VADISPLAY (display), context->va_config, cip->width, cip->height, VA_PROGRESSIVE, - (VASurfaceID *) surfaces->data, surfaces->len, &context_id); + surfaces_data, num_surfaces, &context_id); GST_VAAPI_DISPLAY_UNLOCK (display); if (!vaapi_check_status (status, "vaCreateContext()")) goto cleanup; @@ -627,6 +641,8 @@ gst_vaapi_context_get_surface_count (GstVaapiContext * context) { g_return_val_if_fail (context != NULL, 0); + if (gst_vaapi_video_pool_get_capacity (context->surfaces_pool) == 0) + return G_MAXUINT; return gst_vaapi_video_pool_get_size (context->surfaces_pool); }