From 5de27e06202ed9af6b70e759e8354765adc4d19d Mon Sep 17 00:00:00 2001
From: Michael Tretter <m.tretter@pengutronix.de>
Date: Tue, 31 Jan 2023 15:52:42 +0100
Subject: [PATCH] v4l2videoenc: always allocate CAPTURE buffer from our pool

The videoencoder base class always uses the negotiated allocator for allocating
coded buffers and ignores the negotiated buffer pool. Therefore, the
v4l2videoenc always has to copy buffers from the pool into the allocated
output buffers.

This breaks downstream elements that want to import the CAPTURE buffers of the
v4l2videoenc, since the v4l2videoenc copies the exported CAPTURE buffers and
sends the copies downstream.

Always use the CAPTURE buffer pool for acquiring CAPTURE buffers instead of
allocating the buffers in the base class.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4230>
---
 .../sys/v4l2/gstv4l2videoenc.c                | 22 ++++++++-----------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2videoenc.c b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2videoenc.c
index 2dee813720..d50e96ea0f 100644
--- a/subprojects/gst-plugins-good/sys/v4l2/gstv4l2videoenc.c
+++ b/subprojects/gst-plugins-good/sys/v4l2/gstv4l2videoenc.c
@@ -635,31 +635,27 @@ static void
 gst_v4l2_video_enc_loop (GstVideoEncoder * encoder)
 {
   GstV4l2VideoEnc *self = GST_V4L2_VIDEO_ENC (encoder);
+  GstBufferPool *pool = gst_v4l2_object_get_buffer_pool (self->v4l2capture);
+  GstV4l2BufferPool *cpool = GST_V4L2_BUFFER_POOL (pool);
   GstVideoCodecFrame *frame;
   GstBuffer *buffer = NULL;
   GstFlowReturn ret;
 
   GST_LOG_OBJECT (encoder, "Allocate output buffer");
 
-  buffer = gst_video_encoder_allocate_output_buffer (encoder,
-      self->v4l2capture->info.size);
-
-  if (NULL == buffer) {
-    ret = GST_FLOW_FLUSHING;
+  ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
+  if (ret != GST_FLOW_OK) {
+    if (cpool)
+      gst_object_unref (cpool);
     goto beach;
   }
 
   /* FIXME Check if buffer isn't the last one here */
 
   GST_LOG_OBJECT (encoder, "Process output buffer");
-  {
-    GstV4l2BufferPool *cpool =
-        GST_V4L2_BUFFER_POOL (gst_v4l2_object_get_buffer_pool
-        (self->v4l2capture));
-    ret = gst_v4l2_buffer_pool_process (cpool, &buffer, NULL);
-    if (cpool)
-      gst_object_unref (cpool);
-  }
+  ret = gst_v4l2_buffer_pool_process (cpool, &buffer, NULL);
+  if (cpool)
+    gst_object_unref (cpool);
   if (ret != GST_FLOW_OK)
     goto beach;