From 1d3a5d0679f3a95669f30afe54aade7ec1b5187e Mon Sep 17 00:00:00 2001
From: Jeegar Patel <jeegarp@xilinx.com>
Date: Thu, 19 Jan 2017 19:26:01 +0530
Subject: [PATCH 12/24] encoder : Update OMX statehandling

Put OMX component in executing state
only after getting all the file descriptor
from upstream element for DMA mode.

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

diff --git a/omx/gstomxvideoenc.c b/omx/gstomxvideoenc.c
index b9281e1..b33a480 100644
--- a/omx/gstomxvideoenc.c
+++ b/omx/gstomxvideoenc.c
@@ -989,11 +989,12 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
   OMX_PARAM_PORTDEFINITIONTYPE port_def;
   GstVideoInfo *info = &state->info;
   GList *negotiation_map = NULL, *l;
+#ifndef ENABLE_DMA
   GList *buffer_list = NULL;
   GstMapInfo map_info;
   GstBuffer *mem = NULL;
   gint i;
-
+#endif
 
   self = GST_OMX_VIDEO_ENC (encoder);
   klass = GST_OMX_VIDEO_ENC_GET_CLASS (encoder);
@@ -1211,46 +1212,19 @@ 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
+#ifndef ENABLE_DMA
       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);
@@ -1263,6 +1237,7 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
             GST_CLOCK_TIME_NONE) != OMX_StateIdle)
       return FALSE;
 
+#ifndef ENABLE_DMA
     if (gst_omx_component_set_state (self->enc,
             OMX_StateExecuting) != OMX_ErrorNone)
       return FALSE;
@@ -1270,6 +1245,7 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
     if (gst_omx_component_get_state (self->enc,
             GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
       return FALSE;
+#endif
   }
 
   /* Unset flushing to allow ports to accept data again */
@@ -1287,12 +1263,13 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
     gst_video_codec_state_unref (self->input_state);
   self->input_state = gst_video_codec_state_ref (state);
 
+#ifndef ENABLE_DMA
   /* Start the srcpad loop again */
   GST_DEBUG_OBJECT (self, "Starting task again");
   self->downstream_flow_ret = GST_FLOW_OK;
   gst_pad_start_task (GST_VIDEO_ENCODER_SRC_PAD (self),
       (GstTaskFunction) gst_omx_video_enc_loop, encoder, NULL);
-
+#endif
   return TRUE;
 }
 
@@ -1343,34 +1320,12 @@ 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 */
   if (gst_buffer_get_size (inbuf) ==
@@ -1403,12 +1358,12 @@ gst_omx_video_enc_fill_buffer (GstOMXVideoEnc * self, GstBuffer * inbuf,
 #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",
+    printf ("Stride does not matched,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");
+    printf ("Enable stride=true in pipeline or This should not happen\n");
 #endif
   }
 
@@ -1559,7 +1514,11 @@ gst_omx_video_enc_handle_frame (GstVideoEncoder * encoder,
   GstOMXPort *port;
   GstOMXBuffer *buf;
   OMX_ERRORTYPE err;
-
+#ifdef ENABLE_DMA
+  GList *buffer_list = NULL;
+  static gint count = 0;
+  static gint fd1 = 0, fd2 = 0;
+#endif
   self = GST_OMX_VIDEO_ENC (encoder);
 
   GST_DEBUG_OBJECT (self, "Handling frame");
@@ -1577,6 +1536,53 @@ gst_omx_video_enc_handle_frame (GstVideoEncoder * encoder,
 
   port = self->enc_in_port;
 
+
+#ifdef ENABLE_DMA
+  count++;
+  if (count == 1) {
+    fd1 =
+        gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (frame->input_buffer,
+            0));
+    printf ("Reveived fd1 is %d\n", fd1);
+    gst_video_codec_frame_unref (frame);
+    return self->downstream_flow_ret;
+  }
+
+  if (count == 2) {
+    fd2 =
+        gst_dmabuf_memory_get_fd (gst_buffer_peek_memory (frame->input_buffer,
+            0));
+    printf ("Reveived fd2 is %d\n", fd2);
+
+    gst_omx_port_update_port_definition (self->enc_in_port, NULL);
+
+    //TODO: Currently we know, V4l2src bufferpool has 2 fds, But we need to make it dynamic */
+    buffer_list = g_list_append (buffer_list, fd1);
+    buffer_list = g_list_append (buffer_list, fd2);
+
+    if (gst_omx_port_use_buffers (self->enc_in_port,
+            buffer_list) != OMX_ErrorNone)
+      return FALSE;
+
+    g_list_free (buffer_list);
+
+    if (gst_omx_component_set_state (self->enc,
+            OMX_StateExecuting) != OMX_ErrorNone)
+      return FALSE;
+
+    if (gst_omx_component_get_state (self->enc,
+            GST_CLOCK_TIME_NONE) != OMX_StateExecuting)
+      return FALSE;
+
+    /* Start the srcpad loop again */
+    GST_DEBUG_OBJECT (self, "Starting task again");
+    self->downstream_flow_ret = GST_FLOW_OK;
+    gst_pad_start_task (GST_VIDEO_ENCODER_SRC_PAD (self),
+        (GstTaskFunction) gst_omx_video_enc_loop, self, NULL);
+  }
+#endif
+
+
   while (acq_ret != GST_OMX_ACQUIRE_BUFFER_OK) {
     GstClockTime timestamp, duration;
 
-- 
2.7.4

