va: encoder: Don't preallocate reconstruct buffers.
authorVíctor Manuel Jáquez Leal <vjaquez@igalia.com>
Thu, 3 Mar 2022 14:06:00 +0000 (15:06 +0100)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Mon, 7 Mar 2022 16:31:41 +0000 (16:31 +0000)
It's not required by VA to register the reconstruct buffers at context
creation, just as in decoders.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1857>

subprojects/gst-plugins-bad/sys/va/gstvaencoder.c
subprojects/gst-plugins-bad/sys/va/gstvaencoder.h
subprojects/gst-plugins-bad/sys/va/gstvah264enc.c

index b99276a..f35b346 100644 (file)
@@ -303,7 +303,8 @@ _get_surface_formats (GstVaDisplay * display, VAConfigID config)
 
 static GstBufferPool *
 _create_reconstruct_pool (GstVaDisplay * display, GArray * surface_formats,
-    GstVideoFormat format, gint coded_width, gint coded_height, guint max_num)
+    GstVideoFormat format, gint coded_width, gint coded_height,
+    guint max_buffers)
 {
   GstAllocator *allocator = NULL;
   guint usage_hint = VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER;
@@ -331,8 +332,8 @@ _create_reconstruct_pool (GstVaDisplay * display, GArray * surface_formats,
 
   gst_allocation_params_init (&params);
 
-  pool = gst_va_pool_new_with_config (caps, size, 1, max_num,
-      usage_hint, allocator, &params);
+  pool = gst_va_pool_new_with_config (caps, size, 0, max_buffers, usage_hint,
+      allocator, &params);
 
   gst_clear_object (&allocator);
   gst_clear_caps (&caps);
@@ -344,7 +345,7 @@ gboolean
 gst_va_encoder_open (GstVaEncoder * self, VAProfile profile,
     VAEntrypoint entrypoint, GstVideoFormat video_format, guint rt_format,
     gint coded_width, gint coded_height, gint codedbuf_size,
-    guint reconstruct_buffer_num, guint rc_ctrl, guint32 packed_headers)
+    guint max_reconstruct_surfaces, guint rc_ctrl, guint32 packed_headers)
 {
   /* *INDENT-OFF* */
   VAConfigAttrib attribs[3] = {
@@ -355,15 +356,12 @@ gst_va_encoder_open (GstVaEncoder * self, VAProfile profile,
   VAConfigID config = VA_INVALID_ID;
   VAContextID context = VA_INVALID_ID;
   VADisplay dpy;
-  GPtrArray *reconstruct_buffers = NULL;
-  GArray *surfaces = NULL;
   GArray *surface_formats = NULL;
   VAStatus status;
   GstBufferPool *recon_pool = NULL;
-  guint i, attrib_idx = 2;
+  guint attrib_idx = 2;
 
   g_return_val_if_fail (GST_IS_VA_ENCODER (self), FALSE);
-  g_return_val_if_fail (reconstruct_buffer_num > 0, FALSE);
   g_return_val_if_fail (codedbuf_size > 0, FALSE);
 
   if (gst_va_encoder_is_open (self))
@@ -397,62 +395,20 @@ gst_va_encoder_open (GstVaEncoder * self, VAProfile profile,
   }
 
   recon_pool = _create_reconstruct_pool (self->display, surface_formats,
-      video_format, coded_width, coded_height, reconstruct_buffer_num);
+      video_format, coded_width, coded_height, max_reconstruct_surfaces);
   if (!recon_pool) {
     GST_ERROR_OBJECT (self, "Failed to create reconstruct pool");
     goto error;
   }
   gst_buffer_pool_set_active (recon_pool, TRUE);
 
-  /* Create VA surfaces list for vaCreateContext() */
-  surfaces = g_array_sized_new (FALSE, FALSE, sizeof (VASurfaceID),
-      reconstruct_buffer_num);
-  if (!surfaces)
-    goto error;
-
-  reconstruct_buffers = g_ptr_array_sized_new (reconstruct_buffer_num);
-  if (!reconstruct_buffers)
-    goto error;
-
-  g_ptr_array_set_free_func (reconstruct_buffers,
-      (GDestroyNotify) gst_buffer_unref);
-
-  /* The encoder need to binding all reconstruct surface when create contex,
-     we have to allocate them all here. */
-  for (i = 0; i < reconstruct_buffer_num; i++) {
-    GstBuffer *buffer;
-    VASurfaceID surface_id;
-    GstFlowReturn ret;
-    GstBufferPoolAcquireParams buffer_pool_params = { 0, };
-
-    buffer_pool_params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT;
-    ret = gst_buffer_pool_acquire_buffer (recon_pool, &buffer,
-        &buffer_pool_params);
-    if (ret != GST_FLOW_OK) {
-      GST_ERROR_OBJECT (self, "Failed to create the reconstruct picture.");
-      goto error;
-    }
-
-    surface_id = gst_va_buffer_get_surface (buffer);
-    g_assert (surface_id != VA_INVALID_ID);
-
-    g_ptr_array_add (reconstruct_buffers, buffer);
-    g_array_append_val (surfaces, surface_id);
-  }
-
-  g_assert (surfaces->len == reconstruct_buffer_num);
-
   status = vaCreateContext (dpy, config, coded_width, coded_height,
-      VA_PROGRESSIVE, (VASurfaceID *) surfaces->data, reconstruct_buffer_num,
-      &context);
+      VA_PROGRESSIVE, NULL, 0, &context);
   if (status != VA_STATUS_SUCCESS) {
     GST_ERROR_OBJECT (self, "vaCreateConfig: %s", vaErrorStr (status));
     goto error;
   }
 
-  g_clear_pointer (&surfaces, g_array_unref);
-  g_clear_pointer (&reconstruct_buffers, g_ptr_array_unref);
-
   GST_OBJECT_LOCK (self);
 
   self->config = config;
@@ -475,8 +431,6 @@ gst_va_encoder_open (GstVaEncoder * self, VAProfile profile,
   return TRUE;
 
 error:
-  g_clear_pointer (&surfaces, g_array_unref);
-  g_clear_pointer (&reconstruct_buffers, g_ptr_array_unref);
   g_clear_pointer (&recon_pool, gst_object_unref);
 
   if (config != VA_INVALID_ID)
@@ -1149,7 +1103,7 @@ gst_va_encode_picture_new (GstVaEncoder * self, GstBuffer * raw_buffer)
 
   GST_OBJECT_UNLOCK (self);
 
-  ret = gst_buffer_pool_acquire_buffer (self->recon_pool, &reconstruct_buffer,
+  ret = gst_buffer_pool_acquire_buffer (recon_pool, &reconstruct_buffer,
       &buffer_pool_params);
   gst_clear_object (&recon_pool);
 
index 016cb4f..138bb10 100644 (file)
@@ -53,7 +53,7 @@ gboolean              gst_va_encoder_open                 (GstVaEncoder * self,
                                                            gint coded_width,
                                                            gint coded_height,
                                                            gint codedbuf_size,
-                                                           guint reconstruct_buffer_num,
+                                                           guint max_reconstruct_surfaces,
                                                            guint rc_ctrl,
                                                            guint32 packed_headers);
 gboolean              gst_va_encoder_close                (GstVaEncoder * self);
index 72381e1..98cabfd 100644 (file)
@@ -3733,7 +3733,7 @@ gst_va_h264_enc_set_format (GstVideoEncoder * venc, GstVideoCodecState * state)
   GstVaH264Enc *self = GST_VA_H264_ENC (venc);
   GstVaH264EncClass *klass = GST_VA_H264_ENC_GET_CLASS (self);
   GstCaps *out_caps;
-  guint reconstruct_buffer_num;
+  guint max_ref_frames;
 
   g_return_val_if_fail (state->caps != NULL, FALSE);
 
@@ -3760,12 +3760,11 @@ gst_va_h264_enc_set_format (GstVideoEncoder * venc, GstVideoCodecState * state)
     return FALSE;
   }
 
-  reconstruct_buffer_num = self->gop.num_ref_frames + 3 /* scratch frames */ ;
+  max_ref_frames = self->gop.num_ref_frames + 3 /* scratch frames */ ;
   if (!gst_va_encoder_open (self->encoder, self->profile, self->entrypoint,
           GST_VIDEO_INFO_FORMAT (&self->in_info), self->rt_format,
           self->mb_width * 16, self->mb_height * 16, self->codedbuf_size,
-          reconstruct_buffer_num, self->rc.rc_ctrl_mode,
-          self->packed_headers)) {
+          max_ref_frames, self->rc.rc_ctrl_mode, self->packed_headers)) {
     GST_ERROR_OBJECT (self, "Failed to open the VA encoder.");
     return FALSE;
   }