From a9b0809f352f3d063f3dc679984823ab8dce2574 Mon Sep 17 00:00:00 2001
From: Jeegar Patel <jeegarp@xilinx.com>
Date: Tue, 17 Jan 2017 17:19:21 +0530
Subject: [PATCH 11/24] Add DMA import support in gst-omx encoder

- Use OMX_UseBuffer to import fd from upstream
  element.

Signed-off-by: Jeegar Patel <jeegarp@xilinx.com>
Upstream Status: Pending
---
 omx/gstomxvideoenc.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)

diff --git a/omx/gstomxvideoenc.c b/omx/gstomxvideoenc.c
index 516c201..b9281e1 100644
--- a/omx/gstomxvideoenc.c
+++ b/omx/gstomxvideoenc.c
@@ -29,6 +29,7 @@
 #include "gstomxvideo.h"
 #include "gstomxvideoenc.h"
 
+#define ENABLE_DMA
 GST_DEBUG_CATEGORY_STATIC (gst_omx_video_enc_debug_category);
 #define GST_CAT_DEFAULT gst_omx_video_enc_debug_category
 
@@ -221,9 +222,14 @@ gst_omx_video_enc_open (GstVideoEncoder * encoder)
   OMX_VIDEO_PARAM_ENABLEBOARD enable_board;
   OMX_VIDEO_PARAM_ENABLEDMABUFFER enable_dmabuf;
   OMX_VIDEO_PARAM_ENABLEMCU enable_mcu;
+#ifdef ENABLE_DMA
+  static int use_dmabuf = 1, use_mcu = 1, use_board = 0;
+#else
   static int use_dmabuf = 0, use_mcu = 1, use_board = 0;
 #endif
 
+#endif
+
   self->enc =
       gst_omx_component_new (GST_OBJECT_CAST (self), klass->cdata.core_name,
       klass->cdata.component_name, klass->cdata.component_role,
@@ -279,6 +285,7 @@ gst_omx_video_enc_open (GstVideoEncoder * encoder)
 
   memset (&enable_dmabuf, 0, sizeof (enable_dmabuf));
   enable_dmabuf.bEnable = (OMX_BOOL) use_dmabuf;
+  enable_dmabuf.nPortIndex = 0;
   OMX_SetParameter (self->enc->handle, DMAtype, &enable_dmabuf);
 
   OMX_GetExtensionIndex (self->enc->handle,
@@ -1204,22 +1211,50 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
       /* if (gst_omx_port_allocate_buffers (self->enc_in_port) != OMX_ErrorNone)
          return FALSE; */
 
+#ifdef ENABLE_DMA
+      GstOMXBuffer *buf;
+      buf = g_slice_new0 (GstOMXBuffer);
+      buf->port = self->enc_in_port;
+      buf->used = FALSE;
+      buf->settings_cookie = self->enc_in_port->settings_cookie;
+
+      printf ("\n Before dummy alloc\n");
+      OMX_AllocateBuffer (self->enc_in_port->comp->handle, &buf->omx_buf,
+          self->enc_in_port->index, buf,
+          self->enc_in_port->port_def.nBufferSize);
+      printf ("\n After dummy alloc\n");
+#else
       mem = gst_buffer_new_allocate (NULL, 1024, NULL);
       gst_buffer_map (mem, &map_info, GST_MAP_READ);
+#endif
 
       gst_omx_port_update_port_definition (self->enc_in_port, NULL);
 
+#ifdef ENABLE_DMA
+      for (i = 0; i < self->enc_in_port->port_def.nBufferCountActual; i++)
+        buffer_list = g_list_append (buffer_list, buf->omx_buf->pBuffer);
+
+      OMX_FreeBuffer (self->enc_in_port->comp->handle, self->enc_in_port->index,
+          buf->omx_buf);
+#else
       for (i = 0; i < self->enc_in_port->port_def.nBufferCountActual; i++)
         buffer_list = g_list_append (buffer_list, map_info.data);
+#endif
 
+
+      printf ("\n Before OMX_UseBuffer \n");
       if (gst_omx_port_use_buffers (self->enc_in_port,
               buffer_list) != OMX_ErrorNone)
         return FALSE;
+      printf ("\n After OMX_UseBuffer\n");
 
+#ifdef ENABLE_DMA
+      g_list_free (buffer_list);
+#else
       gst_buffer_unmap (mem, &map_info);
       gst_buffer_unref (mem);
       g_list_free (buffer_list);
-
+#endif
       if (gst_omx_port_allocate_buffers (self->enc_out_port) != OMX_ErrorNone)
         return FALSE;
     }
@@ -1261,6 +1296,7 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
   return TRUE;
 }
 
+
 static gboolean
 gst_omx_video_enc_flush (GstVideoEncoder * encoder)
 {
@@ -1307,14 +1343,36 @@ gst_omx_video_enc_fill_buffer (GstOMXVideoEnc * self, GstBuffer * inbuf,
   OMX_PARAM_PORTDEFINITIONTYPE *port_def = &self->enc_in_port->port_def;
   gboolean ret = FALSE;
   GstVideoFrame frame;
+#ifdef ENABLE_DMA
+  gint i;
+  gsize offset = 0, maxsize = 0, size = 0;
+#endif
 
   if (info->width != port_def->format.video.nFrameWidth ||
       info->height != port_def->format.video.nFrameHeight) {
     GST_ERROR_OBJECT (self, "Width or height do not match");
     goto done;
   }
+#ifdef ENABLE_DMA
+  if (gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0))) {
+    printf ("\nIts a DMA fd having Total memory block %d with Total size %d\n",
+        gst_buffer_n_memory (inbuf), gst_buffer_get_size (inbuf));
+    for (i = 0; i < gst_buffer_n_memory (inbuf); i++) {
+      offset = 0;
+      maxsize = 0;
+      size = gst_memory_get_sizes (gst_buffer_peek_memory (inbuf, i), &offset,
+          &maxsize);
+      printf ("Fd is %d, size is %d, ofset is %d and maxsize is %d \n",
+          gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (inbuf, i)), size,
+          offset, maxsize);
+    }
+  } else {
+    printf ("\nIts NOT DMA fd \n");
+
+  }
+#endif
 
-  /* Same strides and everything */
+  /*Same strides and everything */
   if (gst_buffer_get_size (inbuf) ==
       outbuf->omx_buf->nAllocLen - outbuf->omx_buf->nOffset) {
     outbuf->omx_buf->nFilledLen = gst_buffer_get_size (inbuf);
@@ -1326,15 +1384,32 @@ gst_omx_video_enc_fill_buffer (GstOMXVideoEnc * self, GstBuffer * inbuf,
        outbuf->omx_buf->pBuffer + outbuf->omx_buf->nOffset,
        outbuf->omx_buf->nFilledLen); */
 
+#ifdef ENABLE_DMA
+    outbuf->omx_buf->pBuffer =
+        gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (inbuf, 0));
+    printf ("Stride matches & Passed fd to OMX is %d\n",
+        outbuf->omx_buf->pBuffer);
+#else
     GstMapInfo map = GST_MAP_INFO_INIT;
     if (!gst_buffer_map (inbuf, &map, GST_MAP_READ)) {
       GST_ERROR_OBJECT (self, "Failed to map input buffer");
     }
     outbuf->omx_buf->pBuffer = map.data;
     gst_buffer_unmap (inbuf, &map);
-
+#endif
+    ret = TRUE;
+    goto done;
+  } else {
+#ifdef ENABLE_DMA
+    outbuf->omx_buf->pBuffer =
+        gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (inbuf, 0));
+    printf ("Stride does not mached,Passed fd to OMX is %d\n",
+        outbuf->omx_buf->pBuffer);
     ret = TRUE;
     goto done;
+#else
+    printf ("Enable stride=true in pipeline or this should not happen\n");
+#endif
   }
 
   /* Different strides */
-- 
2.7.4

