]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugin-ducati.git/blobdiff - src/gstducatividdec.c
ducatividdec: only heed bytesConsumed when not in entire frame mode
[glsdk/gst-plugin-ducati.git] / src / gstducatividdec.c
index b4f54ba365e24048fad26b1842ced1909045aa7e..c3c8737dba0c4bc631210a38bb9acb6aa8377ccf 100644 (file)
@@ -1,3 +1,4 @@
+#define USE_DTS_PTS_CODE
 /*
  * GStreamer
  * Copyright (c) 2010, Texas Instruments Incorporated
@@ -22,6 +23,7 @@
 #endif
 
 #include "gstducatividdec.h"
+#include "gstducatibufferpriv.h"
 
 GST_BOILERPLATE (GstDucatiVidDec, gst_ducati_viddec, GstElement,
     GST_TYPE_ELEMENT);
@@ -82,20 +84,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 +129,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 +138,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,90 +184,90 @@ codec_create (GstDucatiVidDec * self)
   self->first_out_buffer = TRUE;
 
   /* allocate input buffer and initialize inBufs: */
+  /* FIXME:  needed size here has nothing to do with width * height */
+  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 XDAS_Int32
-codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer ** buf)
+static GstDucatiBufferPriv *
+get_buffer_priv (GstDucatiVidDec * self, GstBuffer * buf)
 {
-  XDAS_Int16 y_type, uv_type;
-  guint8 *y_vaddr, *uv_vaddr;
-  SSPtr y_paddr, uv_paddr;
-
-  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);
-  }
-
-  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
+  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 ((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);
+    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);
+    gst_mini_object_unref (GST_MINI_OBJECT (priv));
   }
+  return priv;
+}
 
-  self->outBufs->descs[0].buf = (XDAS_Int8 *) y_paddr;
-  self->outBufs->descs[1].buf = (XDAS_Int8 *) uv_paddr;
+static XDAS_Int32
+codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer ** buf,
+    gboolean force_internal)
+{
+  GstDucatiBufferPriv *priv = NULL;
+
+  if (!force_internal)
+    priv = get_buffer_priv (self, *buf);
 
-  return (XDAS_Int32) *buf;      // XXX use lookup table
+  if (!priv) {
+    GstBuffer *orig = *buf;
+
+    GST_DEBUG_OBJECT (self, "internal bufferpool forced");
+    *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);
+  }
+
+  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
 }
 
 static GstBuffer *
@@ -274,14 +290,24 @@ codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
   }
 }
 
+static void
+gst_ducati_viddec_on_flush (GstDucatiVidDec * self, gboolean eos)
+{
+}
+
 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));
@@ -294,12 +320,15 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
   if (err) {
     GST_WARNING_OBJECT (self, "err=%d, extendedError=%08x",
         err, self->outArgs->extendedError);
+    gst_ducati_log_extended_error_info (self->outArgs->extendedError);
 
     err = VIDDEC3_control (self->codec, XDM_GETSTATUS,
         self->dynParams, self->status);
-
-    GST_WARNING_OBJECT (self, "XDM_GETSTATUS: err=%d, extendedError=%08x",
-        err, self->status->extendedError);
+    if (err) {
+      GST_WARNING_OBJECT (self, "XDM_GETSTATUS: err=%d, extendedError=%08x",
+          err, self->status->extendedError);
+      gst_ducati_log_extended_error_info (self->status->extendedError);
+    }
 
     if (flush)
       err = XDM_EFAIL;
@@ -308,9 +337,53 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
           self->outArgs->extendedError, self->status->extendedError);
   }
 
-  for (i = 0; self->outArgs->outputID[i]; i++) {
-    if (G_UNLIKELY (self->first_out_buffer) && send) {
+  /* we now let the codec decide */
+  self->dynParams->newFrameFlag = XDAS_FALSE;
+
+  if (err == XDM_EFAIL)
+    goto skip_outbuf_processing;
+
+  for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->outputID[i]; i++) {
+    gboolean interlaced;
+
+    outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
+
+    interlaced =
+        self->outArgs->decodedBufs.contentType ==
+        IVIDEO_PROGRESSIVE ? FALSE : TRUE;
+
+    /* if send is FALSE, don't try to renegotiate as we could be flushing during
+     * a PAUSED->READY state change
+     */
+    if (send && interlaced != self->interlaced) {
+      GstCaps *caps;
+
+      GST_WARNING_OBJECT (self, "upstream set interlaced=%d but codec "
+          "thinks interlaced=%d... trusting codec", self->interlaced,
+          interlaced);
+
+      self->interlaced = interlaced;
+
+      caps =
+          gst_caps_make_writable (gst_pad_get_negotiated_caps (self->srcpad));
+      GST_INFO_OBJECT (self, "changing interlace field in caps");
+      gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
+          NULL);
+      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");
+        err = XDM_EFAIL;
+      }
+      gst_caps_unref (caps);
+
+      /* this buffer still has the old caps so we skip it */
+      send = FALSE;
+    }
+
+    if (G_UNLIKELY (self->send_crop_event) && send) {
       gint crop_width, crop_height;
+      GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
 
       /* send region of interest to sink on first buffer: */
       XDM_Rect *r = &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
@@ -323,6 +396,9 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
       if (crop_height > self->input_height)
         crop_height = self->input_height;
 
+      if (self->interlaced && !strcmp (klass->codec_name, "ivahd_mpeg2vdec"))
+        crop_height = crop_height / 2;
+
       GST_INFO_OBJECT (self, "active frame region %d, %d, %d, %d, crop %dx%d",
           r->topLeft.x, r->topLeft.y, r->bottomRight.x, r->bottomRight.y,
           crop_width, crop_height);
@@ -331,35 +407,89 @@ 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) {
+      GstDRMBufferPool *pool;
       self->first_out_buffer = FALSE;
+
+      /* Destroy the pool so the buffers we used so far are eventually released.
+       * The pool will be recreated if needed.
+       */
+      pool = self->pool;
+      self->pool = NULL;
+      gst_drm_buffer_pool_destroy (pool);
     }
 
-    outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
     if (send) {
-      if (GST_IS_DUCATIBUFFER (outbuf)) {
-        outbuf = gst_ducati_buffer_get (GST_DUCATIBUFFER (outbuf));
-      }
+      GstClockTime ts;
+
+      ts = GST_BUFFER_TIMESTAMP (outbuf);
+
       GST_DEBUG_OBJECT (self, "got buffer: %d %p (%" GST_TIME_FORMAT ")",
-          i, outbuf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
+          i, outbuf, GST_TIME_ARGS (ts));
+
+#ifdef USE_DTS_PTS_CODE
+      if (self->ts_may_be_pts) {
+        if ((self->last_pts != GST_CLOCK_TIME_NONE) && (self->last_pts > ts)) {
+          GST_DEBUG_OBJECT (self, "detected PTS going backwards, "
+              "enabling ts_is_pts");
+          self->ts_is_pts = TRUE;
+        }
+      }
+#endif
+
+      self->last_pts = ts;
+
+      if (self->dts_ridx != self->dts_widx) {
+        ts = self->dts_queue[self->dts_ridx++ % NDTS];
+      }
 
       if (self->ts_is_pts) {
         /* if we have a queued DTS from demuxer, use that instead: */
-        if (self->dts_ridx != self->dts_widx) {
-          GST_BUFFER_TIMESTAMP (outbuf) =
-              self->dts_queue[self->dts_ridx++ % NDTS];
-          GST_DEBUG_OBJECT (self, "fixed ts: %d %p (%" GST_TIME_FORMAT ")",
-              i, outbuf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)));
-        }
+        GST_BUFFER_TIMESTAMP (outbuf) = ts;
+        GST_DEBUG_OBJECT (self, "fixed ts: %d %p (%" GST_TIME_FORMAT ")",
+            i, outbuf, GST_TIME_ARGS (ts));
       }
 
-      gst_pad_push (self->srcpad, outbuf);
+      if (GST_BUFFER_CAPS (outbuf) &&
+          !gst_caps_is_equal (GST_BUFFER_CAPS (outbuf),
+              GST_PAD_CAPS (self->srcpad))) {
+        /* this looks a bit scary but it's really just to change the interlace=
+         * field in caps when we start as !interlaced and the codec detects
+         * otherwise */
+        GST_WARNING_OBJECT (self, "overriding buffer caps to fix "
+            "interlace mismatch");
+        outbuf = gst_buffer_make_metadata_writable (outbuf);
+        gst_buffer_set_caps (outbuf, GST_PAD_CAPS (self->srcpad));
+      }
+
+      if (self->crop)
+        gst_buffer_set_video_crop (outbuf, self->crop);
+
+      ret = klass->push_output (self, 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);
     }
   }
 
-  for (i = 0; self->outArgs->freeBufID[i]; i++) {
+skip_outbuf_processing:
+  for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->freeBufID[i]; i++) {
     codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
   }
 
@@ -367,27 +497,38 @@ codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush)
 }
 
 /** call control(FLUSH), and then process() to pop out all buffers */
-static gboolean
-codec_flush (GstDucatiVidDec * self, gboolean eos)
+gboolean
+gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos)
 {
-  gint err;
+  gint err = FALSE;
 
   GST_DEBUG_OBJECT (self, "flush: eos=%d", eos);
 
+  GST_DUCATIVIDDEC_GET_CLASS (self)->on_flush (self, eos);
+
   /* note: flush is synchronized against _chain() to avoid calling
    * the codec from multiple threads
    */
   GST_PAD_STREAM_LOCK (self->sinkpad);
 
+#ifdef USE_DTS_PTS_CODE
   self->dts_ridx = self->dts_widx = 0;
+  self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
+  self->ts_may_be_pts = TRUE;
+  self->ts_is_pts = FALSE;
+#endif
+  self->wait_keyframe = TRUE;
+  self->in_size = 0;
+  self->needs_flushing = FALSE;
+  self->need_out_buf = TRUE;
 
   if (G_UNLIKELY (self->first_in_buffer)) {
-    return TRUE;
+    goto out;
   }
 
   if (G_UNLIKELY (!self->codec)) {
     GST_WARNING_OBJECT (self, "no codec");
-    return TRUE;
+    goto out;
   }
 
   err = VIDDEC3_control (self->codec, XDM_FLUSH, self->dynParams, self->status);
@@ -401,9 +542,16 @@ 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
+   * recovery */
+  memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
+  memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
+
+  self->dynParams->newFrameFlag = XDAS_TRUE;
+
   /* on a flush, it is normal (and not an error) for the last _process() call
    * to return an error..
    */
@@ -537,6 +685,12 @@ gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
   return NULL;
 }
 
+static GstFlowReturn
+gst_ducati_viddec_push_output (GstDucatiVidDec * self, GstBuffer * buf)
+{
+  return gst_pad_push (self->srcpad, buf);
+}
+
 static gint
 gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
     gint extended_error, gint status_extended_error)
@@ -552,16 +706,13 @@ gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
 /* GstElement vmethod implementations */
 
 static gboolean
-gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
+gst_ducati_viddec_set_sink_caps (GstDucatiVidDec * self, GstCaps * caps)
 {
   gboolean ret = TRUE;
-  GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
   GstStructure *s;
   GstCaps *outcaps = NULL;
   GstStructure *out_s;
-  gboolean interlaced = FALSE;
-  gint frn = 0, frd = 1;
   gint par_width, par_height;
   gboolean par_present;
 
@@ -575,10 +726,11 @@ gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
   /* update output/padded sizes */
   klass->update_buffer_size (self);
 
-  GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
-
-  gst_structure_get_fraction (s, "framerate", &frn, &frd);
-  gst_structure_get_boolean (s, "interlaced", &interlaced);
+  if (!gst_structure_get_fraction (s, "framerate", &self->fps_n, &self->fps_d)) {
+    self->fps_n = 0;
+    self->fps_d = 1;
+  }
+  gst_structure_get_boolean (s, "interlaced", &self->interlaced);
   par_present = gst_structure_get_fraction (s, "pixel-aspect-ratio",
       &par_width, &par_height);
 
@@ -604,12 +756,12 @@ gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
   gst_structure_set (out_s,
       "width", G_TYPE_INT, self->padded_width,
       "height", G_TYPE_INT, self->padded_height,
-      "framerate", GST_TYPE_FRACTION, frn, frd, NULL);
+      "framerate", GST_TYPE_FRACTION, self->fps_n, self->fps_d, NULL);
   if (par_present)
     gst_structure_set (out_s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
         par_width, par_height, NULL);
 
-  if (interlaced)
+  if (self->interlaced)
     gst_structure_set (out_s, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
 
   if (!strcmp (gst_structure_get_name (out_s), "video/x-raw-yuv-strided")) {
@@ -628,23 +780,91 @@ gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
   GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
       self->outsize, self->stride, outcaps);
 
+  if (!self->first_in_buffer) {
+    /* Caps changed mid stream. We flush the codec to unlock all the potentially
+     * locked buffers. This is needed for downstream sinks that provide a
+     * buffer pool and need to destroy all the outstanding buffers before they
+     * can negotiate new caps (hello v4l2sink).
+     */
+    gst_ducati_viddec_codec_flush (self, FALSE);
+  }
+
+  /* (re)send a crop event when caps change */
+  self->send_crop_event = TRUE;
+
   ret = gst_pad_set_caps (self->srcpad, outcaps);
 
+  GST_INFO_OBJECT (self, "set caps done %d, %" GST_PTR_FORMAT, ret, outcaps);
+
 out:
   if (outcaps)
     gst_caps_unref (outcaps);
-  gst_object_unref (self);
 
   return ret;
 }
 
 static gboolean
-gst_ducati_viddec_query (GstPad * pad, GstQuery * query)
+gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
 {
-  gboolean res = TRUE, forward = TRUE;
-  GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
+  gboolean ret = TRUE;
+  GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
+  GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
 
-  GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
+  GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
+
+  ret = klass->set_sink_caps (self, caps);
+
+  gst_object_unref (self);
+
+  return ret;
+}
+
+static GstCaps *
+gst_ducati_viddec_src_getcaps (GstPad * pad)
+{
+  GstCaps *caps = NULL;
+  GstCaps *outcaps = NULL;
+  int i;
+
+  caps = GST_PAD_CAPS (pad);
+  if (caps == NULL) {
+    outcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
+    return outcaps;
+  }
+
+  outcaps = gst_caps_new_empty ();
+
+  /* allow -rowstrided and regular yuv */
+  for (i = 0; i < gst_caps_get_size (caps); i++) {
+    GstStructure *structure;
+    GstStructure *modified_structure;
+    GValue value = { 0 };
+
+    structure = gst_caps_get_structure (caps, i);
+    gst_caps_append_structure (outcaps, gst_structure_copy (structure));
+    modified_structure = gst_structure_copy (structure);
+
+    if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
+      gst_structure_set_name (modified_structure, "video/x-raw-yuv-strided");
+      g_value_init (&value, GST_TYPE_INT_RANGE);
+      gst_value_set_int_range (&value, 0, G_MAXINT);
+      gst_structure_set_value (modified_structure, "rowstride", &value);
+      gst_caps_append_structure (outcaps, modified_structure);
+      g_value_unset (&value);
+    } else {
+      gst_structure_set_name (modified_structure, "video/x-raw-yuv");
+      gst_structure_remove_field (modified_structure, "rowstride");
+      gst_caps_append_structure (outcaps, modified_structure);
+    }
+  }
+  return outcaps;
+}
+
+static gboolean
+gst_ducati_viddec_query (GstDucatiVidDec * self, GstPad * pad,
+    GstQuery * query, gboolean * forward)
+{
+  gboolean res = TRUE;
 
   switch (GST_QUERY_TYPE (query)) {
     case GST_QUERY_BUFFERS:
@@ -655,13 +875,26 @@ gst_ducati_viddec_query (GstPad * pad, GstQuery * query)
           self->padded_width, self->padded_height);
       gst_query_set_buffers_dimensions (query,
           self->padded_width, self->padded_height);
-      forward = FALSE;
+      *forward = FALSE;
       break;
     default:
       break;
   }
 
-  if (forward)
+
+  return res;
+}
+
+static gboolean
+gst_ducati_viddec_src_query (GstPad * pad, GstQuery * query)
+{
+  gboolean res = TRUE, forward = TRUE;
+  GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
+  GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
+
+  GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
+  res = klass->query (self, pad, query, &forward);
+  if (res && forward)
     res = gst_pad_query_default (pad, query);
 
   return res;
@@ -671,8 +904,17 @@ static gboolean
 gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
 {
   GstClockTime timestamp, qostime;
+  GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
   gint64 diff;
-  gboolean is_keyframe;
+
+  if (self->wait_keyframe) {
+    if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
+      GST_INFO_OBJECT (self, "skipping until the next keyframe");
+      return FALSE;
+    }
+
+    self->wait_keyframe = FALSE;
+  }
 
   timestamp = GST_BUFFER_TIMESTAMP (buf);
   if (self->segment.format != GST_FORMAT_TIME ||
@@ -688,8 +930,6 @@ gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
     /* out of segment */
     goto no_qos;
 
-  is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
-
   /* see how our next timestamp relates to the latest qos timestamp. negative
    * values mean we are early, positive values mean we are too late. */
   diff = GST_CLOCK_DIFF (qostime, self->qos_earliest_time);
@@ -699,19 +939,34 @@ gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
       GST_TIME_ARGS (qostime), GST_TIME_ARGS (self->qos_earliest_time), diff,
       self->qos_proportion);
 
-  if (diff >= 0 && !is_keyframe)
+  if (klass->can_drop_frame (self, buf, diff)) {
+    GST_INFO_OBJECT (self, "dropping frame");
     return FALSE;
+  }
 
 no_qos:
   return TRUE;
 }
 
+static gboolean
+gst_ducati_viddec_can_drop_frame (GstDucatiVidDec * self, GstBuffer * buf,
+    gint64 diff)
+{
+  gboolean is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf,
+      GST_BUFFER_FLAG_DELTA_UNIT);
+
+  if (diff >= 0 && !is_keyframe)
+    return TRUE;
+
+  return FALSE;
+}
+
 static GstFlowReturn
 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;
@@ -719,11 +974,12 @@ gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
 
   if (G_UNLIKELY (!self->engine)) {
     GST_ERROR_OBJECT (self, "no engine");
+    gst_buffer_unref (buf);
     return GST_FLOW_ERROR;
   }
 
-  GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes)",
-      GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf));
+  GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes, flags %d)",
+      GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
 
   decode = gst_ducati_viddec_do_qos (self, buf);
   if (!decode) {
@@ -741,7 +997,9 @@ 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));
+    gst_buffer_unref (buf);
     return ret;
   }
 
@@ -773,6 +1031,8 @@ allocate_buffer:
   if (G_UNLIKELY (!self->codec)) {
     if (!codec_create (self)) {
       GST_ERROR_OBJECT (self, "could not create codec");
+      gst_buffer_unref (buf);
+      gst_buffer_unref (outbuf);
       return GST_FLOW_ERROR;
     }
   }
@@ -780,23 +1040,35 @@ allocate_buffer:
   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
 
-  /* pass new output buffer as to the decoder to decode into: */
-  self->inArgs->inputID = codec_prepare_outbuf (self, &outbuf);
+  /* Pass new output buffer to the decoder to decode into. Use buffers from the
+   * internal pool while self->first_out_buffer == TRUE in order to simplify
+   * things in case we need to renegotiate */
+  self->inArgs->inputID =
+      codec_prepare_outbuf (self, &outbuf, self->first_out_buffer);
   if (!self->inArgs->inputID) {
     GST_ERROR_OBJECT (self, "could not prepare output buffer");
+    gst_buffer_unref (buf);
     return GST_FLOW_ERROR;
   }
 
 have_out_buf:
-  self->in_size = 0;
   buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
 
-  if (self->ts_is_pts && (ts != GST_CLOCK_TIME_NONE)) {
-    self->duration = GST_BUFFER_DURATION (outbuf);
+#ifdef USE_DTS_PTS_CODE
+  if (ts != GST_CLOCK_TIME_NONE) {
     self->dts_queue[self->dts_widx++ % NDTS] = ts;
+    /* if next buffer has earlier ts than previous, then the ts
+     * we are getting are definitely decode order (DTS):
+     */
+    if ((self->last_dts != GST_CLOCK_TIME_NONE) && (self->last_dts > ts)) {
+      GST_DEBUG_OBJECT (self, "input timestamp definitely DTS");
+      self->ts_may_be_pts = FALSE;
+    }
+    self->last_dts = ts;
   }
+#endif
 
-  if (self->in_size == 0) {
+  if (self->in_size == 0 && outbuf) {
     GST_DEBUG_OBJECT (self, "no input, skipping process");
     gst_buffer_unref (outbuf);
     return GST_FLOW_OK;
@@ -804,16 +1076,46 @@ 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));
+    gst_ducati_log_extended_error_info (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;
 
+  if (self->params->inputDataMode != IVIDEO_ENTIREFRAME) {
+    /* The copy could be avoided by playing with the buffer pointer,
+       but it seems to be rare and for not many bytes */
+    GST_DEBUG_OBJECT (self, "Consumed %d/%d (%d) bytes, %d left",
+        self->outArgs->bytesConsumed, self->in_size,
+        self->inArgs->numBytes,
+        self->in_size - self->outArgs->bytesConsumed);
+    if (self->outArgs->bytesConsumed > 0) {
+      if (self->outArgs->bytesConsumed > self->in_size) {
+        GST_WARNING_OBJECT (self, "Codec claims to have used more bytes than supplied");
+        self->in_size = 0;
+      } else {
+        if (self->outArgs->bytesConsumed < self->in_size) {
+          memmove (self->input, self->input + self->outArgs->bytesConsumed,
+              self->in_size - self->outArgs->bytesConsumed);
+        }
+        self->in_size -= self->outArgs->bytesConsumed;
+      }
+    }
+  } else {
+    self->in_size = 0;
+  }
+
   if (self->outArgs->outBufsInUseFlag) {
     GST_DEBUG_OBJECT (self, "outBufsInUseFlag set");
     self->need_out_buf = FALSE;
@@ -828,6 +1130,9 @@ have_out_buf:
     goto allocate_buffer;
   }
 
+  if (self->needs_flushing)
+    gst_ducati_viddec_codec_flush (self, FALSE);
+
   return GST_FLOW_OK;
 }
 
@@ -854,14 +1159,15 @@ gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
       break;
     }
     case GST_EVENT_EOS:
-      if (!codec_flush (self, TRUE)) {
+      if (!gst_ducati_viddec_codec_flush (self, TRUE)) {
         GST_ERROR_OBJECT (self, "could not flush on eos");
         ret = FALSE;
       }
       break;
     case GST_EVENT_FLUSH_STOP:
-      if (!codec_flush (self, FALSE)) {
+      if (!gst_ducati_viddec_codec_flush (self, FALSE)) {
         GST_ERROR_OBJECT (self, "could not flush");
+        gst_event_unref (event);
         ret = FALSE;
       }
       gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
@@ -924,6 +1230,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)),
@@ -935,6 +1242,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;
@@ -946,6 +1263,11 @@ gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
     goto leave;
 
   switch (transition) {
+    case GST_STATE_CHANGE_PAUSED_TO_READY:
+      self->interlaced = FALSE;
+      self->send_crop_event = TRUE;
+      gst_ducati_viddec_codec_flush (self, FALSE);
+      break;
     case GST_STATE_CHANGE_READY_TO_NULL:
       codec_delete (self);
       engine_close (self);
@@ -970,13 +1292,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);
@@ -985,7 +1305,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,
@@ -999,8 +1320,8 @@ gst_ducati_viddec_get_property (GObject * obj,
       }
 
       g_value_set_string (value, version);
-
-      MemMgr_Free (version);
+      if (version)
+        dce_free (version);
 
       break;
     }
@@ -1053,6 +1374,11 @@ gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
       GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
   klass->push_input = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
   klass->handle_error = GST_DEBUG_FUNCPTR (gst_ducati_viddec_handle_error);
+  klass->can_drop_frame = GST_DEBUG_FUNCPTR (gst_ducati_viddec_can_drop_frame);
+  klass->query = GST_DEBUG_FUNCPTR (gst_ducati_viddec_query);
+  klass->push_output = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_output);
+  klass->on_flush = GST_DEBUG_FUNCPTR (gst_ducati_viddec_on_flush);
+  klass->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_sink_caps);
 
   g_object_class_install_property (gobject_class, PROP_VERSION,
       g_param_spec_string ("version", "Version",
@@ -1079,7 +1405,9 @@ gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
   gst_pad_set_event_function (self->srcpad,
       GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_event));
   gst_pad_set_query_function (self->srcpad,
-      GST_DEBUG_FUNCPTR (gst_ducati_viddec_query));
+      GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_query));
+  gst_pad_set_getcaps_function (self->srcpad,
+      GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_getcaps));
 
   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
@@ -1091,9 +1419,20 @@ gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
    */
   self->width = 128;
   self->height = 128;
+  self->fps_n = -1;
+  self->fps_d = -1;
 
   self->first_in_buffer = TRUE;
   self->first_out_buffer = TRUE;
+  self->interlaced = FALSE;
+  self->send_crop_event = TRUE;
+
+#ifdef USE_DTS_PTS_CODE
+  self->dts_ridx = self->dts_widx = 0;
+  self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
+  self->ts_may_be_pts = TRUE;
+  self->ts_is_pts = FALSE;
+#endif
 
   self->pageMemType = XDM_MEMTYPE_TILEDPAGE;
 
@@ -1101,6 +1440,9 @@ gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
 
   self->qos_proportion = 1;
   self->qos_earliest_time = GST_CLOCK_TIME_NONE;
+  self->wait_keyframe = TRUE;
 
   self->need_out_buf = TRUE;
+  self->device = NULL;
+  self->input_bo = NULL;
 }