]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugin-ducati.git/blob - src/gstducatividdec.c
ducatividdec: remove another leftover -strided caps remnant
[glsdk/gst-plugin-ducati.git] / src / gstducatividdec.c
1 #define USE_DTS_PTS_CODE
2 /*
3  * GStreamer
4  * Copyright (c) 2010, Texas Instruments Incorporated
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
25 #include "gstducatividdec.h"
26 #include "gstducatibufferpriv.h"
28 GST_BOILERPLATE (GstDucatiVidDec, gst_ducati_viddec, GstElement,
29     GST_TYPE_ELEMENT);
31 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
32     GST_PAD_SRC,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("NV12"))
35     );
37 enum
38 {
39   PROP_0,
40   PROP_VERSION,
41   PROP_MAX_REORDER_FRAMES,
42 };
44 /* helper functions */
46 static void
47 engine_close (GstDucatiVidDec * self)
48 {
49   if (self->engine) {
50     Engine_close (self->engine);
51     self->engine = NULL;
52   }
54   if (self->params) {
55     dce_free (self->params);
56     self->params = NULL;
57   }
59   if (self->dynParams) {
60     dce_free (self->dynParams);
61     self->dynParams = NULL;
62   }
64   if (self->status) {
65     dce_free (self->status);
66     self->status = NULL;
67   }
69   if (self->inBufs) {
70     dce_free (self->inBufs);
71     self->inBufs = NULL;
72   }
74   if (self->outBufs) {
75     dce_free (self->outBufs);
76     self->outBufs = NULL;
77   }
79   if (self->inArgs) {
80     dce_free (self->inArgs);
81     self->inArgs = NULL;
82   }
84   if (self->outArgs) {
85     dce_free (self->outArgs);
86     self->outArgs = NULL;
87   }
89   if (self->device) {
90     dce_deinit (self->device);
91     self->device = NULL;
92   }
93 }
95 static gboolean
96 engine_open (GstDucatiVidDec * self)
97 {
98   gboolean ret;
99   int ec;
101   if (G_UNLIKELY (self->engine)) {
102     return TRUE;
103   }
105   if (self->device == NULL) {
106     self->device = dce_init ();
107     if (self->device == NULL) {
108       GST_ERROR_OBJECT (self, "dce_init() failed");
109       return FALSE;
110     }
111   }
113   GST_DEBUG_OBJECT (self, "opening engine");
115   self->engine = Engine_open ((String) "ivahd_vidsvr", NULL, &ec);
116   if (G_UNLIKELY (!self->engine)) {
117     GST_ERROR_OBJECT (self, "could not create engine");
118     return FALSE;
119   }
121   ret = GST_DUCATIVIDDEC_GET_CLASS (self)->allocate_params (self,
122       sizeof (IVIDDEC3_Params), sizeof (IVIDDEC3_DynamicParams),
123       sizeof (IVIDDEC3_Status), sizeof (IVIDDEC3_InArgs),
124       sizeof (IVIDDEC3_OutArgs));
126   return ret;
129 static void
130 codec_delete (GstDucatiVidDec * self)
132   if (self->pool) {
133     gst_drm_buffer_pool_destroy (self->pool);
134     self->pool = NULL;
135   }
137   if (self->codec) {
138     VIDDEC3_delete (self->codec);
139     self->codec = NULL;
140   }
142   if (self->input_bo) {
143     omap_bo_del (self->input_bo);
144     self->input_bo = NULL;
145   }
148 static gboolean
149 codec_create (GstDucatiVidDec * self)
151   gint err;
152   const gchar *codec_name;
154   codec_delete (self);
156   if (G_UNLIKELY (!self->engine)) {
157     GST_ERROR_OBJECT (self, "no engine");
158     return FALSE;
159   }
161   /* these need to be set before VIDDEC3_create */
162   self->params->maxWidth = self->width;
163   self->params->maxHeight = self->height;
165   codec_name = GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name;
167   /* create codec: */
168   GST_DEBUG_OBJECT (self, "creating codec: %s", codec_name);
169   self->codec =
170       VIDDEC3_create (self->engine, (String) codec_name, self->params);
172   if (!self->codec) {
173     return FALSE;
174   }
176   err =
177       VIDDEC3_control (self->codec, XDM_SETPARAMS, self->dynParams,
178       self->status);
179   if (err) {
180     GST_ERROR_OBJECT (self, "failed XDM_SETPARAMS");
181     return FALSE;
182   }
184   self->first_in_buffer = TRUE;
185   self->first_out_buffer = TRUE;
187   /* allocate input buffer and initialize inBufs: */
188   /* FIXME:  needed size here has nothing to do with width * height */
189   self->input_bo = omap_bo_new (self->device,
190       self->width * self->height, OMAP_BO_WC);
191   self->input = omap_bo_map (self->input_bo);
192   self->inBufs->numBufs = 1;
193   self->inBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (self->input_bo);
195   return TRUE;
198 static inline GstBuffer *
199 codec_buffer_pool_get (GstDucatiVidDec * self, GstBuffer * buf)
201   if (G_UNLIKELY (!self->pool)) {
202     guint size = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
203         self->padded_width, self->padded_height);
205     GST_DEBUG_OBJECT (self, "creating bufferpool");
206     self->pool = gst_drm_buffer_pool_new (GST_ELEMENT (self),
207         dce_get_fd (), GST_PAD_CAPS (self->srcpad), size);
208   }
209   return GST_BUFFER (gst_drm_buffer_pool_get (self->pool, FALSE));
212 static GstDucatiBufferPriv *
213 get_buffer_priv (GstDucatiVidDec * self, GstBuffer * buf)
215   GstDucatiBufferPriv *priv = gst_ducati_buffer_priv_get (buf);
216   if (!priv) {
217     GstVideoFormat format = GST_VIDEO_FORMAT_NV12;
218     GstDmaBuf *dmabuf = gst_buffer_get_dma_buf (buf);
220     /* if it isn't a dmabuf buffer that we can import, then there
221      * is nothing we can do with it:
222      */
223     if (!dmabuf) {
224       GST_DEBUG_OBJECT (self, "not importing non dmabuf buffer");
225       return NULL;
226     }
228     priv = gst_ducati_buffer_priv_new ();
230     priv->bo = omap_bo_from_dmabuf (self->device, gst_dma_buf_get_fd (dmabuf));
232     priv->uv_offset = gst_video_format_get_component_offset (format,
233         1, self->stride, self->padded_height);
234     priv->size = gst_video_format_get_size (format,
235         self->stride, self->padded_height);
237     gst_ducati_buffer_priv_set (buf, priv);
238     gst_mini_object_unref (GST_MINI_OBJECT (priv));
239   }
240   return priv;
243 static XDAS_Int32
244 codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer ** buf,
245     gboolean force_internal)
247   GstDucatiBufferPriv *priv = NULL;
249   if (!force_internal)
250     priv = get_buffer_priv (self, *buf);
252   if (!priv) {
253     GstBuffer *orig = *buf;
255     GST_DEBUG_OBJECT (self, "internal bufferpool forced");
256     *buf = codec_buffer_pool_get (self, NULL);
257     GST_BUFFER_TIMESTAMP (*buf) = GST_BUFFER_TIMESTAMP (orig);
258     GST_BUFFER_DURATION (*buf) = GST_BUFFER_DURATION (orig);
259     gst_buffer_unref (orig);
260     return codec_prepare_outbuf (self, buf, FALSE);
261   }
263   self->outBufs->numBufs = 2;
264   self->outBufs->descs[0].memType = XDM_MEMTYPE_BO;
265   self->outBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (priv->bo);
266   self->outBufs->descs[0].bufSize.bytes = priv->uv_offset;
267   self->outBufs->descs[1].memType = XDM_MEMTYPE_BO_OFFSET;
268   self->outBufs->descs[1].buf = (XDAS_Int8 *) priv->uv_offset;
269   self->outBufs->descs[1].bufSize.bytes = priv->size - priv->uv_offset;
271   return (XDAS_Int32) * buf;
274 static GstBuffer *
275 codec_get_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
277   GstBuffer *buf = (GstBuffer *) id;
279   if (buf) {
280     g_hash_table_insert (self->passed_in_bufs, buf, buf);
282     gst_buffer_ref (buf);
283   }
284   return buf;
287 static void
288 codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
290   GstBuffer *buf = (GstBuffer *) id;
292   if (buf) {
293     GST_DEBUG_OBJECT (self, "free buffer: %d %p", id, buf);
294     g_hash_table_remove (self->passed_in_bufs, buf);
295   }
298 static GstFlowReturn
299 gst_ducati_viddec_push_earliest (GstDucatiVidDec * self)
301   guint64 earliest_order = G_MAXUINT64;
302   guint earliest_index = 0, i;
303   GstBuffer *buf;
305   if (self->backlog_nframes == 0)
306     return GST_FLOW_OK;
308   /* work out which frame has the earliest poc */
309   for (i = 0; i < self->backlog_nframes; i++) {
310     guint64 order = GST_BUFFER_OFFSET_END (self->backlog_frames[i]);
311     if (earliest_order == G_MAXUINT64 || order < earliest_order) {
312       earliest_order = order;
313       earliest_index = i;
314     }
315   }
317   /* send it, giving away the ref */
318   buf = self->backlog_frames[earliest_index];
319   self->backlog_frames[earliest_index] =
320       self->backlog_frames[--self->backlog_nframes];
321   GST_DEBUG_OBJECT (self, "Actually pushing backlog buffer %" GST_PTR_FORMAT,
322       buf);
323   return gst_pad_push (self->srcpad, buf);
326 static void
327 gst_ducati_viddec_on_flush (GstDucatiVidDec * self, gboolean eos)
329   /* push everything on the backlog, ignoring errors */
330   while (self->backlog_nframes > 0) {
331     gst_ducati_viddec_push_earliest (self);
332   }
335 static gint
336 codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush,
337     GstFlowReturn * flow_ret)
339   gint err;
340   GstClockTime t;
341   GstBuffer *outbuf = NULL;
342   gint i;
343   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
344   GstFlowReturn ret = GST_FLOW_OK;
345   if (flow_ret)
346     /* never leave flow_ret uninitialized */
347     *flow_ret = GST_FLOW_OK;
349   memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
350   memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
352   GST_DEBUG ("Calling VIDDEC3_process");
353   t = gst_util_get_timestamp ();
354   err = VIDDEC3_process (self->codec,
355       self->inBufs, self->outBufs, self->inArgs, self->outArgs);
356   t = gst_util_get_timestamp () - t;
357   GST_DEBUG_OBJECT (self, "VIDDEC3_process took %10dns (%d ms)", (gint) t,
358       (gint) (t / 1000000));
360   if (err) {
361     GST_WARNING_OBJECT (self, "err=%d, extendedError=%08x",
362         err, self->outArgs->extendedError);
363     gst_ducati_log_extended_error_info (self->outArgs->extendedError);
365     err = VIDDEC3_control (self->codec, XDM_GETSTATUS,
366         self->dynParams, self->status);
367     if (err) {
368       GST_WARNING_OBJECT (self, "XDM_GETSTATUS: err=%d, extendedError=%08x",
369           err, self->status->extendedError);
370       gst_ducati_log_extended_error_info (self->status->extendedError);
371     }
373     if (flush)
374       err = XDM_EFAIL;
375     else
376       err = klass->handle_error (self, err,
377           self->outArgs->extendedError, self->status->extendedError);
378   }
380   /* we now let the codec decide */
381   self->dynParams->newFrameFlag = XDAS_FALSE;
383   if (err == XDM_EFAIL)
384     goto skip_outbuf_processing;
386   for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->outputID[i]; i++) {
387     gboolean interlaced;
389     /* Getting an extra reference for the decoder */
390     outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
391     interlaced =
392         self->outArgs->decodedBufs.contentType ==
393         IVIDEO_PROGRESSIVE ? FALSE : TRUE;
395     /* if send is FALSE, don't try to renegotiate as we could be flushing during
396      * a PAUSED->READY state change
397      */
398     if (send && interlaced != self->interlaced) {
399       GstCaps *caps;
401       GST_WARNING_OBJECT (self, "upstream set interlaced=%d but codec "
402           "thinks interlaced=%d... trusting codec", self->interlaced,
403           interlaced);
405       self->interlaced = interlaced;
407       caps =
408           gst_caps_make_writable (gst_pad_get_negotiated_caps (self->srcpad));
409       GST_INFO_OBJECT (self, "changing interlace field in caps");
410       gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
411           NULL);
412       gst_drm_buffer_pool_set_caps (self->pool, caps);
413       if (!gst_pad_set_caps (self->srcpad, caps)) {
414         GST_ERROR_OBJECT (self,
415             "downstream didn't want to change interlace mode");
416         err = XDM_EFAIL;
417       }
418       gst_caps_unref (caps);
420       /* this buffer still has the old caps so we skip it */
421       send = FALSE;
422     }
424     if (G_UNLIKELY (self->send_crop_event) && send) {
425       gint crop_width, crop_height;
427       /* send region of interest to sink on first buffer: */
428       XDM_Rect *r = &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
430       crop_width = r->bottomRight.x - r->topLeft.x;
431       crop_height = r->bottomRight.y - r->topLeft.y;
433       if (crop_width > self->input_width)
434         crop_width = self->input_width;
435       if (crop_height > self->input_height)
436         crop_height = self->input_height;
438       GST_INFO_OBJECT (self, "active frame region %d, %d, %d, %d, crop %dx%d",
439           r->topLeft.x, r->topLeft.y, r->bottomRight.x, r->bottomRight.y,
440           crop_width, crop_height);
442       gst_pad_push_event (self->srcpad,
443           gst_event_new_crop (r->topLeft.y, r->topLeft.x,
444               crop_width, crop_height));
446       if (self->crop)
447         gst_video_crop_unref (self->crop);
449       self->crop = gst_video_crop_new (r->topLeft.y, r->topLeft.x,
450           crop_width, crop_height);
452       self->send_crop_event = FALSE;
453     }
455     if (G_UNLIKELY (self->first_out_buffer) && send) {
456       GstDRMBufferPool *pool;
457       self->first_out_buffer = FALSE;
459       /* Destroy the pool so the buffers we used so far are eventually released.
460        * The pool will be recreated if needed.
461        */
462       pool = self->pool;
463       self->pool = NULL;
464       gst_drm_buffer_pool_destroy (pool);
465     }
467     if (send) {
468       GstClockTime ts;
470       ts = GST_BUFFER_TIMESTAMP (outbuf);
472       GST_DEBUG_OBJECT (self, "got buffer: %d %p (%" GST_TIME_FORMAT ")",
473           i, outbuf, GST_TIME_ARGS (ts));
475 #ifdef USE_DTS_PTS_CODE
476       if (self->ts_may_be_pts) {
477         if ((self->last_pts != GST_CLOCK_TIME_NONE) && (self->last_pts > ts)) {
478           GST_DEBUG_OBJECT (self, "detected PTS going backwards, "
479               "enabling ts_is_pts");
480           self->ts_is_pts = TRUE;
481         }
482       }
483 #endif
485       self->last_pts = ts;
487       if (self->dts_ridx != self->dts_widx) {
488         ts = self->dts_queue[self->dts_ridx++ % NDTS];
489       }
491       if (self->ts_is_pts) {
492         /* if we have a queued DTS from demuxer, use that instead: */
493         GST_BUFFER_TIMESTAMP (outbuf) = ts;
494         GST_DEBUG_OBJECT (self, "fixed ts: %d %p (%" GST_TIME_FORMAT ")",
495             i, outbuf, GST_TIME_ARGS (ts));
496       }
498       if (GST_BUFFER_CAPS (outbuf) &&
499           !gst_caps_is_equal (GST_BUFFER_CAPS (outbuf),
500               GST_PAD_CAPS (self->srcpad))) {
501         /* this looks a bit scary but it's really just to change the interlace=
502          * field in caps when we start as !interlaced and the codec detects
503          * otherwise */
504         GST_WARNING_OBJECT (self, "overriding buffer caps to fix "
505             "interlace mismatch");
506         outbuf = gst_buffer_make_metadata_writable (outbuf);
507         gst_buffer_set_caps (outbuf, GST_PAD_CAPS (self->srcpad));
508       }
510       if (self->crop)
511         gst_buffer_set_video_crop (outbuf, self->crop);
513       ret = klass->push_output (self, outbuf);
514       if (flow_ret)
515         *flow_ret = ret;
516       if (ret != GST_FLOW_OK) {
517         GST_WARNING_OBJECT (self, "push failed %s", gst_flow_get_name (ret));
518         /* just unref the remaining buffers (if any) */
519         send = FALSE;
520       }
521     } else {
522       GST_DEBUG_OBJECT (self, "Buffer not pushed, dropping 'chain' ref: %d %p",
523           i, outbuf);
525       gst_buffer_unref (outbuf);
526     }
527   }
529 skip_outbuf_processing:
530   for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->freeBufID[i]; i++) {
531     codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
532   }
534   return err;
537 /** call control(FLUSH), and then process() to pop out all buffers */
538 gboolean
539 gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos)
541   gint err = FALSE;
543   GST_DEBUG_OBJECT (self, "flush: eos=%d", eos);
545   GST_DUCATIVIDDEC_GET_CLASS (self)->on_flush (self, eos);
547   /* note: flush is synchronized against _chain() to avoid calling
548    * the codec from multiple threads
549    */
550   GST_PAD_STREAM_LOCK (self->sinkpad);
552 #ifdef USE_DTS_PTS_CODE
553   self->dts_ridx = self->dts_widx = 0;
554   self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
555   self->ts_may_be_pts = TRUE;
556   self->ts_is_pts = FALSE;
557 #endif
558   self->wait_keyframe = TRUE;
559   self->in_size = 0;
560   self->needs_flushing = FALSE;
561   self->need_out_buf = TRUE;
563   if (G_UNLIKELY (self->first_in_buffer)) {
564     goto out;
565   }
567   if (G_UNLIKELY (!self->codec)) {
568     GST_WARNING_OBJECT (self, "no codec");
569     goto out;
570   }
572   err = VIDDEC3_control (self->codec, XDM_FLUSH, self->dynParams, self->status);
573   if (err) {
574     GST_ERROR_OBJECT (self, "failed XDM_FLUSH");
575     goto out;
576   }
578   self->inBufs->descs[0].bufSize.bytes = 0;
579   self->inBufs->numBufs = 0;
580   self->inArgs->numBytes = 0;
581   self->inArgs->inputID = 0;
582   self->outBufs->numBufs = 0;
584   do {
585     err = codec_process (self, eos, TRUE, NULL);
586   } while (err != XDM_EFAIL);
588   /* We flushed the decoder, we can now remove the buffer that have never been
589    * unrefed in it */
590   g_hash_table_remove_all (self->passed_in_bufs);
592   /* reset outArgs in case we're flushing in codec_process trying to do error
593    * recovery */
594   memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
595   memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
597   self->dynParams->newFrameFlag = XDAS_TRUE;
599   /* Reset the push buffer and YUV buffers */
600   self->inBufs->numBufs = 1;
601   self->outBufs->numBufs = 2;
603   /* on a flush, it is normal (and not an error) for the last _process() call
604    * to return an error..
605    */
606   err = XDM_EOK;
608 out:
609   GST_PAD_STREAM_UNLOCK (self->sinkpad);
610   GST_DEBUG_OBJECT (self, "done");
612   return !err;
615 /* GstDucatiVidDec vmethod default implementations */
617 static gboolean
618 gst_ducati_viddec_parse_caps (GstDucatiVidDec * self, GstStructure * s)
620   const GValue *codec_data;
621   gint w, h;
623   if (gst_structure_get_int (s, "width", &self->input_width) &&
624       gst_structure_get_int (s, "height", &self->input_height)) {
626     h = ALIGN2 (self->input_height, 4); /* round up to MB */
627     w = ALIGN2 (self->input_width, 4);  /* round up to MB */
629     /* if we've already created codec, but the resolution has changed, we
630      * need to re-create the codec:
631      */
632     if (G_UNLIKELY (self->codec)) {
633       if ((h != self->height) || (w != self->width)) {
634         codec_delete (self);
635       }
636     }
638     self->width = w;
639     self->height = h;
641     codec_data = gst_structure_get_value (s, "codec_data");
643     if (codec_data) {
644       GstBuffer *buffer = gst_value_get_buffer (codec_data);
645       GST_DEBUG_OBJECT (self, "codec_data: %" GST_PTR_FORMAT, buffer);
646       self->codec_data = gst_buffer_ref (buffer);
647     }
649     return TRUE;
650   }
652   return FALSE;
655 static gboolean
656 gst_ducati_viddec_allocate_params (GstDucatiVidDec * self, gint params_sz,
657     gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz)
660   /* allocate params: */
661   self->params = dce_alloc (params_sz);
662   if (G_UNLIKELY (!self->params)) {
663     return FALSE;
664   }
665   self->params->size = params_sz;
666   self->params->maxFrameRate = 30000;
667   self->params->maxBitRate = 10000000;
669   self->params->dataEndianness = XDM_BYTE;
670   self->params->forceChromaFormat = XDM_YUV_420SP;
671   self->params->operatingMode = IVIDEO_DECODE_ONLY;
673   self->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
674   self->params->inputDataMode = IVIDEO_ENTIREFRAME;
675   self->params->outputDataMode = IVIDEO_ENTIREFRAME;
676   self->params->numInputDataUnits = 0;
677   self->params->numOutputDataUnits = 0;
679   self->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
680   self->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
681   self->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
682   self->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
684   /* allocate dynParams: */
685   self->dynParams = dce_alloc (dynparams_sz);
686   if (G_UNLIKELY (!self->dynParams)) {
687     return FALSE;
688   }
689   self->dynParams->size = dynparams_sz;
690   self->dynParams->decodeHeader = XDM_DECODE_AU;
691   self->dynParams->displayWidth = 0;
692   self->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
693   self->dynParams->newFrameFlag = XDAS_TRUE;
695   /* allocate status: */
696   self->status = dce_alloc (status_sz);
697   if (G_UNLIKELY (!self->status)) {
698     return FALSE;
699   }
700   memset (self->status, 0, status_sz);
701   self->status->size = status_sz;
703   /* allocate inBufs/outBufs: */
704   self->inBufs = dce_alloc (sizeof (XDM2_BufDesc));
705   self->outBufs = dce_alloc (sizeof (XDM2_BufDesc));
706   if (G_UNLIKELY (!self->inBufs) || G_UNLIKELY (!self->outBufs)) {
707     return FALSE;
708   }
710   /* allocate inArgs/outArgs: */
711   self->inArgs = dce_alloc (inargs_sz);
712   self->outArgs = dce_alloc (outargs_sz);
713   if (G_UNLIKELY (!self->inArgs) || G_UNLIKELY (!self->outArgs)) {
714     return FALSE;
715   }
716   self->inArgs->size = inargs_sz;
717   self->outArgs->size = outargs_sz;
719   return TRUE;
722 static GstBuffer *
723 gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
725   if (G_UNLIKELY (self->first_in_buffer) && self->codec_data) {
726     push_input (self, GST_BUFFER_DATA (self->codec_data),
727         GST_BUFFER_SIZE (self->codec_data));
728   }
730   /* just copy entire buffer */
731   push_input (self, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
732   gst_buffer_unref (buf);
734   return NULL;
737 static GstFlowReturn
738 gst_ducati_viddec_push_output (GstDucatiVidDec * self, GstBuffer * buf)
740   GstFlowReturn ret = GST_FLOW_OK;
742   /* if no reordering info was set, just send the buffer */
743   if (GST_BUFFER_OFFSET_END (buf) == GST_BUFFER_OFFSET_NONE) {
744     GST_DEBUG_OBJECT (self, "No reordering info on that buffer, sending now");
745     return gst_pad_push (self->srcpad, buf);
746   }
748   /* add the frame to the list, the array will own the ref */
749   GST_DEBUG_OBJECT (self, "Adding buffer %" GST_PTR_FORMAT " to backlog", buf);
750   self->backlog_frames[self->backlog_nframes++] = buf;
752   /* push till we have no more than the max needed, or error */
753   while (self->backlog_nframes > self->backlog_maxframes) {
754     ret = gst_ducati_viddec_push_earliest (self);
755     if (ret != GST_FLOW_OK)
756       break;
757   }
759   return ret;
762 static gint
763 gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
764     gint extended_error, gint status_extended_error)
766   if (XDM_ISFATALERROR (extended_error))
767     ret = XDM_EFAIL;
768   else
769     ret = XDM_EOK;
771   return ret;
774 /* GstElement vmethod implementations */
776 static gboolean
777 gst_ducati_viddec_set_sink_caps (GstDucatiVidDec * self, GstCaps * caps)
779   gboolean ret = TRUE;
780   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
781   GstStructure *s;
782   GstCaps *outcaps = NULL;
783   GstStructure *out_s;
784   gint par_width, par_height;
785   gboolean par_present;
787   s = gst_caps_get_structure (caps, 0);
788   if (!klass->parse_caps (self, s)) {
789     GST_WARNING_OBJECT (self, "missing required fields");
790     ret = FALSE;
791     goto out;
792   }
794   /* update output/padded sizes */
795   klass->update_buffer_size (self);
797   if (!gst_structure_get_fraction (s, "framerate", &self->fps_n, &self->fps_d)) {
798     self->fps_n = 0;
799     self->fps_d = 1;
800   }
801   gst_structure_get_boolean (s, "interlaced", &self->interlaced);
802   par_present = gst_structure_get_fraction (s, "pixel-aspect-ratio",
803       &par_width, &par_height);
805   outcaps = gst_pad_get_allowed_caps (self->srcpad);
806   if (outcaps) {
807     outcaps = gst_caps_make_writable (outcaps);
808     gst_caps_truncate (outcaps);
809     if (gst_caps_is_empty (outcaps)) {
810       gst_caps_unref (outcaps);
811       outcaps = NULL;
812     }
813   }
815   if (!outcaps) {
816     outcaps = gst_caps_new_simple ("video/x-raw-yuv",
817         "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N', 'V', '1', '2'), NULL);
818   }
820   out_s = gst_caps_get_structure (outcaps, 0);
821   gst_structure_set (out_s,
822       "width", G_TYPE_INT, self->padded_width,
823       "height", G_TYPE_INT, self->padded_height,
824       "framerate", GST_TYPE_FRACTION, self->fps_n, self->fps_d, NULL);
825   if (par_present)
826     gst_structure_set (out_s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
827         par_width, par_height, NULL);
829   if (self->interlaced)
830     gst_structure_set (out_s, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
832   self->stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_NV12,
833       0, self->padded_width);
835   self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
836       self->stride, self->padded_height);
838   GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
839       self->outsize, self->stride, outcaps);
841   if (!self->first_in_buffer) {
842     /* Caps changed mid stream. We flush the codec to unlock all the potentially
843      * locked buffers. This is needed for downstream sinks that provide a
844      * buffer pool and need to destroy all the outstanding buffers before they
845      * can negotiate new caps (hello v4l2sink).
846      */
847     gst_ducati_viddec_codec_flush (self, FALSE);
848   }
850   /* (re)send a crop event when caps change */
851   self->send_crop_event = TRUE;
853   ret = gst_pad_set_caps (self->srcpad, outcaps);
855   GST_INFO_OBJECT (self, "set caps done %d, %" GST_PTR_FORMAT, ret, outcaps);
857   /* default to no reordering */
858   self->backlog_maxframes = 0;
860 out:
861   if (outcaps)
862     gst_caps_unref (outcaps);
864   return ret;
867 static gboolean
868 gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
870   gboolean ret = TRUE;
871   GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
872   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
874   GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
876   ret = klass->set_sink_caps (self, caps);
878   gst_object_unref (self);
880   return ret;
883 static GstCaps *
884 gst_ducati_viddec_src_getcaps (GstPad * pad)
886   GstCaps *caps = NULL;
888   caps = GST_PAD_CAPS (pad);
889   if (caps == NULL) {
890     return gst_caps_copy (gst_pad_get_pad_template_caps (pad));
891   } else {
892     return gst_caps_copy (caps);
893   }
896 static gboolean
897 gst_ducati_viddec_query (GstDucatiVidDec * self, GstPad * pad,
898     GstQuery * query, gboolean * forward)
900   gboolean res = TRUE;
902   switch (GST_QUERY_TYPE (query)) {
903     case GST_QUERY_BUFFERS:
904       GST_DEBUG_OBJECT (self, "min buffers: %d", self->min_buffers);
905       gst_query_set_buffers_count (query, self->min_buffers);
907       GST_DEBUG_OBJECT (self, "min dimensions: %dx%d",
908           self->padded_width, self->padded_height);
909       gst_query_set_buffers_dimensions (query,
910           self->padded_width, self->padded_height);
911       *forward = FALSE;
912       break;
913     default:
914       break;
915   }
918   return res;
921 static gboolean
922 gst_ducati_viddec_src_query (GstPad * pad, GstQuery * query)
924   gboolean res = TRUE, forward = TRUE;
925   GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
926   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
928   GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
929   res = klass->query (self, pad, query, &forward);
930   if (res && forward)
931     res = gst_pad_query_default (pad, query);
933   return res;
936 static gboolean
937 gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
939   GstClockTime timestamp, qostime;
940   GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
941   gint64 diff;
943   if (self->wait_keyframe) {
944     if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
945       GST_INFO_OBJECT (self, "skipping until the next keyframe");
946       return FALSE;
947     }
949     self->wait_keyframe = FALSE;
950   }
952   timestamp = GST_BUFFER_TIMESTAMP (buf);
953   if (self->segment.format != GST_FORMAT_TIME ||
954       self->qos_earliest_time == GST_CLOCK_TIME_NONE)
955     goto no_qos;
957   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)))
958     goto no_qos;
960   qostime = gst_segment_to_running_time (&self->segment,
961       GST_FORMAT_TIME, timestamp);
962   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime)))
963     /* out of segment */
964     goto no_qos;
966   /* see how our next timestamp relates to the latest qos timestamp. negative
967    * values mean we are early, positive values mean we are too late. */
968   diff = GST_CLOCK_DIFF (qostime, self->qos_earliest_time);
970   GST_DEBUG_OBJECT (self, "QOS: qostime %" GST_TIME_FORMAT
971       ", earliest %" GST_TIME_FORMAT " diff %" G_GINT64_FORMAT " proportion %f",
972       GST_TIME_ARGS (qostime), GST_TIME_ARGS (self->qos_earliest_time), diff,
973       self->qos_proportion);
975   if (klass->can_drop_frame (self, buf, diff)) {
976     GST_INFO_OBJECT (self, "dropping frame");
977     return FALSE;
978   }
980 no_qos:
981   return TRUE;
984 static gboolean
985 gst_ducati_viddec_can_drop_frame (GstDucatiVidDec * self, GstBuffer * buf,
986     gint64 diff)
988   gboolean is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf,
989       GST_BUFFER_FLAG_DELTA_UNIT);
991   if (diff >= 0 && !is_keyframe)
992     return TRUE;
994   return FALSE;
997 static GstFlowReturn
998 gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
1000   GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1001   GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
1002   GstFlowReturn ret = GST_FLOW_OK;
1003   Int32 err;
1004   GstBuffer *outbuf = NULL;
1005   GstCaps *outcaps = NULL;
1006   gboolean decode;
1008   if (G_UNLIKELY (!self->engine)) {
1009     GST_ERROR_OBJECT (self, "no engine");
1010     gst_buffer_unref (buf);
1011     return GST_FLOW_ERROR;
1012   }
1014   GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes, flags %d)",
1015       GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
1017   decode = gst_ducati_viddec_do_qos (self, buf);
1018   if (!decode) {
1019     gst_buffer_unref (buf);
1020     return GST_FLOW_OK;
1021   }
1023   if (!self->need_out_buf)
1024     goto have_out_buf;
1026   /* do this before creating codec to ensure reverse caps negotiation
1027    * happens first:
1028    */
1029 allocate_buffer:
1030   ret = gst_pad_alloc_buffer (self->srcpad, 0, self->outsize,
1031       GST_PAD_CAPS (self->srcpad), &outbuf);
1032   if (ret != GST_FLOW_OK) {
1033     GST_WARNING_OBJECT (self, "alloc_buffer failed %s",
1034         gst_flow_get_name (ret));
1035     gst_buffer_unref (buf);
1036     return ret;
1037   }
1039   outcaps = GST_BUFFER_CAPS (outbuf);
1040   if (outcaps && !gst_caps_is_equal (outcaps, GST_PAD_CAPS (self->srcpad))) {
1041     GstStructure *s;
1043     GST_INFO_OBJECT (self, "doing upstream negotiation bufsize %d",
1044         GST_BUFFER_SIZE (outbuf));
1046     s = gst_caps_get_structure (outcaps, 0);
1047     gst_structure_get_int (s, "rowstride", &self->stride);
1048     self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
1049         self->stride, self->padded_height);
1051     GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
1052         self->outsize, self->stride, outcaps);
1054     gst_pad_set_caps (self->srcpad, outcaps);
1056     if (GST_BUFFER_SIZE (outbuf) != self->outsize) {
1057       GST_INFO_OBJECT (self, "dropping buffer (bufsize %d != outsize %d)",
1058           GST_BUFFER_SIZE (outbuf), self->outsize);
1059       gst_buffer_unref (outbuf);
1060       goto allocate_buffer;
1061     }
1062   }
1064   if (G_UNLIKELY (!self->codec)) {
1065     if (!codec_create (self)) {
1066       GST_ERROR_OBJECT (self, "could not create codec");
1067       gst_buffer_unref (buf);
1068       gst_buffer_unref (outbuf);
1069       return GST_FLOW_ERROR;
1070     }
1071   }
1073   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
1074   GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
1076   /* Pass new output buffer to the decoder to decode into. Use buffers from the
1077    * internal pool while self->first_out_buffer == TRUE in order to simplify
1078    * things in case we need to renegotiate */
1079   self->inArgs->inputID =
1080       codec_prepare_outbuf (self, &outbuf, self->first_out_buffer);
1081   if (!self->inArgs->inputID) {
1082     GST_ERROR_OBJECT (self, "could not prepare output buffer");
1083     gst_buffer_unref (buf);
1084     return GST_FLOW_ERROR;
1085   }
1086   GST_BUFFER_OFFSET_END (outbuf) = GST_BUFFER_OFFSET_END (buf);
1088 have_out_buf:
1089   buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
1091 #ifdef USE_DTS_PTS_CODE
1092   if (ts != GST_CLOCK_TIME_NONE) {
1093     self->dts_queue[self->dts_widx++ % NDTS] = ts;
1094     /* if next buffer has earlier ts than previous, then the ts
1095      * we are getting are definitely decode order (DTS):
1096      */
1097     if ((self->last_dts != GST_CLOCK_TIME_NONE) && (self->last_dts > ts)) {
1098       GST_DEBUG_OBJECT (self, "input timestamp definitely DTS");
1099       self->ts_may_be_pts = FALSE;
1100     }
1101     self->last_dts = ts;
1102   }
1103 #endif
1105   if (self->in_size == 0 && outbuf) {
1106     GST_DEBUG_OBJECT (self, "no input, skipping process");
1108     gst_buffer_unref (outbuf);
1109     return GST_FLOW_OK;
1110   }
1112   self->inArgs->numBytes = self->in_size;
1113   self->inBufs->descs[0].bufSize.bytes = self->in_size;
1114   self->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
1116   err = codec_process (self, TRUE, FALSE, &ret);
1117   if (err) {
1118     GST_ELEMENT_ERROR (self, STREAM, DECODE, (NULL),
1119         ("process returned error: %d %08x", err, self->outArgs->extendedError));
1120     gst_ducati_log_extended_error_info (self->outArgs->extendedError);
1122     return GST_FLOW_ERROR;
1123   }
1125   if (ret != GST_FLOW_OK) {
1126     GST_WARNING_OBJECT (self, "push from codec_process failed %s",
1127         gst_flow_get_name (ret));
1129     return ret;
1130   }
1132   self->first_in_buffer = FALSE;
1134   if (self->params->inputDataMode != IVIDEO_ENTIREFRAME) {
1135     /* The copy could be avoided by playing with the buffer pointer,
1136        but it seems to be rare and for not many bytes */
1137     GST_DEBUG_OBJECT (self, "Consumed %d/%d (%d) bytes, %d left",
1138         self->outArgs->bytesConsumed, self->in_size,
1139         self->inArgs->numBytes, self->in_size - self->outArgs->bytesConsumed);
1140     if (self->outArgs->bytesConsumed > 0) {
1141       if (self->outArgs->bytesConsumed > self->in_size) {
1142         GST_WARNING_OBJECT (self,
1143             "Codec claims to have used more bytes than supplied");
1144         self->in_size = 0;
1145       } else {
1146         if (self->outArgs->bytesConsumed < self->in_size) {
1147           memmove (self->input, self->input + self->outArgs->bytesConsumed,
1148               self->in_size - self->outArgs->bytesConsumed);
1149         }
1150         self->in_size -= self->outArgs->bytesConsumed;
1151       }
1152     }
1153   } else {
1154     self->in_size = 0;
1155   }
1157   if (self->outArgs->outBufsInUseFlag) {
1158     GST_DEBUG_OBJECT (self, "outBufsInUseFlag set");
1159     self->need_out_buf = FALSE;
1160   } else {
1161     self->need_out_buf = TRUE;
1162   }
1164   if (buf) {
1165     GST_DEBUG_OBJECT (self, "found remaining data: %d bytes",
1166         GST_BUFFER_SIZE (buf));
1167     ts = GST_BUFFER_TIMESTAMP (buf);
1168     goto allocate_buffer;
1169   }
1171   if (self->needs_flushing)
1172     gst_ducati_viddec_codec_flush (self, FALSE);
1174   return GST_FLOW_OK;
1177 static gboolean
1178 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
1180   GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1181   gboolean ret = TRUE;
1183   GST_DEBUG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1185   switch (GST_EVENT_TYPE (event)) {
1186     case GST_EVENT_NEWSEGMENT:
1187     {
1188       gboolean update;
1189       GstFormat fmt;
1190       gint64 start, stop, time;
1191       gdouble rate, arate;
1193       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
1194           &start, &stop, &time);
1195       gst_segment_set_newsegment_full (&self->segment, update,
1196           rate, arate, fmt, start, stop, time);
1197       break;
1198     }
1199     case GST_EVENT_EOS:
1200       if (!gst_ducati_viddec_codec_flush (self, TRUE)) {
1201         GST_ERROR_OBJECT (self, "could not flush on eos");
1202         ret = FALSE;
1203       }
1204       break;
1205     case GST_EVENT_FLUSH_STOP:
1206       if (!gst_ducati_viddec_codec_flush (self, FALSE)) {
1207         GST_ERROR_OBJECT (self, "could not flush");
1208         gst_event_unref (event);
1209         ret = FALSE;
1210       }
1211       gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1212       self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1213       self->qos_proportion = 1;
1214       self->need_out_buf = TRUE;
1215       break;
1216     default:
1217       break;
1218   }
1220   if (ret)
1221     ret = gst_pad_push_event (self->srcpad, event);
1222   GST_LOG_OBJECT (self, "end");
1224   return ret;
1227 static gboolean
1228 gst_ducati_viddec_src_event (GstPad * pad, GstEvent * event)
1230   GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1231   gboolean ret = TRUE;
1233   GST_LOG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1235   switch (GST_EVENT_TYPE (event)) {
1236     case GST_EVENT_QOS:
1237     {
1238       gdouble proportion;
1239       GstClockTimeDiff diff;
1240       GstClockTime timestamp;
1242       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
1244       GST_OBJECT_LOCK (self);
1245       self->qos_proportion = proportion;
1246       self->qos_earliest_time = timestamp + 2 * diff;
1247       GST_OBJECT_UNLOCK (self);
1249       GST_DEBUG_OBJECT (self,
1250           "got QoS proportion %f %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1251           proportion, GST_TIME_ARGS (timestamp), diff);
1253       ret = gst_pad_push_event (self->sinkpad, event);
1254       break;
1255     }
1256     default:
1257       ret = gst_pad_push_event (self->sinkpad, event);
1258       break;
1259   }
1261   GST_LOG_OBJECT (self, "end");
1263   return ret;
1266 static GstStateChangeReturn
1267 gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
1269   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1270   GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
1271   gboolean supported;
1273   GST_DEBUG_OBJECT (self, "begin: changing state %s -> %s",
1274       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1275       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1277   switch (transition) {
1278     case GST_STATE_CHANGE_NULL_TO_READY:
1279       if (!engine_open (self)) {
1280         GST_ERROR_OBJECT (self, "could not open");
1281         return GST_STATE_CHANGE_FAILURE;
1282       }
1283       /* try to create/destroy the codec here, it may not be supported */
1284       supported = codec_create (self);
1285       codec_delete (self);
1286       self->codec = NULL;
1287       if (!supported) {
1288         GST_ERROR_OBJECT (element, "Failed to create codec %s, not supported",
1289             GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name);
1290         engine_close (self);
1291         return GST_STATE_CHANGE_FAILURE;
1292       }
1293       break;
1294     default:
1295       break;
1296   }
1298   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1300   if (ret == GST_STATE_CHANGE_FAILURE)
1301     goto leave;
1303   switch (transition) {
1304     case GST_STATE_CHANGE_PAUSED_TO_READY:
1305       self->interlaced = FALSE;
1306       self->send_crop_event = TRUE;
1307       gst_ducati_viddec_codec_flush (self, FALSE);
1308       break;
1309     case GST_STATE_CHANGE_READY_TO_NULL:
1310       codec_delete (self);
1311       engine_close (self);
1312       break;
1313     default:
1314       break;
1315   }
1317 leave:
1318   GST_LOG_OBJECT (self, "end");
1320   return ret;
1323 /* GObject vmethod implementations */
1325 #define VERSION_LENGTH 256
1327 static void
1328 gst_ducati_viddec_get_property (GObject * obj,
1329     guint prop_id, GValue * value, GParamSpec * pspec)
1331   GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1334   switch (prop_id) {
1335     case PROP_VERSION:{
1336       int err;
1337       char *version = NULL;
1339       if (!self->engine)
1340         engine_open (self);
1342       if (!self->codec)
1343         codec_create (self);
1345       if (self->codec) {
1346         version = dce_alloc (VERSION_LENGTH);
1347         self->status->data.buf = (XDAS_Int8 *) version;
1348         self->status->data.bufSize = VERSION_LENGTH;
1350         err = VIDDEC3_control (self->codec, XDM_GETVERSION,
1351             self->dynParams, self->status);
1352         if (err) {
1353           GST_ERROR_OBJECT (self, "failed XDM_GETVERSION");
1354         }
1356         self->status->data.buf = NULL;
1357         self->status->data.bufSize = 0;
1358       }
1360       g_value_set_string (value, version);
1361       if (version)
1362         dce_free (version);
1364       break;
1365     }
1366     case PROP_MAX_REORDER_FRAMES:
1367       g_value_set_int (value, self->backlog_max_maxframes);
1368       break;
1369     default:{
1370       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1371       break;
1372     }
1373   }
1376 static void
1377 gst_ducati_viddec_set_property (GObject * obj,
1378     guint prop_id, const GValue * value, GParamSpec * pspec)
1380   GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1382   switch (prop_id) {
1383     case PROP_MAX_REORDER_FRAMES:
1384       self->backlog_max_maxframes = g_value_get_int (value);
1385       break;
1386     default:{
1387       G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1388       break;
1389     }
1390   }
1393 static void
1394 gst_ducati_viddec_finalize (GObject * obj)
1396   GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1398   codec_delete (self);
1399   engine_close (self);
1401   /* Will unref the remaining buffers if needed */
1402   g_hash_table_unref (self->passed_in_bufs);
1403   if (self->codec_data) {
1404     gst_buffer_unref (self->codec_data);
1405     self->codec_data = NULL;
1406   }
1408   G_OBJECT_CLASS (parent_class)->finalize (obj);
1411 static void
1412 gst_ducati_viddec_base_init (gpointer gclass)
1414   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
1416   gst_element_class_add_pad_template (element_class,
1417       gst_static_pad_template_get (&src_factory));
1420 static void
1421 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
1423   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1424   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1426   gobject_class->get_property =
1427       GST_DEBUG_FUNCPTR (gst_ducati_viddec_get_property);
1428   gobject_class->set_property =
1429       GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_property);
1430   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ducati_viddec_finalize);
1431   gstelement_class->change_state =
1432       GST_DEBUG_FUNCPTR (gst_ducati_viddec_change_state);
1434   klass->parse_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_parse_caps);
1435   klass->allocate_params =
1436       GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
1437   klass->push_input = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
1438   klass->handle_error = GST_DEBUG_FUNCPTR (gst_ducati_viddec_handle_error);
1439   klass->can_drop_frame = GST_DEBUG_FUNCPTR (gst_ducati_viddec_can_drop_frame);
1440   klass->query = GST_DEBUG_FUNCPTR (gst_ducati_viddec_query);
1441   klass->push_output = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_output);
1442   klass->on_flush = GST_DEBUG_FUNCPTR (gst_ducati_viddec_on_flush);
1443   klass->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_sink_caps);
1445   g_object_class_install_property (gobject_class, PROP_VERSION,
1446       g_param_spec_string ("version", "Version",
1447           "The codec version string", "",
1448           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1450   g_object_class_install_property (gobject_class, PROP_MAX_REORDER_FRAMES,
1451       g_param_spec_int ("max-reorder-frames",
1452           "Maximum number of frames needed for reordering",
1453           "The maximum number of frames needed for reordering output frames. "
1454           "Only meaningful for codecs with B frames. 0 means no reordering. "
1455           "This value will be used if the correct value cannot be inferred "
1456           "from the stream. Too low a value may cause misordering, too high "
1457           "will cause extra latency.",
1458           0, MAX_BACKLOG_FRAMES, MAX_BACKLOG_FRAMES,
1459           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1462 static void
1463 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
1465   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1467   self->sinkpad =
1468       gst_pad_new_from_template (gst_element_class_get_pad_template
1469       (gstelement_class, "sink"), "sink");
1470   gst_pad_set_setcaps_function (self->sinkpad,
1471       GST_DEBUG_FUNCPTR (gst_ducati_viddec_sink_setcaps));
1472   gst_pad_set_chain_function (self->sinkpad,
1473       GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
1474   gst_pad_set_event_function (self->sinkpad,
1475       GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
1477   self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
1478   gst_pad_set_event_function (self->srcpad,
1479       GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_event));
1480   gst_pad_set_query_function (self->srcpad,
1481       GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_query));
1482   gst_pad_set_getcaps_function (self->srcpad,
1483       GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_getcaps));
1485   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
1486   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1488   self->input_width = 0;
1489   self->input_height = 0;
1490   /* sane defaults in case we need to create codec without caps negotiation
1491    * (for example, to get 'version' property)
1492    */
1493   self->width = 128;
1494   self->height = 128;
1495   self->fps_n = -1;
1496   self->fps_d = -1;
1498   self->first_in_buffer = TRUE;
1499   self->first_out_buffer = TRUE;
1500   self->interlaced = FALSE;
1501   self->send_crop_event = TRUE;
1503 #ifdef USE_DTS_PTS_CODE
1504   self->dts_ridx = self->dts_widx = 0;
1505   self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
1506   self->ts_may_be_pts = TRUE;
1507   self->ts_is_pts = FALSE;
1508 #endif
1510   self->pageMemType = XDM_MEMTYPE_TILEDPAGE;
1512   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1514   self->qos_proportion = 1;
1515   self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1516   self->wait_keyframe = TRUE;
1518   self->need_out_buf = TRUE;
1519   self->device = NULL;
1520   self->input_bo = NULL;
1522   self->backlog_maxframes = 0;
1523   self->backlog_nframes = 0;
1524   self->backlog_max_maxframes = MAX_BACKLOG_FRAMES;
1526   self->passed_in_bufs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1527       NULL, (GDestroyNotify) gst_buffer_unref);