11673362317b8ce9a0230890307c8f999ac3f55e
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 memset(self->status, 0, status_sz);
653 self->status->size = status_sz;
655 /* allocate inBufs/outBufs: */
656 self->inBufs = dce_alloc (sizeof (XDM2_BufDesc));
657 self->outBufs = dce_alloc (sizeof (XDM2_BufDesc));
658 if (G_UNLIKELY (!self->inBufs) || G_UNLIKELY (!self->outBufs)) {
659 return FALSE;
660 }
662 /* allocate inArgs/outArgs: */
663 self->inArgs = dce_alloc (inargs_sz);
664 self->outArgs = dce_alloc (outargs_sz);
665 if (G_UNLIKELY (!self->inArgs) || G_UNLIKELY (!self->outArgs)) {
666 return FALSE;
667 }
668 self->inArgs->size = inargs_sz;
669 self->outArgs->size = outargs_sz;
671 return TRUE;
672 }
674 static GstBuffer *
675 gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
676 {
677 if (G_UNLIKELY (self->first_in_buffer) && self->codec_data) {
678 push_input (self, GST_BUFFER_DATA (self->codec_data),
679 GST_BUFFER_SIZE (self->codec_data));
680 }
682 /* just copy entire buffer */
683 push_input (self, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
684 gst_buffer_unref (buf);
686 return NULL;
687 }
689 static GstFlowReturn
690 gst_ducati_viddec_push_output (GstDucatiVidDec * self, GstBuffer * buf)
691 {
692 return gst_pad_push (self->srcpad, buf);
693 }
695 static gint
696 gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
697 gint extended_error, gint status_extended_error)
698 {
699 if (XDM_ISFATALERROR (extended_error))
700 ret = XDM_EFAIL;
701 else
702 ret = XDM_EOK;
704 return ret;
705 }
707 /* GstElement vmethod implementations */
709 static gboolean
710 gst_ducati_viddec_set_sink_caps (GstDucatiVidDec * self, GstCaps * caps)
711 {
712 gboolean ret = TRUE;
713 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
714 GstStructure *s;
715 GstCaps *outcaps = NULL;
716 GstStructure *out_s;
717 gint par_width, par_height;
718 gboolean par_present;
720 s = gst_caps_get_structure (caps, 0);
721 if (!klass->parse_caps (self, s)) {
722 GST_WARNING_OBJECT (self, "missing required fields");
723 ret = FALSE;
724 goto out;
725 }
727 /* update output/padded sizes */
728 klass->update_buffer_size (self);
730 if (!gst_structure_get_fraction (s, "framerate", &self->fps_n, &self->fps_d)) {
731 self->fps_n = 0;
732 self->fps_d = 1;
733 }
734 gst_structure_get_boolean (s, "interlaced", &self->interlaced);
735 par_present = gst_structure_get_fraction (s, "pixel-aspect-ratio",
736 &par_width, &par_height);
738 outcaps = gst_pad_get_allowed_caps (self->srcpad);
739 if (outcaps) {
740 outcaps = gst_caps_make_writable (outcaps);
741 gst_caps_truncate (outcaps);
742 if (gst_caps_is_empty (outcaps)) {
743 gst_caps_unref (outcaps);
744 outcaps = NULL;
745 }
746 }
748 if (!outcaps) {
749 /* note: default to non-strided for better compatibility with
750 * other gst elements that don't understand stride:
751 */
752 outcaps = gst_caps_new_simple ("video/x-raw-yuv",
753 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N', 'V', '1', '2'), NULL);
754 }
756 out_s = gst_caps_get_structure (outcaps, 0);
757 gst_structure_set (out_s,
758 "width", G_TYPE_INT, self->padded_width,
759 "height", G_TYPE_INT, self->padded_height,
760 "framerate", GST_TYPE_FRACTION, self->fps_n, self->fps_d, NULL);
761 if (par_present)
762 gst_structure_set (out_s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
763 par_width, par_height, NULL);
765 if (self->interlaced)
766 gst_structure_set (out_s, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
768 if (!strcmp (gst_structure_get_name (out_s), "video/x-raw-yuv-strided")) {
769 if (!gst_structure_get_int (out_s, "rowstride", &self->stride)) {
770 self->stride = 4096;
771 gst_structure_set (out_s, "rowstride", G_TYPE_INT, self->stride, NULL);
772 }
773 } else {
774 self->stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_NV12,
775 0, self->padded_width);
776 }
778 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
779 self->stride, self->padded_height);
781 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
782 self->outsize, self->stride, outcaps);
784 if (!self->first_in_buffer) {
785 /* Caps changed mid stream. We flush the codec to unlock all the potentially
786 * locked buffers. This is needed for downstream sinks that provide a
787 * buffer pool and need to destroy all the outstanding buffers before they
788 * can negotiate new caps (hello v4l2sink).
789 */
790 gst_ducati_viddec_codec_flush (self, FALSE);
791 }
793 /* (re)send a crop event when caps change */
794 self->send_crop_event = TRUE;
796 ret = gst_pad_set_caps (self->srcpad, outcaps);
798 GST_INFO_OBJECT (self, "set caps done %d, %" GST_PTR_FORMAT, ret, outcaps);
800 out:
801 if (outcaps)
802 gst_caps_unref (outcaps);
804 return ret;
805 }
807 static gboolean
808 gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
809 {
810 gboolean ret = TRUE;
811 GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
812 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
814 GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
816 ret = klass->set_sink_caps (self, caps);
818 gst_object_unref (self);
820 return ret;
821 }
823 static GstCaps *
824 gst_ducati_viddec_src_getcaps (GstPad * pad)
825 {
826 GstCaps *caps = NULL;
827 GstCaps *outcaps = NULL;
828 int i;
830 caps = GST_PAD_CAPS (pad);
831 if (caps == NULL) {
832 outcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
833 return outcaps;
834 }
836 outcaps = gst_caps_new_empty ();
838 /* allow -rowstrided and regular yuv */
839 for (i = 0; i < gst_caps_get_size (caps); i++) {
840 GstStructure *structure;
841 GstStructure *modified_structure;
842 GValue value = { 0 };
844 structure = gst_caps_get_structure (caps, i);
845 gst_caps_append_structure (outcaps, gst_structure_copy (structure));
846 modified_structure = gst_structure_copy (structure);
848 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
849 gst_structure_set_name (modified_structure, "video/x-raw-yuv-strided");
850 g_value_init (&value, GST_TYPE_INT_RANGE);
851 gst_value_set_int_range (&value, 0, G_MAXINT);
852 gst_structure_set_value (modified_structure, "rowstride", &value);
853 gst_caps_append_structure (outcaps, modified_structure);
854 g_value_unset (&value);
855 } else {
856 gst_structure_set_name (modified_structure, "video/x-raw-yuv");
857 gst_structure_remove_field (modified_structure, "rowstride");
858 gst_caps_append_structure (outcaps, modified_structure);
859 }
860 }
861 return outcaps;
862 }
864 static gboolean
865 gst_ducati_viddec_query (GstDucatiVidDec * self, GstPad * pad,
866 GstQuery * query, gboolean * forward)
867 {
868 gboolean res = TRUE;
870 switch (GST_QUERY_TYPE (query)) {
871 case GST_QUERY_BUFFERS:
872 GST_DEBUG_OBJECT (self, "min buffers: %d", self->min_buffers);
873 gst_query_set_buffers_count (query, self->min_buffers);
875 GST_DEBUG_OBJECT (self, "min dimensions: %dx%d",
876 self->padded_width, self->padded_height);
877 gst_query_set_buffers_dimensions (query,
878 self->padded_width, self->padded_height);
879 *forward = FALSE;
880 break;
881 default:
882 break;
883 }
886 return res;
887 }
889 static gboolean
890 gst_ducati_viddec_src_query (GstPad * pad, GstQuery * query)
891 {
892 gboolean res = TRUE, forward = TRUE;
893 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
894 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
896 GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
897 res = klass->query (self, pad, query, &forward);
898 if (res && forward)
899 res = gst_pad_query_default (pad, query);
901 return res;
902 }
904 static gboolean
905 gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
906 {
907 GstClockTime timestamp, qostime;
908 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
909 gint64 diff;
911 if (self->wait_keyframe) {
912 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
913 GST_INFO_OBJECT (self, "skipping until the next keyframe");
914 return FALSE;
915 }
917 self->wait_keyframe = FALSE;
918 }
920 timestamp = GST_BUFFER_TIMESTAMP (buf);
921 if (self->segment.format != GST_FORMAT_TIME ||
922 self->qos_earliest_time == GST_CLOCK_TIME_NONE)
923 goto no_qos;
925 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)))
926 goto no_qos;
928 qostime = gst_segment_to_running_time (&self->segment,
929 GST_FORMAT_TIME, timestamp);
930 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime)))
931 /* out of segment */
932 goto no_qos;
934 /* see how our next timestamp relates to the latest qos timestamp. negative
935 * values mean we are early, positive values mean we are too late. */
936 diff = GST_CLOCK_DIFF (qostime, self->qos_earliest_time);
938 GST_DEBUG_OBJECT (self, "QOS: qostime %" GST_TIME_FORMAT
939 ", earliest %" GST_TIME_FORMAT " diff %" G_GINT64_FORMAT " proportion %f",
940 GST_TIME_ARGS (qostime), GST_TIME_ARGS (self->qos_earliest_time), diff,
941 self->qos_proportion);
943 if (klass->can_drop_frame (self, buf, diff)) {
944 GST_INFO_OBJECT (self, "dropping frame");
945 return FALSE;
946 }
948 no_qos:
949 return TRUE;
950 }
952 static gboolean
953 gst_ducati_viddec_can_drop_frame (GstDucatiVidDec * self, GstBuffer * buf,
954 gint64 diff)
955 {
956 gboolean is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf,
957 GST_BUFFER_FLAG_DELTA_UNIT);
959 if (diff >= 0 && !is_keyframe)
960 return TRUE;
962 return FALSE;
963 }
965 static GstFlowReturn
966 gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
967 {
968 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
969 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
970 GstFlowReturn ret = GST_FLOW_OK;
971 Int32 err;
972 GstBuffer *outbuf = NULL;
973 GstCaps *outcaps = NULL;
974 gboolean decode;
976 if (G_UNLIKELY (!self->engine)) {
977 GST_ERROR_OBJECT (self, "no engine");
978 gst_buffer_unref (buf);
979 return GST_FLOW_ERROR;
980 }
982 GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes, flags %d)",
983 GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
985 decode = gst_ducati_viddec_do_qos (self, buf);
986 if (!decode) {
987 gst_buffer_unref (buf);
988 return GST_FLOW_OK;
989 }
991 if (!self->need_out_buf)
992 goto have_out_buf;
994 /* do this before creating codec to ensure reverse caps negotiation
995 * happens first:
996 */
997 allocate_buffer:
998 ret = gst_pad_alloc_buffer (self->srcpad, 0, self->outsize,
999 GST_PAD_CAPS (self->srcpad), &outbuf);
1000 if (ret != GST_FLOW_OK) {
1001 GST_WARNING_OBJECT (self, "alloc_buffer failed %s",
1002 gst_flow_get_name (ret));
1003 gst_buffer_unref (buf);
1004 return ret;
1005 }
1007 outcaps = GST_BUFFER_CAPS (outbuf);
1008 if (outcaps && !gst_caps_is_equal (outcaps, GST_PAD_CAPS (self->srcpad))) {
1009 GstStructure *s;
1011 GST_INFO_OBJECT (self, "doing upstream negotiation bufsize %d",
1012 GST_BUFFER_SIZE (outbuf));
1014 s = gst_caps_get_structure (outcaps, 0);
1015 gst_structure_get_int (s, "rowstride", &self->stride);
1016 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
1017 self->stride, self->padded_height);
1019 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
1020 self->outsize, self->stride, outcaps);
1022 gst_pad_set_caps (self->srcpad, outcaps);
1024 if (GST_BUFFER_SIZE (outbuf) != self->outsize) {
1025 GST_INFO_OBJECT (self, "dropping buffer (bufsize %d != outsize %d)",
1026 GST_BUFFER_SIZE (outbuf), self->outsize);
1027 gst_buffer_unref (outbuf);
1028 goto allocate_buffer;
1029 }
1030 }
1032 if (G_UNLIKELY (!self->codec)) {
1033 if (!codec_create (self)) {
1034 GST_ERROR_OBJECT (self, "could not create codec");
1035 gst_buffer_unref (buf);
1036 gst_buffer_unref (outbuf);
1037 return GST_FLOW_ERROR;
1038 }
1039 }
1041 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
1042 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
1044 /* Pass new output buffer to the decoder to decode into. Use buffers from the
1045 * internal pool while self->first_out_buffer == TRUE in order to simplify
1046 * things in case we need to renegotiate */
1047 self->inArgs->inputID =
1048 codec_prepare_outbuf (self, &outbuf, self->first_out_buffer);
1049 if (!self->inArgs->inputID) {
1050 GST_ERROR_OBJECT (self, "could not prepare output buffer");
1051 gst_buffer_unref (buf);
1052 return GST_FLOW_ERROR;
1053 }
1055 have_out_buf:
1056 buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
1058 #ifdef USE_DTS_PTS_CODE
1059 if (ts != GST_CLOCK_TIME_NONE) {
1060 self->dts_queue[self->dts_widx++ % NDTS] = ts;
1061 /* if next buffer has earlier ts than previous, then the ts
1062 * we are getting are definitely decode order (DTS):
1063 */
1064 if ((self->last_dts != GST_CLOCK_TIME_NONE) && (self->last_dts > ts)) {
1065 GST_DEBUG_OBJECT (self, "input timestamp definitely DTS");
1066 self->ts_may_be_pts = FALSE;
1067 }
1068 self->last_dts = ts;
1069 }
1070 #endif
1072 if (self->in_size == 0 && outbuf) {
1073 GST_DEBUG_OBJECT (self, "no input, skipping process");
1074 gst_buffer_unref (outbuf);
1075 return GST_FLOW_OK;
1076 }
1078 self->inArgs->numBytes = self->in_size;
1079 self->inBufs->descs[0].bufSize.bytes = self->in_size;
1080 self->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
1082 err = codec_process (self, TRUE, FALSE, &ret);
1083 if (err) {
1084 GST_ELEMENT_ERROR (self, STREAM, DECODE, (NULL),
1085 ("process returned error: %d %08x", err, self->outArgs->extendedError));
1086 gst_ducati_log_extended_error_info (self->outArgs->extendedError);
1087 return GST_FLOW_ERROR;
1088 }
1089 if (ret != GST_FLOW_OK) {
1090 GST_WARNING_OBJECT (self, "push from codec_process failed %s",
1091 gst_flow_get_name (ret));
1092 return ret;
1093 }
1095 self->first_in_buffer = FALSE;
1097 if (self->params->inputDataMode != IVIDEO_ENTIREFRAME) {
1098 /* The copy could be avoided by playing with the buffer pointer,
1099 but it seems to be rare and for not many bytes */
1100 GST_DEBUG_OBJECT (self, "Consumed %d/%d (%d) bytes, %d left",
1101 self->outArgs->bytesConsumed, self->in_size,
1102 self->inArgs->numBytes,
1103 self->in_size - self->outArgs->bytesConsumed);
1104 if (self->outArgs->bytesConsumed > 0) {
1105 if (self->outArgs->bytesConsumed > self->in_size) {
1106 GST_WARNING_OBJECT (self, "Codec claims to have used more bytes than supplied");
1107 self->in_size = 0;
1108 } else {
1109 if (self->outArgs->bytesConsumed < self->in_size) {
1110 memmove (self->input, self->input + self->outArgs->bytesConsumed,
1111 self->in_size - self->outArgs->bytesConsumed);
1112 }
1113 self->in_size -= self->outArgs->bytesConsumed;
1114 }
1115 }
1116 } else {
1117 self->in_size = 0;
1118 }
1120 if (self->outArgs->outBufsInUseFlag) {
1121 GST_DEBUG_OBJECT (self, "outBufsInUseFlag set");
1122 self->need_out_buf = FALSE;
1123 } else {
1124 self->need_out_buf = TRUE;
1125 }
1127 if (buf) {
1128 GST_DEBUG_OBJECT (self, "found remaining data: %d bytes",
1129 GST_BUFFER_SIZE (buf));
1130 ts = GST_BUFFER_TIMESTAMP (buf);
1131 goto allocate_buffer;
1132 }
1134 if (self->needs_flushing)
1135 gst_ducati_viddec_codec_flush (self, FALSE);
1137 return GST_FLOW_OK;
1138 }
1140 static gboolean
1141 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
1142 {
1143 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1144 gboolean ret = TRUE;
1146 GST_DEBUG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1148 switch (GST_EVENT_TYPE (event)) {
1149 case GST_EVENT_NEWSEGMENT:
1150 {
1151 gboolean update;
1152 GstFormat fmt;
1153 gint64 start, stop, time;
1154 gdouble rate, arate;
1156 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
1157 &start, &stop, &time);
1158 gst_segment_set_newsegment_full (&self->segment, update,
1159 rate, arate, fmt, start, stop, time);
1160 break;
1161 }
1162 case GST_EVENT_EOS:
1163 if (!gst_ducati_viddec_codec_flush (self, TRUE)) {
1164 GST_ERROR_OBJECT (self, "could not flush on eos");
1165 ret = FALSE;
1166 }
1167 break;
1168 case GST_EVENT_FLUSH_STOP:
1169 if (!gst_ducati_viddec_codec_flush (self, FALSE)) {
1170 GST_ERROR_OBJECT (self, "could not flush");
1171 gst_event_unref (event);
1172 ret = FALSE;
1173 }
1174 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1175 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1176 self->qos_proportion = 1;
1177 self->need_out_buf = TRUE;
1178 break;
1179 default:
1180 break;
1181 }
1183 if (ret)
1184 ret = gst_pad_push_event (self->srcpad, event);
1185 GST_LOG_OBJECT (self, "end");
1187 return ret;
1188 }
1190 static gboolean
1191 gst_ducati_viddec_src_event (GstPad * pad, GstEvent * event)
1192 {
1193 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1194 gboolean ret = TRUE;
1196 GST_LOG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1198 switch (GST_EVENT_TYPE (event)) {
1199 case GST_EVENT_QOS:
1200 {
1201 gdouble proportion;
1202 GstClockTimeDiff diff;
1203 GstClockTime timestamp;
1205 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1207 GST_OBJECT_LOCK (self);
1208 self->qos_proportion = proportion;
1209 self->qos_earliest_time = timestamp + 2 * diff;
1210 GST_OBJECT_UNLOCK (self);
1212 GST_DEBUG_OBJECT (self,
1213 "got QoS proportion %f %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1214 proportion, GST_TIME_ARGS (timestamp), diff);
1216 ret = gst_pad_push_event (self->sinkpad, event);
1217 break;
1218 }
1219 default:
1220 ret = gst_pad_push_event (self->sinkpad, event);
1221 break;
1222 }
1224 GST_LOG_OBJECT (self, "end");
1226 return ret;
1227 }
1229 static GstStateChangeReturn
1230 gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
1231 {
1232 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1233 GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
1234 gboolean supported;
1236 GST_DEBUG_OBJECT (self, "begin: changing state %s -> %s",
1237 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1238 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1240 switch (transition) {
1241 case GST_STATE_CHANGE_NULL_TO_READY:
1242 if (!engine_open (self)) {
1243 GST_ERROR_OBJECT (self, "could not open");
1244 return GST_STATE_CHANGE_FAILURE;
1245 }
1246 /* try to create/destroy the codec here, it may not be supported */
1247 supported = codec_create (self);
1248 codec_delete (self);
1249 self->codec = NULL;
1250 if (!supported) {
1251 GST_ERROR_OBJECT (element, "Failed to create codec %s, not supported",
1252 GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name);
1253 engine_close (self);
1254 return GST_STATE_CHANGE_FAILURE;
1255 }
1256 break;
1257 default:
1258 break;
1259 }
1261 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1263 if (ret == GST_STATE_CHANGE_FAILURE)
1264 goto leave;
1266 switch (transition) {
1267 case GST_STATE_CHANGE_PAUSED_TO_READY:
1268 self->interlaced = FALSE;
1269 self->send_crop_event = TRUE;
1270 gst_ducati_viddec_codec_flush (self, FALSE);
1271 break;
1272 case GST_STATE_CHANGE_READY_TO_NULL:
1273 codec_delete (self);
1274 engine_close (self);
1275 break;
1276 default:
1277 break;
1278 }
1280 leave:
1281 GST_LOG_OBJECT (self, "end");
1283 return ret;
1284 }
1286 /* GObject vmethod implementations */
1288 #define VERSION_LENGTH 256
1290 static void
1291 gst_ducati_viddec_get_property (GObject * obj,
1292 guint prop_id, GValue * value, GParamSpec * pspec)
1293 {
1294 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1297 switch (prop_id) {
1298 case PROP_VERSION:{
1299 int err;
1300 char *version = NULL;
1302 if (!self->engine)
1303 engine_open (self);
1305 if (!self->codec)
1306 codec_create (self);
1308 if (self->codec) {
1309 version = dce_alloc (VERSION_LENGTH);
1310 self->status->data.buf = (XDAS_Int8 *) version;
1311 self->status->data.bufSize = VERSION_LENGTH;
1313 err = VIDDEC3_control (self->codec, XDM_GETVERSION,
1314 self->dynParams, self->status);
1315 if (err) {
1316 GST_ERROR_OBJECT (self, "failed XDM_GETVERSION");
1317 }
1319 self->status->data.buf = NULL;
1320 self->status->data.bufSize = 0;
1321 }
1323 g_value_set_string (value, version);
1324 if (version)
1325 dce_free (version);
1327 break;
1328 }
1329 default:{
1330 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1331 break;
1332 }
1333 }
1334 }
1336 static void
1337 gst_ducati_viddec_finalize (GObject * obj)
1338 {
1339 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1341 codec_delete (self);
1342 engine_close (self);
1344 if (self->codec_data) {
1345 gst_buffer_unref (self->codec_data);
1346 self->codec_data = NULL;
1347 }
1349 G_OBJECT_CLASS (parent_class)->finalize (obj);
1350 }
1352 static void
1353 gst_ducati_viddec_base_init (gpointer gclass)
1354 {
1355 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
1357 gst_element_class_add_pad_template (element_class,
1358 gst_static_pad_template_get (&src_factory));
1359 }
1361 static void
1362 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
1363 {
1364 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1365 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1367 gobject_class->get_property =
1368 GST_DEBUG_FUNCPTR (gst_ducati_viddec_get_property);
1369 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ducati_viddec_finalize);
1370 gstelement_class->change_state =
1371 GST_DEBUG_FUNCPTR (gst_ducati_viddec_change_state);
1373 klass->parse_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_parse_caps);
1374 klass->allocate_params =
1375 GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
1376 klass->push_input = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
1377 klass->handle_error = GST_DEBUG_FUNCPTR (gst_ducati_viddec_handle_error);
1378 klass->can_drop_frame = GST_DEBUG_FUNCPTR (gst_ducati_viddec_can_drop_frame);
1379 klass->query = GST_DEBUG_FUNCPTR (gst_ducati_viddec_query);
1380 klass->push_output = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_output);
1381 klass->on_flush = GST_DEBUG_FUNCPTR (gst_ducati_viddec_on_flush);
1382 klass->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_sink_caps);
1384 g_object_class_install_property (gobject_class, PROP_VERSION,
1385 g_param_spec_string ("version", "Version",
1386 "The codec version string", "",
1387 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1388 }
1390 static void
1391 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
1392 {
1393 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1395 self->sinkpad =
1396 gst_pad_new_from_template (gst_element_class_get_pad_template
1397 (gstelement_class, "sink"), "sink");
1398 gst_pad_set_setcaps_function (self->sinkpad,
1399 GST_DEBUG_FUNCPTR (gst_ducati_viddec_sink_setcaps));
1400 gst_pad_set_chain_function (self->sinkpad,
1401 GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
1402 gst_pad_set_event_function (self->sinkpad,
1403 GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
1405 self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
1406 gst_pad_set_event_function (self->srcpad,
1407 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_event));
1408 gst_pad_set_query_function (self->srcpad,
1409 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_query));
1410 gst_pad_set_getcaps_function (self->srcpad,
1411 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_getcaps));
1413 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
1414 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1416 self->input_width = 0;
1417 self->input_height = 0;
1418 /* sane defaults in case we need to create codec without caps negotiation
1419 * (for example, to get 'version' property)
1420 */
1421 self->width = 128;
1422 self->height = 128;
1423 self->fps_n = -1;
1424 self->fps_d = -1;
1426 self->first_in_buffer = TRUE;
1427 self->first_out_buffer = TRUE;
1428 self->interlaced = FALSE;
1429 self->send_crop_event = TRUE;
1431 #ifdef USE_DTS_PTS_CODE
1432 self->dts_ridx = self->dts_widx = 0;
1433 self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
1434 self->ts_may_be_pts = TRUE;
1435 self->ts_is_pts = FALSE;
1436 #endif
1438 self->pageMemType = XDM_MEMTYPE_TILEDPAGE;
1440 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1442 self->qos_proportion = 1;
1443 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1444 self->wait_keyframe = TRUE;
1446 self->need_out_buf = TRUE;
1447 self->device = NULL;
1448 self->input_bo = NULL;
1449 }