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_STRIDED ("NV12", "[ 0, max ]"))
35 );
37 enum
38 {
39 PROP_0,
40 PROP_VERSION,
41 };
43 /* helper functions */
45 static void
46 engine_close (GstDucatiVidDec * self)
47 {
48 if (self->engine) {
49 Engine_close (self->engine);
50 self->engine = NULL;
51 }
53 if (self->params) {
54 dce_free (self->params);
55 self->params = NULL;
56 }
58 if (self->dynParams) {
59 dce_free (self->dynParams);
60 self->dynParams = NULL;
61 }
63 if (self->status) {
64 dce_free (self->status);
65 self->status = NULL;
66 }
68 if (self->inBufs) {
69 dce_free (self->inBufs);
70 self->inBufs = NULL;
71 }
73 if (self->outBufs) {
74 dce_free (self->outBufs);
75 self->outBufs = NULL;
76 }
78 if (self->inArgs) {
79 dce_free (self->inArgs);
80 self->inArgs = NULL;
81 }
83 if (self->outArgs) {
84 dce_free (self->outArgs);
85 self->outArgs = NULL;
86 }
88 if (self->device) {
89 dce_deinit (self->device);
90 self->device = NULL;
91 }
92 }
94 static gboolean
95 engine_open (GstDucatiVidDec * self)
96 {
97 gboolean ret;
98 int ec;
100 if (G_UNLIKELY (self->engine)) {
101 return TRUE;
102 }
104 if (self->device == NULL) {
105 self->device = dce_init ();
106 if (self->device == NULL) {
107 GST_ERROR_OBJECT (self, "dce_init() failed");
108 return FALSE;
109 }
110 }
112 GST_DEBUG_OBJECT (self, "opening engine");
114 self->engine = Engine_open ((String) "ivahd_vidsvr", NULL, &ec);
115 if (G_UNLIKELY (!self->engine)) {
116 GST_ERROR_OBJECT (self, "could not create engine");
117 return FALSE;
118 }
120 ret = GST_DUCATIVIDDEC_GET_CLASS (self)->allocate_params (self,
121 sizeof (IVIDDEC3_Params), sizeof (IVIDDEC3_DynamicParams),
122 sizeof (IVIDDEC3_Status), sizeof (IVIDDEC3_InArgs),
123 sizeof (IVIDDEC3_OutArgs));
125 return ret;
126 }
128 static void
129 codec_delete (GstDucatiVidDec * self)
130 {
131 if (self->pool) {
132 gst_drm_buffer_pool_destroy (self->pool);
133 self->pool = NULL;
134 }
136 if (self->codec) {
137 VIDDEC3_delete (self->codec);
138 self->codec = NULL;
139 }
141 if (self->input_bo) {
142 omap_bo_del (self->input_bo);
143 self->input_bo = NULL;
144 }
145 }
147 static gboolean
148 codec_create (GstDucatiVidDec * self)
149 {
150 gint err;
151 const gchar *codec_name;
153 codec_delete (self);
155 if (G_UNLIKELY (!self->engine)) {
156 GST_ERROR_OBJECT (self, "no engine");
157 return FALSE;
158 }
160 /* these need to be set before VIDDEC3_create */
161 self->params->maxWidth = self->width;
162 self->params->maxHeight = self->height;
164 codec_name = GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name;
166 /* create codec: */
167 GST_DEBUG_OBJECT (self, "creating codec: %s", codec_name);
168 self->codec =
169 VIDDEC3_create (self->engine, (String) codec_name, self->params);
171 if (!self->codec) {
172 return FALSE;
173 }
175 err =
176 VIDDEC3_control (self->codec, XDM_SETPARAMS, self->dynParams,
177 self->status);
178 if (err) {
179 GST_ERROR_OBJECT (self, "failed XDM_SETPARAMS");
180 return FALSE;
181 }
183 self->first_in_buffer = TRUE;
184 self->first_out_buffer = TRUE;
186 /* allocate input buffer and initialize inBufs: */
187 /* FIXME: needed size here has nothing to do with width * height */
188 self->input_bo = omap_bo_new (self->device,
189 self->width * self->height, OMAP_BO_WC);
190 self->input = omap_bo_map (self->input_bo);
191 self->inBufs->numBufs = 1;
192 self->inBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (self->input_bo);
194 return TRUE;
195 }
197 static inline GstBuffer *
198 codec_buffer_pool_get (GstDucatiVidDec * self, GstBuffer * buf)
199 {
200 if (G_UNLIKELY (!self->pool)) {
201 guint size = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
202 self->padded_width, self->padded_height);
204 GST_DEBUG_OBJECT (self, "creating bufferpool");
205 self->pool = gst_drm_buffer_pool_new (GST_ELEMENT (self),
206 dce_get_fd (), GST_PAD_CAPS (self->srcpad), size);
207 }
208 return GST_BUFFER (gst_drm_buffer_pool_get (self->pool, FALSE));
209 }
211 static GstDucatiBufferPriv *
212 get_buffer_priv (GstDucatiVidDec * self, GstBuffer * buf)
213 {
214 GstDucatiBufferPriv *priv = gst_ducati_buffer_priv_get (buf);
215 if (!priv) {
216 GstVideoFormat format = GST_VIDEO_FORMAT_NV12;
217 GstDmaBuf *dmabuf = gst_buffer_get_dma_buf (buf);
219 /* if it isn't a dmabuf buffer that we can import, then there
220 * is nothing we can do with it:
221 */
222 if (!dmabuf) {
223 GST_DEBUG_OBJECT (self, "not importing non dmabuf buffer");
224 return NULL;
225 }
227 priv = gst_ducati_buffer_priv_new ();
229 priv->bo = omap_bo_from_dmabuf (self->device, gst_dma_buf_get_fd (dmabuf));
231 priv->uv_offset = gst_video_format_get_component_offset (format,
232 1, self->stride, self->padded_height);
233 priv->size = gst_video_format_get_size (format,
234 self->stride, self->padded_height);
236 gst_ducati_buffer_priv_set (buf, priv);
237 gst_mini_object_unref (GST_MINI_OBJECT (priv));
238 }
239 return priv;
240 }
242 static XDAS_Int32
243 codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer ** buf,
244 gboolean force_internal)
245 {
246 GstDucatiBufferPriv *priv = NULL;
248 if (!force_internal)
249 priv = get_buffer_priv (self, *buf);
251 if (!priv) {
252 GstBuffer *orig = *buf;
254 GST_DEBUG_OBJECT (self, "internal bufferpool forced");
255 *buf = codec_buffer_pool_get (self, NULL);
256 GST_BUFFER_TIMESTAMP (*buf) = GST_BUFFER_TIMESTAMP (orig);
257 GST_BUFFER_DURATION (*buf) = GST_BUFFER_DURATION (orig);
258 gst_buffer_unref (orig);
259 return codec_prepare_outbuf (self, buf, FALSE);
260 }
262 self->outBufs->numBufs = 2;
263 self->outBufs->descs[0].memType = XDM_MEMTYPE_BO;
264 self->outBufs->descs[0].buf = (XDAS_Int8 *) omap_bo_handle (priv->bo);
265 self->outBufs->descs[0].bufSize.bytes = priv->uv_offset;
266 self->outBufs->descs[1].memType = XDM_MEMTYPE_BO_OFFSET;
267 self->outBufs->descs[1].buf = (XDAS_Int8 *) priv->uv_offset;
268 self->outBufs->descs[1].bufSize.bytes = priv->size - priv->uv_offset;
270 return (XDAS_Int32) * buf; // XXX use lookup table
271 }
273 static GstBuffer *
274 codec_get_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
275 {
276 GstBuffer *buf = (GstBuffer *) id; // XXX use lookup table
277 if (buf) {
278 gst_buffer_ref (buf);
279 }
280 return buf;
281 }
283 static void
284 codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
285 {
286 GstBuffer *buf = (GstBuffer *) id; // XXX use lookup table
287 if (buf) {
288 GST_DEBUG_OBJECT (self, "free buffer: %d %p", id, buf);
289 gst_buffer_unref (buf);
290 }
291 }
293 static void
294 gst_ducati_viddec_on_flush (GstDucatiVidDec * self, gboolean eos)
295 {
296 }
298 static gint
299 codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush,
300 GstFlowReturn * flow_ret)
301 {
302 gint err;
303 GstClockTime t;
304 GstBuffer *outbuf = NULL;
305 gint i;
306 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
307 GstFlowReturn ret = GST_FLOW_OK;
308 if (flow_ret)
309 /* never leave flow_ret uninitialized */
310 *flow_ret = GST_FLOW_OK;
312 memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
313 memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
315 t = gst_util_get_timestamp ();
316 err = VIDDEC3_process (self->codec,
317 self->inBufs, self->outBufs, self->inArgs, self->outArgs);
318 GST_DEBUG_OBJECT (self, "%10dns", (gint) (gst_util_get_timestamp () - t));
320 if (err) {
321 GST_WARNING_OBJECT (self, "err=%d, extendedError=%08x",
322 err, self->outArgs->extendedError);
323 gst_ducati_log_extended_error_info (self->outArgs->extendedError);
325 err = VIDDEC3_control (self->codec, XDM_GETSTATUS,
326 self->dynParams, self->status);
327 if (err) {
328 GST_WARNING_OBJECT (self, "XDM_GETSTATUS: err=%d, extendedError=%08x",
329 err, self->status->extendedError);
330 gst_ducati_log_extended_error_info (self->status->extendedError);
331 }
333 if (flush)
334 err = XDM_EFAIL;
335 else
336 err = klass->handle_error (self, err,
337 self->outArgs->extendedError, self->status->extendedError);
338 }
340 /* we now let the codec decide */
341 self->dynParams->newFrameFlag = XDAS_FALSE;
343 if (err == XDM_EFAIL)
344 goto skip_outbuf_processing;
346 for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->outputID[i]; i++) {
347 gboolean interlaced;
349 outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
351 interlaced =
352 self->outArgs->decodedBufs.contentType ==
353 IVIDEO_PROGRESSIVE ? FALSE : TRUE;
355 /* if send is FALSE, don't try to renegotiate as we could be flushing during
356 * a PAUSED->READY state change
357 */
358 if (send && interlaced != self->interlaced) {
359 GstCaps *caps;
361 GST_WARNING_OBJECT (self, "upstream set interlaced=%d but codec "
362 "thinks interlaced=%d... trusting codec", self->interlaced,
363 interlaced);
365 self->interlaced = interlaced;
367 caps =
368 gst_caps_make_writable (gst_pad_get_negotiated_caps (self->srcpad));
369 GST_INFO_OBJECT (self, "changing interlace field in caps");
370 gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
371 NULL);
372 gst_drm_buffer_pool_set_caps (self->pool, caps);
373 if (!gst_pad_set_caps (self->srcpad, caps)) {
374 GST_ERROR_OBJECT (self,
375 "downstream didn't want to change interlace mode");
376 err = XDM_EFAIL;
377 }
378 gst_caps_unref (caps);
380 /* this buffer still has the old caps so we skip it */
381 send = FALSE;
382 }
384 if (G_UNLIKELY (self->send_crop_event) && send) {
385 gint crop_width, crop_height;
386 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
388 /* send region of interest to sink on first buffer: */
389 XDM_Rect *r = &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
391 crop_width = r->bottomRight.x - r->topLeft.x;
392 crop_height = r->bottomRight.y - r->topLeft.y;
394 if (crop_width > self->input_width)
395 crop_width = self->input_width;
396 if (crop_height > self->input_height)
397 crop_height = self->input_height;
399 if (self->interlaced && !strcmp (klass->codec_name, "ivahd_mpeg2vdec"))
400 crop_height = crop_height / 2;
402 GST_INFO_OBJECT (self, "active frame region %d, %d, %d, %d, crop %dx%d",
403 r->topLeft.x, r->topLeft.y, r->bottomRight.x, r->bottomRight.y,
404 crop_width, crop_height);
406 gst_pad_push_event (self->srcpad,
407 gst_event_new_crop (r->topLeft.y, r->topLeft.x,
408 crop_width, crop_height));
410 if (self->crop)
411 gst_video_crop_unref (self->crop);
413 self->crop = gst_video_crop_new (r->topLeft.y, r->topLeft.x,
414 crop_width, crop_height);
416 self->send_crop_event = FALSE;
417 }
419 if (G_UNLIKELY (self->first_out_buffer) && send) {
420 GstDRMBufferPool *pool;
421 self->first_out_buffer = FALSE;
423 /* Destroy the pool so the buffers we used so far are eventually released.
424 * The pool will be recreated if needed.
425 */
426 pool = self->pool;
427 self->pool = NULL;
428 gst_drm_buffer_pool_destroy (pool);
429 }
431 if (send) {
432 GstClockTime ts;
434 ts = GST_BUFFER_TIMESTAMP (outbuf);
436 GST_DEBUG_OBJECT (self, "got buffer: %d %p (%" GST_TIME_FORMAT ")",
437 i, outbuf, GST_TIME_ARGS (ts));
439 #ifdef USE_DTS_PTS_CODE
440 if (self->ts_may_be_pts) {
441 if ((self->last_pts != GST_CLOCK_TIME_NONE) && (self->last_pts > ts)) {
442 GST_DEBUG_OBJECT (self, "detected PTS going backwards, "
443 "enabling ts_is_pts");
444 self->ts_is_pts = TRUE;
445 }
446 }
447 #endif
449 self->last_pts = ts;
451 if (self->dts_ridx != self->dts_widx) {
452 ts = self->dts_queue[self->dts_ridx++ % NDTS];
453 }
455 if (self->ts_is_pts) {
456 /* if we have a queued DTS from demuxer, use that instead: */
457 GST_BUFFER_TIMESTAMP (outbuf) = ts;
458 GST_DEBUG_OBJECT (self, "fixed ts: %d %p (%" GST_TIME_FORMAT ")",
459 i, outbuf, GST_TIME_ARGS (ts));
460 }
462 if (GST_BUFFER_CAPS (outbuf) &&
463 !gst_caps_is_equal (GST_BUFFER_CAPS (outbuf),
464 GST_PAD_CAPS (self->srcpad))) {
465 /* this looks a bit scary but it's really just to change the interlace=
466 * field in caps when we start as !interlaced and the codec detects
467 * otherwise */
468 GST_WARNING_OBJECT (self, "overriding buffer caps to fix "
469 "interlace mismatch");
470 outbuf = gst_buffer_make_metadata_writable (outbuf);
471 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (self->srcpad));
472 }
474 if (self->crop)
475 gst_buffer_set_video_crop (outbuf, self->crop);
477 ret = klass->push_output (self, outbuf);
478 if (flow_ret)
479 *flow_ret = ret;
480 if (ret != GST_FLOW_OK) {
481 GST_WARNING_OBJECT (self, "push failed %s", gst_flow_get_name (ret));
482 /* just unref the remaining buffers (if any) */
483 send = FALSE;
484 }
485 } else {
486 GST_DEBUG_OBJECT (self, "free buffer: %d %p", i, outbuf);
487 gst_buffer_unref (outbuf);
488 }
489 }
491 skip_outbuf_processing:
492 for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->freeBufID[i]; i++) {
493 codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
494 }
496 return err;
497 }
499 /** call control(FLUSH), and then process() to pop out all buffers */
500 gboolean
501 gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos)
502 {
503 gint err = FALSE;
505 GST_DEBUG_OBJECT (self, "flush: eos=%d", eos);
507 GST_DUCATIVIDDEC_GET_CLASS (self)->on_flush (self, eos);
509 /* note: flush is synchronized against _chain() to avoid calling
510 * the codec from multiple threads
511 */
512 GST_PAD_STREAM_LOCK (self->sinkpad);
514 #ifdef USE_DTS_PTS_CODE
515 self->dts_ridx = self->dts_widx = 0;
516 self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
517 self->ts_may_be_pts = TRUE;
518 self->ts_is_pts = FALSE;
519 #endif
520 self->wait_keyframe = TRUE;
521 self->in_size = 0;
522 self->needs_flushing = FALSE;
523 self->need_out_buf = TRUE;
525 if (G_UNLIKELY (self->first_in_buffer)) {
526 goto out;
527 }
529 if (G_UNLIKELY (!self->codec)) {
530 GST_WARNING_OBJECT (self, "no codec");
531 goto out;
532 }
534 err = VIDDEC3_control (self->codec, XDM_FLUSH, self->dynParams, self->status);
535 if (err) {
536 GST_ERROR_OBJECT (self, "failed XDM_FLUSH");
537 goto out;
538 }
540 self->inBufs->descs[0].bufSize.bytes = 0;
541 self->inArgs->numBytes = 0;
542 self->inArgs->inputID = 0;
544 do {
545 err = codec_process (self, eos, TRUE, NULL);
546 } while (err != XDM_EFAIL);
548 /* reset outArgs in case we're flushing in codec_process trying to do error
549 * recovery */
550 memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
551 memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
553 self->dynParams->newFrameFlag = XDAS_TRUE;
555 /* on a flush, it is normal (and not an error) for the last _process() call
556 * to return an error..
557 */
558 err = XDM_EOK;
560 out:
561 GST_PAD_STREAM_UNLOCK (self->sinkpad);
562 GST_DEBUG_OBJECT (self, "done");
564 return !err;
565 }
567 /* GstDucatiVidDec vmethod default implementations */
569 static gboolean
570 gst_ducati_viddec_parse_caps (GstDucatiVidDec * self, GstStructure * s)
571 {
572 const GValue *codec_data;
573 gint w, h;
575 if (gst_structure_get_int (s, "width", &self->input_width) &&
576 gst_structure_get_int (s, "height", &self->input_height)) {
578 h = ALIGN2 (self->input_height, 4); /* round up to MB */
579 w = ALIGN2 (self->input_width, 4); /* round up to MB */
581 /* if we've already created codec, but the resolution has changed, we
582 * need to re-create the codec:
583 */
584 if (G_UNLIKELY (self->codec)) {
585 if ((h != self->height) || (w != self->width)) {
586 codec_delete (self);
587 }
588 }
590 self->width = w;
591 self->height = h;
593 codec_data = gst_structure_get_value (s, "codec_data");
595 if (codec_data) {
596 GstBuffer *buffer = gst_value_get_buffer (codec_data);
597 GST_DEBUG_OBJECT (self, "codec_data: %" GST_PTR_FORMAT, buffer);
598 self->codec_data = gst_buffer_ref (buffer);
599 }
601 return TRUE;
602 }
604 return FALSE;
605 }
607 static gboolean
608 gst_ducati_viddec_allocate_params (GstDucatiVidDec * self, gint params_sz,
609 gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz)
610 {
612 /* allocate params: */
613 self->params = dce_alloc (params_sz);
614 if (G_UNLIKELY (!self->params)) {
615 return FALSE;
616 }
617 self->params->size = params_sz;
618 self->params->maxFrameRate = 30000;
619 self->params->maxBitRate = 10000000;
621 self->params->dataEndianness = XDM_BYTE;
622 self->params->forceChromaFormat = XDM_YUV_420SP;
623 self->params->operatingMode = IVIDEO_DECODE_ONLY;
625 self->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
626 self->params->inputDataMode = IVIDEO_ENTIREFRAME;
627 self->params->outputDataMode = IVIDEO_ENTIREFRAME;
628 self->params->numInputDataUnits = 0;
629 self->params->numOutputDataUnits = 0;
631 self->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
632 self->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
633 self->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
634 self->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
636 /* allocate dynParams: */
637 self->dynParams = dce_alloc (dynparams_sz);
638 if (G_UNLIKELY (!self->dynParams)) {
639 return FALSE;
640 }
641 self->dynParams->size = dynparams_sz;
642 self->dynParams->decodeHeader = XDM_DECODE_AU;
643 self->dynParams->displayWidth = 0;
644 self->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
645 self->dynParams->newFrameFlag = XDAS_TRUE;
647 /* allocate status: */
648 self->status = dce_alloc (status_sz);
649 if (G_UNLIKELY (!self->status)) {
650 return FALSE;
651 }
652 self->status->size = status_sz;
654 /* allocate inBufs/outBufs: */
655 self->inBufs = dce_alloc (sizeof (XDM2_BufDesc));
656 self->outBufs = dce_alloc (sizeof (XDM2_BufDesc));
657 if (G_UNLIKELY (!self->inBufs) || G_UNLIKELY (!self->outBufs)) {
658 return FALSE;
659 }
661 /* allocate inArgs/outArgs: */
662 self->inArgs = dce_alloc (inargs_sz);
663 self->outArgs = dce_alloc (outargs_sz);
664 if (G_UNLIKELY (!self->inArgs) || G_UNLIKELY (!self->outArgs)) {
665 return FALSE;
666 }
667 self->inArgs->size = inargs_sz;
668 self->outArgs->size = outargs_sz;
670 return TRUE;
671 }
673 static GstBuffer *
674 gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
675 {
676 if (G_UNLIKELY (self->first_in_buffer) && self->codec_data) {
677 push_input (self, GST_BUFFER_DATA (self->codec_data),
678 GST_BUFFER_SIZE (self->codec_data));
679 }
681 /* just copy entire buffer */
682 push_input (self, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
683 gst_buffer_unref (buf);
685 return NULL;
686 }
688 static GstFlowReturn
689 gst_ducati_viddec_push_output (GstDucatiVidDec * self, GstBuffer * buf)
690 {
691 return gst_pad_push (self->srcpad, buf);
692 }
694 static gint
695 gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
696 gint extended_error, gint status_extended_error)
697 {
698 if (XDM_ISFATALERROR (extended_error))
699 ret = XDM_EFAIL;
700 else
701 ret = XDM_EOK;
703 return ret;
704 }
706 /* GstElement vmethod implementations */
708 static gboolean
709 gst_ducati_viddec_set_sink_caps (GstDucatiVidDec * self, GstCaps * caps)
710 {
711 gboolean ret = TRUE;
712 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
713 GstStructure *s;
714 GstCaps *outcaps = NULL;
715 GstStructure *out_s;
716 gint par_width, par_height;
717 gboolean par_present;
719 s = gst_caps_get_structure (caps, 0);
720 if (!klass->parse_caps (self, s)) {
721 GST_WARNING_OBJECT (self, "missing required fields");
722 ret = FALSE;
723 goto out;
724 }
726 /* update output/padded sizes */
727 klass->update_buffer_size (self);
729 if (!gst_structure_get_fraction (s, "framerate", &self->fps_n, &self->fps_d)) {
730 self->fps_n = 0;
731 self->fps_d = 1;
732 }
733 gst_structure_get_boolean (s, "interlaced", &self->interlaced);
734 par_present = gst_structure_get_fraction (s, "pixel-aspect-ratio",
735 &par_width, &par_height);
737 outcaps = gst_pad_get_allowed_caps (self->srcpad);
738 if (outcaps) {
739 outcaps = gst_caps_make_writable (outcaps);
740 gst_caps_truncate (outcaps);
741 if (gst_caps_is_empty (outcaps)) {
742 gst_caps_unref (outcaps);
743 outcaps = NULL;
744 }
745 }
747 if (!outcaps) {
748 /* note: default to non-strided for better compatibility with
749 * other gst elements that don't understand stride:
750 */
751 outcaps = gst_caps_new_simple ("video/x-raw-yuv",
752 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N', 'V', '1', '2'), NULL);
753 }
755 out_s = gst_caps_get_structure (outcaps, 0);
756 gst_structure_set (out_s,
757 "width", G_TYPE_INT, self->padded_width,
758 "height", G_TYPE_INT, self->padded_height,
759 "framerate", GST_TYPE_FRACTION, self->fps_n, self->fps_d, NULL);
760 if (par_present)
761 gst_structure_set (out_s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
762 par_width, par_height, NULL);
764 if (self->interlaced)
765 gst_structure_set (out_s, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
767 if (!strcmp (gst_structure_get_name (out_s), "video/x-raw-yuv-strided")) {
768 if (!gst_structure_get_int (out_s, "rowstride", &self->stride)) {
769 self->stride = 4096;
770 gst_structure_set (out_s, "rowstride", G_TYPE_INT, self->stride, NULL);
771 }
772 } else {
773 self->stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_NV12,
774 0, self->padded_width);
775 }
777 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
778 self->stride, self->padded_height);
780 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
781 self->outsize, self->stride, outcaps);
783 if (!self->first_in_buffer) {
784 /* Caps changed mid stream. We flush the codec to unlock all the potentially
785 * locked buffers. This is needed for downstream sinks that provide a
786 * buffer pool and need to destroy all the outstanding buffers before they
787 * can negotiate new caps (hello v4l2sink).
788 */
789 gst_ducati_viddec_codec_flush (self, FALSE);
790 }
792 /* (re)send a crop event when caps change */
793 self->send_crop_event = TRUE;
795 ret = gst_pad_set_caps (self->srcpad, outcaps);
797 GST_INFO_OBJECT (self, "set caps done %d, %" GST_PTR_FORMAT, ret, outcaps);
799 out:
800 if (outcaps)
801 gst_caps_unref (outcaps);
803 return ret;
804 }
806 static gboolean
807 gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
808 {
809 gboolean ret = TRUE;
810 GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
811 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
813 GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
815 ret = klass->set_sink_caps (self, caps);
817 gst_object_unref (self);
819 return ret;
820 }
822 static GstCaps *
823 gst_ducati_viddec_src_getcaps (GstPad * pad)
824 {
825 GstCaps *caps = NULL;
826 GstCaps *outcaps = NULL;
827 int i;
829 caps = GST_PAD_CAPS (pad);
830 if (caps == NULL) {
831 outcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
832 return outcaps;
833 }
835 outcaps = gst_caps_new_empty ();
837 /* allow -rowstrided and regular yuv */
838 for (i = 0; i < gst_caps_get_size (caps); i++) {
839 GstStructure *structure;
840 GstStructure *modified_structure;
841 GValue value = { 0 };
843 structure = gst_caps_get_structure (caps, i);
844 gst_caps_append_structure (outcaps, gst_structure_copy (structure));
845 modified_structure = gst_structure_copy (structure);
847 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
848 gst_structure_set_name (modified_structure, "video/x-raw-yuv-strided");
849 g_value_init (&value, GST_TYPE_INT_RANGE);
850 gst_value_set_int_range (&value, 0, G_MAXINT);
851 gst_structure_set_value (modified_structure, "rowstride", &value);
852 gst_caps_append_structure (outcaps, modified_structure);
853 g_value_unset (&value);
854 } else {
855 gst_structure_set_name (modified_structure, "video/x-raw-yuv");
856 gst_structure_remove_field (modified_structure, "rowstride");
857 gst_caps_append_structure (outcaps, modified_structure);
858 }
859 }
860 return outcaps;
861 }
863 static gboolean
864 gst_ducati_viddec_query (GstDucatiVidDec * self, GstPad * pad,
865 GstQuery * query, gboolean * forward)
866 {
867 gboolean res = TRUE;
869 switch (GST_QUERY_TYPE (query)) {
870 case GST_QUERY_BUFFERS:
871 GST_DEBUG_OBJECT (self, "min buffers: %d", self->min_buffers);
872 gst_query_set_buffers_count (query, self->min_buffers);
874 GST_DEBUG_OBJECT (self, "min dimensions: %dx%d",
875 self->padded_width, self->padded_height);
876 gst_query_set_buffers_dimensions (query,
877 self->padded_width, self->padded_height);
878 *forward = FALSE;
879 break;
880 default:
881 break;
882 }
885 return res;
886 }
888 static gboolean
889 gst_ducati_viddec_src_query (GstPad * pad, GstQuery * query)
890 {
891 gboolean res = TRUE, forward = TRUE;
892 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
893 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
895 GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
896 res = klass->query (self, pad, query, &forward);
897 if (res && forward)
898 res = gst_pad_query_default (pad, query);
900 return res;
901 }
903 static gboolean
904 gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
905 {
906 GstClockTime timestamp, qostime;
907 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
908 gint64 diff;
910 if (self->wait_keyframe) {
911 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
912 GST_INFO_OBJECT (self, "skipping until the next keyframe");
913 return FALSE;
914 }
916 self->wait_keyframe = FALSE;
917 }
919 timestamp = GST_BUFFER_TIMESTAMP (buf);
920 if (self->segment.format != GST_FORMAT_TIME ||
921 self->qos_earliest_time == GST_CLOCK_TIME_NONE)
922 goto no_qos;
924 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)))
925 goto no_qos;
927 qostime = gst_segment_to_running_time (&self->segment,
928 GST_FORMAT_TIME, timestamp);
929 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime)))
930 /* out of segment */
931 goto no_qos;
933 /* see how our next timestamp relates to the latest qos timestamp. negative
934 * values mean we are early, positive values mean we are too late. */
935 diff = GST_CLOCK_DIFF (qostime, self->qos_earliest_time);
937 GST_DEBUG_OBJECT (self, "QOS: qostime %" GST_TIME_FORMAT
938 ", earliest %" GST_TIME_FORMAT " diff %" G_GINT64_FORMAT " proportion %f",
939 GST_TIME_ARGS (qostime), GST_TIME_ARGS (self->qos_earliest_time), diff,
940 self->qos_proportion);
942 if (klass->can_drop_frame (self, buf, diff)) {
943 GST_INFO_OBJECT (self, "dropping frame");
944 return FALSE;
945 }
947 no_qos:
948 return TRUE;
949 }
951 static gboolean
952 gst_ducati_viddec_can_drop_frame (GstDucatiVidDec * self, GstBuffer * buf,
953 gint64 diff)
954 {
955 gboolean is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf,
956 GST_BUFFER_FLAG_DELTA_UNIT);
958 if (diff >= 0 && !is_keyframe)
959 return TRUE;
961 return FALSE;
962 }
964 static GstFlowReturn
965 gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
966 {
967 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
968 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
969 GstFlowReturn ret = GST_FLOW_OK;
970 Int32 err;
971 GstBuffer *outbuf = NULL;
972 GstCaps *outcaps = NULL;
973 gboolean decode;
975 if (G_UNLIKELY (!self->engine)) {
976 GST_ERROR_OBJECT (self, "no engine");
977 gst_buffer_unref (buf);
978 return GST_FLOW_ERROR;
979 }
981 GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes, flags %d)",
982 GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
984 decode = gst_ducati_viddec_do_qos (self, buf);
985 if (!decode) {
986 gst_buffer_unref (buf);
987 return GST_FLOW_OK;
988 }
990 if (!self->need_out_buf)
991 goto have_out_buf;
993 /* do this before creating codec to ensure reverse caps negotiation
994 * happens first:
995 */
996 allocate_buffer:
997 ret = gst_pad_alloc_buffer (self->srcpad, 0, self->outsize,
998 GST_PAD_CAPS (self->srcpad), &outbuf);
999 if (ret != GST_FLOW_OK) {
1000 GST_WARNING_OBJECT (self, "alloc_buffer failed %s",
1001 gst_flow_get_name (ret));
1002 gst_buffer_unref (buf);
1003 return ret;
1004 }
1006 outcaps = GST_BUFFER_CAPS (outbuf);
1007 if (outcaps && !gst_caps_is_equal (outcaps, GST_PAD_CAPS (self->srcpad))) {
1008 GstStructure *s;
1010 GST_INFO_OBJECT (self, "doing upstream negotiation bufsize %d",
1011 GST_BUFFER_SIZE (outbuf));
1013 s = gst_caps_get_structure (outcaps, 0);
1014 gst_structure_get_int (s, "rowstride", &self->stride);
1015 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
1016 self->stride, self->padded_height);
1018 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
1019 self->outsize, self->stride, outcaps);
1021 gst_pad_set_caps (self->srcpad, outcaps);
1023 if (GST_BUFFER_SIZE (outbuf) != self->outsize) {
1024 GST_INFO_OBJECT (self, "dropping buffer (bufsize %d != outsize %d)",
1025 GST_BUFFER_SIZE (outbuf), self->outsize);
1026 gst_buffer_unref (outbuf);
1027 goto allocate_buffer;
1028 }
1029 }
1031 if (G_UNLIKELY (!self->codec)) {
1032 if (!codec_create (self)) {
1033 GST_ERROR_OBJECT (self, "could not create codec");
1034 gst_buffer_unref (buf);
1035 gst_buffer_unref (outbuf);
1036 return GST_FLOW_ERROR;
1037 }
1038 }
1040 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
1041 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
1043 /* Pass new output buffer to the decoder to decode into. Use buffers from the
1044 * internal pool while self->first_out_buffer == TRUE in order to simplify
1045 * things in case we need to renegotiate */
1046 self->inArgs->inputID =
1047 codec_prepare_outbuf (self, &outbuf, self->first_out_buffer);
1048 if (!self->inArgs->inputID) {
1049 GST_ERROR_OBJECT (self, "could not prepare output buffer");
1050 gst_buffer_unref (buf);
1051 return GST_FLOW_ERROR;
1052 }
1054 have_out_buf:
1055 buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
1057 #ifdef USE_DTS_PTS_CODE
1058 if (ts != GST_CLOCK_TIME_NONE) {
1059 self->dts_queue[self->dts_widx++ % NDTS] = ts;
1060 /* if next buffer has earlier ts than previous, then the ts
1061 * we are getting are definitely decode order (DTS):
1062 */
1063 if ((self->last_dts != GST_CLOCK_TIME_NONE) && (self->last_dts > ts)) {
1064 GST_DEBUG_OBJECT (self, "input timestamp definitely DTS");
1065 self->ts_may_be_pts = FALSE;
1066 }
1067 self->last_dts = ts;
1068 }
1069 #endif
1071 if (self->in_size == 0 && outbuf) {
1072 GST_DEBUG_OBJECT (self, "no input, skipping process");
1073 gst_buffer_unref (outbuf);
1074 return GST_FLOW_OK;
1075 }
1077 self->inArgs->numBytes = self->in_size;
1078 self->inBufs->descs[0].bufSize.bytes = self->in_size;
1079 self->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
1081 err = codec_process (self, TRUE, FALSE, &ret);
1082 if (err) {
1083 GST_ELEMENT_ERROR (self, STREAM, DECODE, (NULL),
1084 ("process returned error: %d %08x", err, self->outArgs->extendedError));
1085 gst_ducati_log_extended_error_info (self->outArgs->extendedError);
1086 return GST_FLOW_ERROR;
1087 }
1088 if (ret != GST_FLOW_OK) {
1089 GST_WARNING_OBJECT (self, "push from codec_process failed %s",
1090 gst_flow_get_name (ret));
1091 return ret;
1092 }
1094 self->first_in_buffer = FALSE;
1096 /* The copy could be avoided by playing with the buffer pointer,
1097 but it seems to be rare and for not many bytes */
1098 GST_DEBUG_OBJECT (self, "Consumed %d/%d (%d) bytes, %d left",
1099 self->outArgs->bytesConsumed, self->in_size,
1100 self->inArgs->numBytes,
1101 self->in_size - self->outArgs->bytesConsumed);
1102 if (self->outArgs->bytesConsumed > 0) {
1103 if (self->outArgs->bytesConsumed > self->in_size) {
1104 GST_WARNING_OBJECT (self, "Codec claims to have used more bytes than supplied");
1105 self->in_size = 0;
1106 } else {
1107 if (self->outArgs->bytesConsumed < self->in_size) {
1108 memmove (self->input, self->input + self->outArgs->bytesConsumed,
1109 self->in_size - self->outArgs->bytesConsumed);
1110 }
1111 self->in_size -= self->outArgs->bytesConsumed;
1112 }
1113 }
1115 if (self->outArgs->outBufsInUseFlag) {
1116 GST_DEBUG_OBJECT (self, "outBufsInUseFlag set");
1117 self->need_out_buf = FALSE;
1118 } else {
1119 self->need_out_buf = TRUE;
1120 }
1122 if (buf) {
1123 GST_DEBUG_OBJECT (self, "found remaining data: %d bytes",
1124 GST_BUFFER_SIZE (buf));
1125 ts = GST_BUFFER_TIMESTAMP (buf);
1126 goto allocate_buffer;
1127 }
1129 if (self->needs_flushing)
1130 gst_ducati_viddec_codec_flush (self, FALSE);
1132 return GST_FLOW_OK;
1133 }
1135 static gboolean
1136 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
1137 {
1138 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1139 gboolean ret = TRUE;
1141 GST_DEBUG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1143 switch (GST_EVENT_TYPE (event)) {
1144 case GST_EVENT_NEWSEGMENT:
1145 {
1146 gboolean update;
1147 GstFormat fmt;
1148 gint64 start, stop, time;
1149 gdouble rate, arate;
1151 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
1152 &start, &stop, &time);
1153 gst_segment_set_newsegment_full (&self->segment, update,
1154 rate, arate, fmt, start, stop, time);
1155 break;
1156 }
1157 case GST_EVENT_EOS:
1158 if (!gst_ducati_viddec_codec_flush (self, TRUE)) {
1159 GST_ERROR_OBJECT (self, "could not flush on eos");
1160 ret = FALSE;
1161 }
1162 break;
1163 case GST_EVENT_FLUSH_STOP:
1164 if (!gst_ducati_viddec_codec_flush (self, FALSE)) {
1165 GST_ERROR_OBJECT (self, "could not flush");
1166 gst_event_unref (event);
1167 ret = FALSE;
1168 }
1169 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1170 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1171 self->qos_proportion = 1;
1172 self->need_out_buf = TRUE;
1173 break;
1174 default:
1175 break;
1176 }
1178 if (ret)
1179 ret = gst_pad_push_event (self->srcpad, event);
1180 GST_LOG_OBJECT (self, "end");
1182 return ret;
1183 }
1185 static gboolean
1186 gst_ducati_viddec_src_event (GstPad * pad, GstEvent * event)
1187 {
1188 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1189 gboolean ret = TRUE;
1191 GST_LOG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1193 switch (GST_EVENT_TYPE (event)) {
1194 case GST_EVENT_QOS:
1195 {
1196 gdouble proportion;
1197 GstClockTimeDiff diff;
1198 GstClockTime timestamp;
1200 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1202 GST_OBJECT_LOCK (self);
1203 self->qos_proportion = proportion;
1204 self->qos_earliest_time = timestamp + 2 * diff;
1205 GST_OBJECT_UNLOCK (self);
1207 GST_DEBUG_OBJECT (self,
1208 "got QoS proportion %f %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1209 proportion, GST_TIME_ARGS (timestamp), diff);
1211 ret = gst_pad_push_event (self->sinkpad, event);
1212 break;
1213 }
1214 default:
1215 ret = gst_pad_push_event (self->sinkpad, event);
1216 break;
1217 }
1219 GST_LOG_OBJECT (self, "end");
1221 return ret;
1222 }
1224 static GstStateChangeReturn
1225 gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
1226 {
1227 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1228 GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
1229 gboolean supported;
1231 GST_DEBUG_OBJECT (self, "begin: changing state %s -> %s",
1232 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1233 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1235 switch (transition) {
1236 case GST_STATE_CHANGE_NULL_TO_READY:
1237 if (!engine_open (self)) {
1238 GST_ERROR_OBJECT (self, "could not open");
1239 return GST_STATE_CHANGE_FAILURE;
1240 }
1241 /* try to create/destroy the codec here, it may not be supported */
1242 supported = codec_create (self);
1243 codec_delete (self);
1244 self->codec = NULL;
1245 if (!supported) {
1246 GST_ERROR_OBJECT (element, "Failed to create codec %s, not supported",
1247 GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name);
1248 engine_close (self);
1249 return GST_STATE_CHANGE_FAILURE;
1250 }
1251 break;
1252 default:
1253 break;
1254 }
1256 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1258 if (ret == GST_STATE_CHANGE_FAILURE)
1259 goto leave;
1261 switch (transition) {
1262 case GST_STATE_CHANGE_PAUSED_TO_READY:
1263 self->interlaced = FALSE;
1264 self->send_crop_event = TRUE;
1265 gst_ducati_viddec_codec_flush (self, FALSE);
1266 break;
1267 case GST_STATE_CHANGE_READY_TO_NULL:
1268 codec_delete (self);
1269 engine_close (self);
1270 break;
1271 default:
1272 break;
1273 }
1275 leave:
1276 GST_LOG_OBJECT (self, "end");
1278 return ret;
1279 }
1281 /* GObject vmethod implementations */
1283 #define VERSION_LENGTH 256
1285 static void
1286 gst_ducati_viddec_get_property (GObject * obj,
1287 guint prop_id, GValue * value, GParamSpec * pspec)
1288 {
1289 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1292 switch (prop_id) {
1293 case PROP_VERSION:{
1294 int err;
1295 char *version = NULL;
1297 if (!self->engine)
1298 engine_open (self);
1300 if (!self->codec)
1301 codec_create (self);
1303 if (self->codec) {
1304 version = dce_alloc (VERSION_LENGTH);
1305 self->status->data.buf = (XDAS_Int8 *) version;
1306 self->status->data.bufSize = VERSION_LENGTH;
1308 err = VIDDEC3_control (self->codec, XDM_GETVERSION,
1309 self->dynParams, self->status);
1310 if (err) {
1311 GST_ERROR_OBJECT (self, "failed XDM_GETVERSION");
1312 }
1314 self->status->data.buf = NULL;
1315 self->status->data.bufSize = 0;
1316 }
1318 g_value_set_string (value, version);
1319 if (version)
1320 dce_free (version);
1322 break;
1323 }
1324 default:{
1325 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1326 break;
1327 }
1328 }
1329 }
1331 static void
1332 gst_ducati_viddec_finalize (GObject * obj)
1333 {
1334 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1336 codec_delete (self);
1337 engine_close (self);
1339 if (self->codec_data) {
1340 gst_buffer_unref (self->codec_data);
1341 self->codec_data = NULL;
1342 }
1344 G_OBJECT_CLASS (parent_class)->finalize (obj);
1345 }
1347 static void
1348 gst_ducati_viddec_base_init (gpointer gclass)
1349 {
1350 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
1352 gst_element_class_add_pad_template (element_class,
1353 gst_static_pad_template_get (&src_factory));
1354 }
1356 static void
1357 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
1358 {
1359 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1360 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1362 gobject_class->get_property =
1363 GST_DEBUG_FUNCPTR (gst_ducati_viddec_get_property);
1364 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ducati_viddec_finalize);
1365 gstelement_class->change_state =
1366 GST_DEBUG_FUNCPTR (gst_ducati_viddec_change_state);
1368 klass->parse_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_parse_caps);
1369 klass->allocate_params =
1370 GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
1371 klass->push_input = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
1372 klass->handle_error = GST_DEBUG_FUNCPTR (gst_ducati_viddec_handle_error);
1373 klass->can_drop_frame = GST_DEBUG_FUNCPTR (gst_ducati_viddec_can_drop_frame);
1374 klass->query = GST_DEBUG_FUNCPTR (gst_ducati_viddec_query);
1375 klass->push_output = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_output);
1376 klass->on_flush = GST_DEBUG_FUNCPTR (gst_ducati_viddec_on_flush);
1377 klass->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_sink_caps);
1379 g_object_class_install_property (gobject_class, PROP_VERSION,
1380 g_param_spec_string ("version", "Version",
1381 "The codec version string", "",
1382 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1383 }
1385 static void
1386 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
1387 {
1388 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1390 self->sinkpad =
1391 gst_pad_new_from_template (gst_element_class_get_pad_template
1392 (gstelement_class, "sink"), "sink");
1393 gst_pad_set_setcaps_function (self->sinkpad,
1394 GST_DEBUG_FUNCPTR (gst_ducati_viddec_sink_setcaps));
1395 gst_pad_set_chain_function (self->sinkpad,
1396 GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
1397 gst_pad_set_event_function (self->sinkpad,
1398 GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
1400 self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
1401 gst_pad_set_event_function (self->srcpad,
1402 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_event));
1403 gst_pad_set_query_function (self->srcpad,
1404 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_query));
1405 gst_pad_set_getcaps_function (self->srcpad,
1406 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_getcaps));
1408 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
1409 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1411 self->input_width = 0;
1412 self->input_height = 0;
1413 /* sane defaults in case we need to create codec without caps negotiation
1414 * (for example, to get 'version' property)
1415 */
1416 self->width = 128;
1417 self->height = 128;
1418 self->fps_n = -1;
1419 self->fps_d = -1;
1421 self->first_in_buffer = TRUE;
1422 self->first_out_buffer = TRUE;
1423 self->interlaced = FALSE;
1424 self->send_crop_event = TRUE;
1426 #ifdef USE_DTS_PTS_CODE
1427 self->dts_ridx = self->dts_widx = 0;
1428 self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
1429 self->ts_may_be_pts = TRUE;
1430 self->ts_is_pts = FALSE;
1431 #endif
1433 self->pageMemType = XDM_MEMTYPE_TILEDPAGE;
1435 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1437 self->qos_proportion = 1;
1438 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1439 self->wait_keyframe = TRUE;
1441 self->need_out_buf = TRUE;
1442 self->device = NULL;
1443 self->input_bo = NULL;
1444 }