]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugin-ducati.git/blobdiff - src/gstducatividdec.c
ducatividdec: detect unsupported codecs when switching from NULL to READY
[glsdk/gst-plugin-ducati.git] / src / gstducatividdec.c
index 9f17a137c9ce65c8065e37e81488e4db8fb01b46..a82e4110f061872277ad24c8b1664026a58b36c6 100644 (file)
@@ -22,6 +22,7 @@
 #endif
 
 #include "gstducatividdec.h"
+#include "gstducatibufferpriv.h"
 
 GST_BOILERPLATE (GstDucatiVidDec, gst_ducati_viddec, GstElement,
     GST_TYPE_ELEMENT);
@@ -82,20 +83,34 @@ engine_close (GstDucatiVidDec * self)
     dce_free (self->outArgs);
     self->outArgs = NULL;
   }
+
+  if (self->device) {
+    dce_deinit (self->device);
+    self->device = NULL;
+  }
 }
 
 static gboolean
 engine_open (GstDucatiVidDec * self)
 {
   gboolean ret;
+  int ec;
 
   if (G_UNLIKELY (self->engine)) {
     return TRUE;
   }
 
+  if (self->device == NULL) {
+    self->device = dce_init ();
+    if (self->device == NULL) {
+      GST_ERROR_OBJECT (self, "dce_init() failed");
+      return FALSE;
+    }
+  }
+
   GST_DEBUG_OBJECT (self, "opening engine");
 
-  self->engine = Engine_open ((String) "ivahd_vidsvr", NULL, NULL);
+  self->engine = Engine_open ((String) "ivahd_vidsvr", NULL, &ec);
   if (G_UNLIKELY (!self->engine)) {
     GST_ERROR_OBJECT (self, "could not create engine");
     return FALSE;
@@ -113,7 +128,7 @@ static void
 codec_delete (GstDucatiVidDec * self)
 {
   if (self->pool) {
-    gst_ducati_bufferpool_destroy (self->pool);
+    gst_drm_buffer_pool_destroy (self->pool);
     self->pool = NULL;
   }
 
@@ -122,9 +137,9 @@ codec_delete (GstDucatiVidDec * self)
     self->codec = NULL;
   }
 
-  if (self->input) {
-    MemMgr_Free (self->input);
-    self->input = NULL;
+  if (self->input_bo) {
+    omap_bo_del (self->input_bo);
+    self->input_bo = NULL;
   }
 }
 
@@ -168,100 +183,87 @@ codec_create (GstDucatiVidDec * self)
   self->first_out_buffer = TRUE;
 
   /* allocate input buffer and initialize inBufs: */
+  self->input_bo = omap_bo_new (self->device,
+      self->width * self->height, OMAP_BO_WC);
+  self->input = omap_bo_map (self->input_bo);
   self->inBufs->numBufs = 1;
-  self->input = gst_ducati_alloc_1d (self->width * self->height);
-  self->inBufs->descs[0].buf = (XDAS_Int8 *) TilerMem_VirtToPhys (self->input);
-  self->inBufs->descs[0].memType = XDM_MEMTYPE_RAW;
+  self->inBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (self->input_bo);
 
   return TRUE;
 }
 
 static inline GstBuffer *
-codec_bufferpool_get (GstDucatiVidDec * self, GstBuffer * buf)
+codec_buffer_pool_get (GstDucatiVidDec * self, GstBuffer * buf)
 {
   if (G_UNLIKELY (!self->pool)) {
-    guint size;
-
-    size = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
+    guint size = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
         self->padded_width, self->padded_height);
+
     GST_DEBUG_OBJECT (self, "creating bufferpool");
-    self->pool = gst_ducati_bufferpool_new (GST_ELEMENT (self),
-        GST_PAD_CAPS (self->srcpad), size);
+    self->pool = gst_drm_buffer_pool_new (GST_ELEMENT (self),
+        dce_get_fd (), GST_PAD_CAPS (self->srcpad), size);
   }
-  return GST_BUFFER (gst_ducati_bufferpool_get (self->pool, buf));
+  return GST_BUFFER (gst_drm_buffer_pool_get (self->pool, FALSE));
+}
+
+static GstDucatiBufferPriv *
+get_buffer_priv (GstDucatiVidDec * self, GstBuffer * buf)
+{
+  GstDucatiBufferPriv *priv = gst_ducati_buffer_priv_get (buf);
+  if (!priv) {
+    GstVideoFormat format = GST_VIDEO_FORMAT_NV12;
+    GstDmaBuf *dmabuf = gst_buffer_get_dma_buf (buf);
+
+    /* if it isn't a dmabuf buffer that we can import, then there
+     * is nothing we can do with it:
+     */
+    if (!dmabuf) {
+      GST_DEBUG_OBJECT (self, "not importing non dmabuf buffer");
+      return NULL;
+    }
+
+    priv = gst_ducati_buffer_priv_new ();
+
+    priv->bo = omap_bo_from_dmabuf (self->device,
+        gst_dma_buf_get_fd (dmabuf));
+
+    priv->uv_offset = gst_video_format_get_component_offset (format,
+        1, self->stride, self->padded_height);
+    priv->size = gst_video_format_get_size (format,
+        self->stride, self->padded_height);
+
+    gst_ducati_buffer_priv_set (buf, priv);
+  }
+  return priv;
 }
 
 static XDAS_Int32
 codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer ** buf,
     gboolean force_internal)
 {
-  XDAS_Int16 y_type, uv_type;
-  guint8 *y_vaddr, *uv_vaddr;
-  SSPtr y_paddr, uv_paddr;
+  GstDucatiBufferPriv *priv = NULL;
+
+  if (!force_internal)
+    priv = get_buffer_priv (self, *buf);
 
-  if (force_internal) {
+  if (!priv) {
     GstBuffer *orig = *buf;
 
     GST_DEBUG_OBJECT (self, "internal bufferpool forced");
-    *buf = codec_bufferpool_get (self, NULL);
+    *buf = codec_buffer_pool_get (self, NULL);
     GST_BUFFER_TIMESTAMP (*buf) = GST_BUFFER_TIMESTAMP (orig);
     GST_BUFFER_DURATION (*buf) = GST_BUFFER_DURATION (orig);
     gst_buffer_unref (orig);
     return codec_prepare_outbuf (self, buf, FALSE);
   }
 
-  y_vaddr = GST_BUFFER_DATA (*buf);
-  uv_vaddr = y_vaddr + self->stride * self->padded_height;
-
-  y_paddr = TilerMem_VirtToPhys (y_vaddr);
-  uv_paddr = TilerMem_VirtToPhys (uv_vaddr);
-
-  y_type = gst_ducati_get_mem_type (y_paddr);
-  uv_type = gst_ducati_get_mem_type (uv_paddr);
-  /* FIXME: workaround for the vc1 codec expecting _RAW when it's actually
-   * _TILEDPAGE... should be removed once the codec is fixed  */
-  if (y_type == XDM_MEMTYPE_TILEDPAGE && self->pageMemType != y_type)
-    y_type = self->pageMemType;
-  if (uv_type == XDM_MEMTYPE_TILEDPAGE && self->pageMemType != uv_type)
-    uv_type = self->pageMemType;
-
-  if (y_type < 0 || uv_type < 0) {
-    GST_DEBUG_OBJECT (self, "non TILER buffer, fallback to bufferpool");
-    *buf = codec_bufferpool_get (self, *buf);
-    return codec_prepare_outbuf (self, buf, FALSE);
-  }
-
-  if (!self->outBufs->numBufs) {
-    /* initialize output buffer type */
-    self->outBufs->numBufs = 2;
-    self->outBufs->descs[0].memType = y_type;
-    self->outBufs->descs[1].memType = uv_type;
-    if (y_type == XDM_MEMTYPE_RAW || y_type == XDM_MEMTYPE_TILEDPAGE) {
-      self->outBufs->descs[0].bufSize.bytes =
-          self->stride * self->padded_height;
-      self->outBufs->descs[1].bufSize.bytes =
-          self->stride * self->padded_height / 2;
-    } else {
-      self->outBufs->descs[0].bufSize.tileMem.width = self->padded_width;
-      self->outBufs->descs[0].bufSize.tileMem.height = self->padded_height;
-      /* note that UV interleaved width is same a Y: */
-      self->outBufs->descs[1].bufSize.tileMem.width = self->padded_width;
-      self->outBufs->descs[1].bufSize.tileMem.height = self->padded_height / 2;
-    }
-  } else {
-    /* verify output buffer type matches what we've already given
-     * to the codec
-     */
-    if ((self->outBufs->descs[0].memType != y_type) ||
-        (self->outBufs->descs[1].memType != uv_type)) {
-      GST_DEBUG_OBJECT (self, "buffer mismatch, fallback to bufferpool");
-      *buf = codec_bufferpool_get (self, *buf);
-      return codec_prepare_outbuf (self, buf, FALSE);
-    }
-  }
-
-  self->outBufs->descs[0].buf = (XDAS_Int8 *) y_paddr;
-  self->outBufs->descs[1].buf = (XDAS_Int8 *) uv_paddr;
+  self->outBufs->numBufs = 2;
+  self->outBufs->descs[0].memType = XDM_MEMTYPE_BO;
+  self->outBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (priv->bo);
+  self->outBufs->descs[0].bufSize.bytes = priv->uv_offset;
+  self->outBufs->descs[1].memType = XDM_MEMTYPE_BO_OFFSET;
+  self->outBufs->descs[1].buf = (XDAS_Int8 *) priv->uv_offset;
+  self->outBufs->descs[1].bufSize.bytes = priv->size - priv->uv_offset;
 
   return (XDAS_Int32) * buf;    // XXX use lookup table
 }
@@ -287,13 +289,18 @@ codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
 }
 
 static gint
-codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
+codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush,
+    GstFlowReturn * flow_ret)
 {
   gint err;
   GstClockTime t;
   GstBuffer *outbuf = NULL;
   gint i;
   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
+  GstFlowReturn ret = GST_FLOW_OK;
+  if (flow_ret)
+    /* never leave flow_ret uninitialized */
+    *flow_ret = GST_FLOW_OK;
 
   memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
   memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
@@ -347,7 +354,7 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
       GST_INFO_OBJECT (self, "changing interlace field in caps");
       gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
           NULL);
-      gst_ducati_bufferpool_set_caps (self->pool, caps);
+      gst_drm_buffer_pool_set_caps (self->pool, caps);
       if (!gst_pad_set_caps (self->srcpad, caps)) {
         GST_ERROR_OBJECT (self,
             "downstream didn't want to change interlace mode");
@@ -385,11 +392,17 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
           gst_event_new_crop (r->topLeft.y, r->topLeft.x,
               crop_width, crop_height));
 
+      if (self->crop)
+        gst_video_crop_unref (self->crop);
+
+      self->crop = gst_video_crop_new (r->topLeft.y, r->topLeft.x,
+          crop_width, crop_height);
+
       self->send_crop_event = FALSE;
     }
 
     if (G_UNLIKELY (self->first_out_buffer) && send) {
-      GstDucatiBufferPool *pool;
+      GstDRMBufferPool *pool;
       self->first_out_buffer = FALSE;
 
       /* Destroy the pool so the buffers we used so far are eventually released.
@@ -397,16 +410,12 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
        */
       pool = self->pool;
       self->pool = NULL;
-      gst_ducati_bufferpool_destroy (pool);
+      gst_drm_buffer_pool_destroy (pool);
     }
 
     if (send) {
       GstClockTime ts;
 
-      if (GST_IS_DUCATIBUFFER (outbuf)) {
-        outbuf = gst_ducati_buffer_get (GST_DUCATIBUFFER (outbuf));
-      }
-
       ts = GST_BUFFER_TIMESTAMP (outbuf);
 
       GST_DEBUG_OBJECT (self, "got buffer: %d %p (%" GST_TIME_FORMAT ")",
@@ -445,7 +454,17 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
         gst_buffer_set_caps (outbuf, GST_PAD_CAPS (self->srcpad));
       }
 
-      gst_pad_push (self->srcpad, outbuf);
+      if (self->crop)
+        gst_buffer_set_video_crop (outbuf, self->crop);
+
+      ret = gst_pad_push (self->srcpad, outbuf);
+      if (flow_ret)
+        *flow_ret = ret;
+      if (ret != GST_FLOW_OK) {
+        GST_WARNING_OBJECT (self, "push failed %s", gst_flow_get_name (ret));
+        /* just unref the remaining buffers (if any) */
+        send = FALSE;
+      }
     } else {
       GST_DEBUG_OBJECT (self, "free buffer: %d %p", i, outbuf);
       gst_buffer_unref (outbuf);
@@ -498,7 +517,7 @@ gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos)
   self->inArgs->inputID = 0;
 
   do {
-    err = codec_process (self, eos, TRUE);
+    err = codec_process (self, eos, TRUE, NULL);
   } while (err != XDM_EFAIL);
 
   /* reset outArgs in case we're flushing in codec_process trying to do error
@@ -902,7 +921,7 @@ gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
 {
   GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
-  GstFlowReturn ret;
+  GstFlowReturn ret = GST_FLOW_OK;
   Int32 err;
   GstBuffer *outbuf = NULL;
   GstCaps *outcaps = NULL;
@@ -932,7 +951,8 @@ allocate_buffer:
   ret = gst_pad_alloc_buffer (self->srcpad, 0, self->outsize,
       GST_PAD_CAPS (self->srcpad), &outbuf);
   if (ret != GST_FLOW_OK) {
-    GST_ERROR_OBJECT (self, "alloc_buffer failed %s", gst_flow_get_name (ret));
+    GST_WARNING_OBJECT (self, "alloc_buffer failed %s",
+        gst_flow_get_name (ret));
     return ret;
   }
 
@@ -1005,13 +1025,19 @@ have_out_buf:
 
   self->inArgs->numBytes = self->in_size;
   self->inBufs->descs[0].bufSize.bytes = self->in_size;
+  self->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
 
-  err = codec_process (self, TRUE, FALSE);
+  err = codec_process (self, TRUE, FALSE, &ret);
   if (err) {
     GST_ELEMENT_ERROR (self, STREAM, DECODE, (NULL),
         ("process returned error: %d %08x", err, self->outArgs->extendedError));
     return GST_FLOW_ERROR;
   }
+  if (ret != GST_FLOW_OK) {
+    GST_WARNING_OBJECT (self, "push from codec_process failed %s",
+        gst_flow_get_name (ret));
+    return ret;
+  }
 
   self->first_in_buffer = FALSE;
 
@@ -1125,6 +1151,7 @@ gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
 {
   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
   GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
+  gboolean supported;
 
   GST_DEBUG_OBJECT (self, "begin: changing state %s -> %s",
       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
@@ -1136,6 +1163,16 @@ gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
         GST_ERROR_OBJECT (self, "could not open");
         return GST_STATE_CHANGE_FAILURE;
       }
+      /* try to create/destroy the codec here, it may not be supported */
+      supported = codec_create (self);
+      codec_delete (self);
+      self->codec = NULL;
+      if (!supported) {
+        GST_ERROR_OBJECT (element, "Failed to create codec %s, not supported",
+            GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name);
+        engine_close (self);
+        return GST_STATE_CHANGE_FAILURE;
+      }
       break;
     default:
       break;
@@ -1176,13 +1213,11 @@ gst_ducati_viddec_get_property (GObject * obj,
 {
   GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
 
+
   switch (prop_id) {
     case PROP_VERSION:{
       int err;
-      char *version = gst_ducati_alloc_1d (VERSION_LENGTH);
-
-      /* in case something fails: */
-      snprintf (version, VERSION_LENGTH, "unsupported");
+      char *version = NULL;
 
       if (!self->engine)
         engine_open (self);
@@ -1191,7 +1226,8 @@ gst_ducati_viddec_get_property (GObject * obj,
         codec_create (self);
 
       if (self->codec) {
-        self->status->data.buf = (XDAS_Int8 *) TilerMem_VirtToPhys (version);
+        version = dce_alloc (VERSION_LENGTH);
+        self->status->data.buf = (XDAS_Int8 *) version;
         self->status->data.bufSize = VERSION_LENGTH;
 
         err = VIDDEC3_control (self->codec, XDM_GETVERSION,
@@ -1205,8 +1241,8 @@ gst_ducati_viddec_get_property (GObject * obj,
       }
 
       g_value_set_string (value, version);
-
-      MemMgr_Free (version);
+      if (version)
+        dce_free (version);
 
       break;
     }
@@ -1323,4 +1359,6 @@ gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
   self->wait_keyframe = TRUE;
 
   self->need_out_buf = TRUE;
+  self->device = NULL;
+  self->input_bo = NULL;
 }