987dc403ba9a705a8c90b854a948561d03239a59
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;
271 }
273 static GstBuffer *
274 codec_get_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
275 {
276 GstBuffer *buf = (GstBuffer *) id;
278 if (buf) {
279 g_hash_table_insert (self->passed_in_bufs, buf, buf);
281 gst_buffer_ref (buf);
282 }
283 return buf;
284 }
286 static void
287 codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
288 {
289 GstBuffer *buf = (GstBuffer *) id;
291 if (buf) {
292 GST_DEBUG_OBJECT (self, "free buffer: %d %p", id, buf);
293 g_hash_table_remove (self->passed_in_bufs, buf);
294 }
295 }
297 static void
298 gst_ducati_viddec_on_flush (GstDucatiVidDec * self, gboolean eos)
299 {
300 }
302 static gint
303 codec_process (GstDucatiVidDec * self, gboolean send, gboolean flush,
304 GstFlowReturn * flow_ret)
305 {
306 gint err;
307 GstClockTime t;
308 GstBuffer *outbuf = NULL;
309 gint i;
310 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
311 GstFlowReturn ret = GST_FLOW_OK;
312 if (flow_ret)
313 /* never leave flow_ret uninitialized */
314 *flow_ret = GST_FLOW_OK;
316 memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
317 memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
319 t = gst_util_get_timestamp ();
320 err = VIDDEC3_process (self->codec,
321 self->inBufs, self->outBufs, self->inArgs, self->outArgs);
322 GST_DEBUG_OBJECT (self, "%10dns", (gint) (gst_util_get_timestamp () - t));
324 if (err) {
325 GST_WARNING_OBJECT (self, "err=%d, extendedError=%08x",
326 err, self->outArgs->extendedError);
327 gst_ducati_log_extended_error_info (self->outArgs->extendedError);
329 err = VIDDEC3_control (self->codec, XDM_GETSTATUS,
330 self->dynParams, self->status);
331 if (err) {
332 GST_WARNING_OBJECT (self, "XDM_GETSTATUS: err=%d, extendedError=%08x",
333 err, self->status->extendedError);
334 gst_ducati_log_extended_error_info (self->status->extendedError);
335 }
337 if (flush)
338 err = XDM_EFAIL;
339 else
340 err = klass->handle_error (self, err,
341 self->outArgs->extendedError, self->status->extendedError);
342 }
344 /* we now let the codec decide */
345 self->dynParams->newFrameFlag = XDAS_FALSE;
347 if (err == XDM_EFAIL)
348 goto skip_outbuf_processing;
350 for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->outputID[i]; i++) {
351 gboolean interlaced;
353 /* Getting an extra reference for the decoder */
354 outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
355 interlaced =
356 self->outArgs->decodedBufs.contentType ==
357 IVIDEO_PROGRESSIVE ? FALSE : TRUE;
359 /* if send is FALSE, don't try to renegotiate as we could be flushing during
360 * a PAUSED->READY state change
361 */
362 if (send && interlaced != self->interlaced) {
363 GstCaps *caps;
365 GST_WARNING_OBJECT (self, "upstream set interlaced=%d but codec "
366 "thinks interlaced=%d... trusting codec", self->interlaced,
367 interlaced);
369 self->interlaced = interlaced;
371 caps =
372 gst_caps_make_writable (gst_pad_get_negotiated_caps (self->srcpad));
373 GST_INFO_OBJECT (self, "changing interlace field in caps");
374 gst_caps_set_simple (caps, "interlaced", G_TYPE_BOOLEAN, interlaced,
375 NULL);
376 gst_drm_buffer_pool_set_caps (self->pool, caps);
377 if (!gst_pad_set_caps (self->srcpad, caps)) {
378 GST_ERROR_OBJECT (self,
379 "downstream didn't want to change interlace mode");
380 err = XDM_EFAIL;
381 }
382 gst_caps_unref (caps);
384 /* this buffer still has the old caps so we skip it */
385 send = FALSE;
386 }
388 if (G_UNLIKELY (self->send_crop_event) && send) {
389 gint crop_width, crop_height;
391 /* send region of interest to sink on first buffer: */
392 XDM_Rect *r = &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
394 crop_width = r->bottomRight.x - r->topLeft.x;
395 crop_height = r->bottomRight.y - r->topLeft.y;
397 if (crop_width > self->input_width)
398 crop_width = self->input_width;
399 if (crop_height > self->input_height)
400 crop_height = self->input_height;
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, "Buffer not pushed, dropping 'chain' ref: %d %p",
487 i, outbuf);
489 gst_buffer_unref (outbuf);
490 }
491 }
493 skip_outbuf_processing:
494 for (i = 0; i < IVIDEO2_MAX_IO_BUFFERS && self->outArgs->freeBufID[i]; i++) {
495 codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
496 }
498 return err;
499 }
501 /** call control(FLUSH), and then process() to pop out all buffers */
502 gboolean
503 gst_ducati_viddec_codec_flush (GstDucatiVidDec * self, gboolean eos)
504 {
505 gint err = FALSE;
507 GST_DEBUG_OBJECT (self, "flush: eos=%d", eos);
509 GST_DUCATIVIDDEC_GET_CLASS (self)->on_flush (self, eos);
511 /* note: flush is synchronized against _chain() to avoid calling
512 * the codec from multiple threads
513 */
514 GST_PAD_STREAM_LOCK (self->sinkpad);
516 #ifdef USE_DTS_PTS_CODE
517 self->dts_ridx = self->dts_widx = 0;
518 self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
519 self->ts_may_be_pts = TRUE;
520 self->ts_is_pts = FALSE;
521 #endif
522 self->wait_keyframe = TRUE;
523 self->in_size = 0;
524 self->needs_flushing = FALSE;
525 self->need_out_buf = TRUE;
527 if (G_UNLIKELY (self->first_in_buffer)) {
528 goto out;
529 }
531 if (G_UNLIKELY (!self->codec)) {
532 GST_WARNING_OBJECT (self, "no codec");
533 goto out;
534 }
536 err = VIDDEC3_control (self->codec, XDM_FLUSH, self->dynParams, self->status);
537 if (err) {
538 GST_ERROR_OBJECT (self, "failed XDM_FLUSH");
539 goto out;
540 }
542 self->inBufs->descs[0].bufSize.bytes = 0;
543 self->inArgs->numBytes = 0;
544 self->inArgs->inputID = 0;
546 do {
547 err = codec_process (self, eos, TRUE, NULL);
548 } while (err != XDM_EFAIL);
550 /* We flushed the decoder, we can now remove the buffer that have never been
551 * unrefed in it */
552 g_hash_table_remove_all (self->passed_in_bufs);
554 /* reset outArgs in case we're flushing in codec_process trying to do error
555 * recovery */
556 memset (&self->outArgs->outputID, 0, sizeof (self->outArgs->outputID));
557 memset (&self->outArgs->freeBufID, 0, sizeof (self->outArgs->freeBufID));
559 self->dynParams->newFrameFlag = XDAS_TRUE;
561 /* on a flush, it is normal (and not an error) for the last _process() call
562 * to return an error..
563 */
564 err = XDM_EOK;
566 out:
567 GST_PAD_STREAM_UNLOCK (self->sinkpad);
568 GST_DEBUG_OBJECT (self, "done");
570 return !err;
571 }
573 /* GstDucatiVidDec vmethod default implementations */
575 static gboolean
576 gst_ducati_viddec_parse_caps (GstDucatiVidDec * self, GstStructure * s)
577 {
578 const GValue *codec_data;
579 gint w, h;
581 if (gst_structure_get_int (s, "width", &self->input_width) &&
582 gst_structure_get_int (s, "height", &self->input_height)) {
584 h = ALIGN2 (self->input_height, 4); /* round up to MB */
585 w = ALIGN2 (self->input_width, 4); /* round up to MB */
587 /* if we've already created codec, but the resolution has changed, we
588 * need to re-create the codec:
589 */
590 if (G_UNLIKELY (self->codec)) {
591 if ((h != self->height) || (w != self->width)) {
592 codec_delete (self);
593 }
594 }
596 self->width = w;
597 self->height = h;
599 codec_data = gst_structure_get_value (s, "codec_data");
601 if (codec_data) {
602 GstBuffer *buffer = gst_value_get_buffer (codec_data);
603 GST_DEBUG_OBJECT (self, "codec_data: %" GST_PTR_FORMAT, buffer);
604 self->codec_data = gst_buffer_ref (buffer);
605 }
607 return TRUE;
608 }
610 return FALSE;
611 }
613 static gboolean
614 gst_ducati_viddec_allocate_params (GstDucatiVidDec * self, gint params_sz,
615 gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz)
616 {
618 /* allocate params: */
619 self->params = dce_alloc (params_sz);
620 if (G_UNLIKELY (!self->params)) {
621 return FALSE;
622 }
623 self->params->size = params_sz;
624 self->params->maxFrameRate = 30000;
625 self->params->maxBitRate = 10000000;
627 self->params->dataEndianness = XDM_BYTE;
628 self->params->forceChromaFormat = XDM_YUV_420SP;
629 self->params->operatingMode = IVIDEO_DECODE_ONLY;
631 self->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
632 self->params->inputDataMode = IVIDEO_ENTIREFRAME;
633 self->params->outputDataMode = IVIDEO_ENTIREFRAME;
634 self->params->numInputDataUnits = 0;
635 self->params->numOutputDataUnits = 0;
637 self->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
638 self->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
639 self->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
640 self->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
642 /* allocate dynParams: */
643 self->dynParams = dce_alloc (dynparams_sz);
644 if (G_UNLIKELY (!self->dynParams)) {
645 return FALSE;
646 }
647 self->dynParams->size = dynparams_sz;
648 self->dynParams->decodeHeader = XDM_DECODE_AU;
649 self->dynParams->displayWidth = 0;
650 self->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
651 self->dynParams->newFrameFlag = XDAS_TRUE;
653 /* allocate status: */
654 self->status = dce_alloc (status_sz);
655 if (G_UNLIKELY (!self->status)) {
656 return FALSE;
657 }
658 memset (self->status, 0, status_sz);
659 self->status->size = status_sz;
661 /* allocate inBufs/outBufs: */
662 self->inBufs = dce_alloc (sizeof (XDM2_BufDesc));
663 self->outBufs = dce_alloc (sizeof (XDM2_BufDesc));
664 if (G_UNLIKELY (!self->inBufs) || G_UNLIKELY (!self->outBufs)) {
665 return FALSE;
666 }
668 /* allocate inArgs/outArgs: */
669 self->inArgs = dce_alloc (inargs_sz);
670 self->outArgs = dce_alloc (outargs_sz);
671 if (G_UNLIKELY (!self->inArgs) || G_UNLIKELY (!self->outArgs)) {
672 return FALSE;
673 }
674 self->inArgs->size = inargs_sz;
675 self->outArgs->size = outargs_sz;
677 return TRUE;
678 }
680 static GstBuffer *
681 gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
682 {
683 if (G_UNLIKELY (self->first_in_buffer) && self->codec_data) {
684 push_input (self, GST_BUFFER_DATA (self->codec_data),
685 GST_BUFFER_SIZE (self->codec_data));
686 }
688 /* just copy entire buffer */
689 push_input (self, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
690 gst_buffer_unref (buf);
692 return NULL;
693 }
695 static GstFlowReturn
696 gst_ducati_viddec_push_output (GstDucatiVidDec * self, GstBuffer * buf)
697 {
698 return gst_pad_push (self->srcpad, buf);
699 }
701 static gint
702 gst_ducati_viddec_handle_error (GstDucatiVidDec * self, gint ret,
703 gint extended_error, gint status_extended_error)
704 {
705 if (XDM_ISFATALERROR (extended_error))
706 ret = XDM_EFAIL;
707 else
708 ret = XDM_EOK;
710 return ret;
711 }
713 /* GstElement vmethod implementations */
715 static gboolean
716 gst_ducati_viddec_set_sink_caps (GstDucatiVidDec * self, GstCaps * caps)
717 {
718 gboolean ret = TRUE;
719 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
720 GstStructure *s;
721 GstCaps *outcaps = NULL;
722 GstStructure *out_s;
723 gint par_width, par_height;
724 gboolean par_present;
726 s = gst_caps_get_structure (caps, 0);
727 if (!klass->parse_caps (self, s)) {
728 GST_WARNING_OBJECT (self, "missing required fields");
729 ret = FALSE;
730 goto out;
731 }
733 /* update output/padded sizes */
734 klass->update_buffer_size (self);
736 if (!gst_structure_get_fraction (s, "framerate", &self->fps_n, &self->fps_d)) {
737 self->fps_n = 0;
738 self->fps_d = 1;
739 }
740 gst_structure_get_boolean (s, "interlaced", &self->interlaced);
741 par_present = gst_structure_get_fraction (s, "pixel-aspect-ratio",
742 &par_width, &par_height);
744 outcaps = gst_pad_get_allowed_caps (self->srcpad);
745 if (outcaps) {
746 outcaps = gst_caps_make_writable (outcaps);
747 gst_caps_truncate (outcaps);
748 if (gst_caps_is_empty (outcaps)) {
749 gst_caps_unref (outcaps);
750 outcaps = NULL;
751 }
752 }
754 if (!outcaps) {
755 /* note: default to non-strided for better compatibility with
756 * other gst elements that don't understand stride:
757 */
758 outcaps = gst_caps_new_simple ("video/x-raw-yuv",
759 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N', 'V', '1', '2'), NULL);
760 }
762 out_s = gst_caps_get_structure (outcaps, 0);
763 gst_structure_set (out_s,
764 "width", G_TYPE_INT, self->padded_width,
765 "height", G_TYPE_INT, self->padded_height,
766 "framerate", GST_TYPE_FRACTION, self->fps_n, self->fps_d, NULL);
767 if (par_present)
768 gst_structure_set (out_s, "pixel-aspect-ratio", GST_TYPE_FRACTION,
769 par_width, par_height, NULL);
771 if (self->interlaced)
772 gst_structure_set (out_s, "interlaced", G_TYPE_BOOLEAN, TRUE, NULL);
774 if (!strcmp (gst_structure_get_name (out_s), "video/x-raw-yuv-strided")) {
775 if (!gst_structure_get_int (out_s, "rowstride", &self->stride)) {
776 self->stride = 4096;
777 gst_structure_set (out_s, "rowstride", G_TYPE_INT, self->stride, NULL);
778 }
779 } else {
780 self->stride = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_NV12,
781 0, self->padded_width);
782 }
784 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
785 self->stride, self->padded_height);
787 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
788 self->outsize, self->stride, outcaps);
790 if (!self->first_in_buffer) {
791 /* Caps changed mid stream. We flush the codec to unlock all the potentially
792 * locked buffers. This is needed for downstream sinks that provide a
793 * buffer pool and need to destroy all the outstanding buffers before they
794 * can negotiate new caps (hello v4l2sink).
795 */
796 gst_ducati_viddec_codec_flush (self, FALSE);
797 }
799 /* (re)send a crop event when caps change */
800 self->send_crop_event = TRUE;
802 ret = gst_pad_set_caps (self->srcpad, outcaps);
804 GST_INFO_OBJECT (self, "set caps done %d, %" GST_PTR_FORMAT, ret, outcaps);
806 out:
807 if (outcaps)
808 gst_caps_unref (outcaps);
810 return ret;
811 }
813 static gboolean
814 gst_ducati_viddec_sink_setcaps (GstPad * pad, GstCaps * caps)
815 {
816 gboolean ret = TRUE;
817 GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
818 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
820 GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
822 ret = klass->set_sink_caps (self, caps);
824 gst_object_unref (self);
826 return ret;
827 }
829 static GstCaps *
830 gst_ducati_viddec_src_getcaps (GstPad * pad)
831 {
832 GstCaps *caps = NULL;
833 GstCaps *outcaps = NULL;
834 int i;
836 caps = GST_PAD_CAPS (pad);
837 if (caps == NULL) {
838 outcaps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
839 return outcaps;
840 }
842 outcaps = gst_caps_new_empty ();
844 /* allow -rowstrided and regular yuv */
845 for (i = 0; i < gst_caps_get_size (caps); i++) {
846 GstStructure *structure;
847 GstStructure *modified_structure;
848 GValue value = { 0 };
850 structure = gst_caps_get_structure (caps, i);
851 gst_caps_append_structure (outcaps, gst_structure_copy (structure));
852 modified_structure = gst_structure_copy (structure);
854 if (gst_structure_has_name (structure, "video/x-raw-yuv")) {
855 gst_structure_set_name (modified_structure, "video/x-raw-yuv-strided");
856 g_value_init (&value, GST_TYPE_INT_RANGE);
857 gst_value_set_int_range (&value, 0, G_MAXINT);
858 gst_structure_set_value (modified_structure, "rowstride", &value);
859 gst_caps_append_structure (outcaps, modified_structure);
860 g_value_unset (&value);
861 } else {
862 gst_structure_set_name (modified_structure, "video/x-raw-yuv");
863 gst_structure_remove_field (modified_structure, "rowstride");
864 gst_caps_append_structure (outcaps, modified_structure);
865 }
866 }
867 return outcaps;
868 }
870 static gboolean
871 gst_ducati_viddec_query (GstDucatiVidDec * self, GstPad * pad,
872 GstQuery * query, gboolean * forward)
873 {
874 gboolean res = TRUE;
876 switch (GST_QUERY_TYPE (query)) {
877 case GST_QUERY_BUFFERS:
878 GST_DEBUG_OBJECT (self, "min buffers: %d", self->min_buffers);
879 gst_query_set_buffers_count (query, self->min_buffers);
881 GST_DEBUG_OBJECT (self, "min dimensions: %dx%d",
882 self->padded_width, self->padded_height);
883 gst_query_set_buffers_dimensions (query,
884 self->padded_width, self->padded_height);
885 *forward = FALSE;
886 break;
887 default:
888 break;
889 }
892 return res;
893 }
895 static gboolean
896 gst_ducati_viddec_src_query (GstPad * pad, GstQuery * query)
897 {
898 gboolean res = TRUE, forward = TRUE;
899 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
900 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
902 GST_DEBUG_OBJECT (self, "query: %" GST_PTR_FORMAT, query);
903 res = klass->query (self, pad, query, &forward);
904 if (res && forward)
905 res = gst_pad_query_default (pad, query);
907 return res;
908 }
910 static gboolean
911 gst_ducati_viddec_do_qos (GstDucatiVidDec * self, GstBuffer * buf)
912 {
913 GstClockTime timestamp, qostime;
914 GstDucatiVidDecClass *klass = GST_DUCATIVIDDEC_GET_CLASS (self);
915 gint64 diff;
917 if (self->wait_keyframe) {
918 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
919 GST_INFO_OBJECT (self, "skipping until the next keyframe");
920 return FALSE;
921 }
923 self->wait_keyframe = FALSE;
924 }
926 timestamp = GST_BUFFER_TIMESTAMP (buf);
927 if (self->segment.format != GST_FORMAT_TIME ||
928 self->qos_earliest_time == GST_CLOCK_TIME_NONE)
929 goto no_qos;
931 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (timestamp)))
932 goto no_qos;
934 qostime = gst_segment_to_running_time (&self->segment,
935 GST_FORMAT_TIME, timestamp);
936 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (qostime)))
937 /* out of segment */
938 goto no_qos;
940 /* see how our next timestamp relates to the latest qos timestamp. negative
941 * values mean we are early, positive values mean we are too late. */
942 diff = GST_CLOCK_DIFF (qostime, self->qos_earliest_time);
944 GST_DEBUG_OBJECT (self, "QOS: qostime %" GST_TIME_FORMAT
945 ", earliest %" GST_TIME_FORMAT " diff %" G_GINT64_FORMAT " proportion %f",
946 GST_TIME_ARGS (qostime), GST_TIME_ARGS (self->qos_earliest_time), diff,
947 self->qos_proportion);
949 if (klass->can_drop_frame (self, buf, diff)) {
950 GST_INFO_OBJECT (self, "dropping frame");
951 return FALSE;
952 }
954 no_qos:
955 return TRUE;
956 }
958 static gboolean
959 gst_ducati_viddec_can_drop_frame (GstDucatiVidDec * self, GstBuffer * buf,
960 gint64 diff)
961 {
962 gboolean is_keyframe = !GST_BUFFER_FLAG_IS_SET (buf,
963 GST_BUFFER_FLAG_DELTA_UNIT);
965 if (diff >= 0 && !is_keyframe)
966 return TRUE;
968 return FALSE;
969 }
971 static GstFlowReturn
972 gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
973 {
974 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
975 GstClockTime ts = GST_BUFFER_TIMESTAMP (buf);
976 GstFlowReturn ret = GST_FLOW_OK;
977 Int32 err;
978 GstBuffer *outbuf = NULL;
979 GstCaps *outcaps = NULL;
980 gboolean decode;
982 if (G_UNLIKELY (!self->engine)) {
983 GST_ERROR_OBJECT (self, "no engine");
984 gst_buffer_unref (buf);
985 return GST_FLOW_ERROR;
986 }
988 GST_DEBUG_OBJECT (self, "chain: %" GST_TIME_FORMAT " (%d bytes, flags %d)",
989 GST_TIME_ARGS (ts), GST_BUFFER_SIZE (buf), GST_BUFFER_FLAGS (buf));
991 decode = gst_ducati_viddec_do_qos (self, buf);
992 if (!decode) {
993 gst_buffer_unref (buf);
994 return GST_FLOW_OK;
995 }
997 if (!self->need_out_buf)
998 goto have_out_buf;
1000 /* do this before creating codec to ensure reverse caps negotiation
1001 * happens first:
1002 */
1003 allocate_buffer:
1004 ret = gst_pad_alloc_buffer (self->srcpad, 0, self->outsize,
1005 GST_PAD_CAPS (self->srcpad), &outbuf);
1006 if (ret != GST_FLOW_OK) {
1007 GST_WARNING_OBJECT (self, "alloc_buffer failed %s",
1008 gst_flow_get_name (ret));
1009 gst_buffer_unref (buf);
1010 return ret;
1011 }
1013 outcaps = GST_BUFFER_CAPS (outbuf);
1014 if (outcaps && !gst_caps_is_equal (outcaps, GST_PAD_CAPS (self->srcpad))) {
1015 GstStructure *s;
1017 GST_INFO_OBJECT (self, "doing upstream negotiation bufsize %d",
1018 GST_BUFFER_SIZE (outbuf));
1020 s = gst_caps_get_structure (outcaps, 0);
1021 gst_structure_get_int (s, "rowstride", &self->stride);
1022 self->outsize = gst_video_format_get_size (GST_VIDEO_FORMAT_NV12,
1023 self->stride, self->padded_height);
1025 GST_INFO_OBJECT (self, "outsize %d stride %d outcaps: %" GST_PTR_FORMAT,
1026 self->outsize, self->stride, outcaps);
1028 gst_pad_set_caps (self->srcpad, outcaps);
1030 if (GST_BUFFER_SIZE (outbuf) != self->outsize) {
1031 GST_INFO_OBJECT (self, "dropping buffer (bufsize %d != outsize %d)",
1032 GST_BUFFER_SIZE (outbuf), self->outsize);
1033 gst_buffer_unref (outbuf);
1034 goto allocate_buffer;
1035 }
1036 }
1038 if (G_UNLIKELY (!self->codec)) {
1039 if (!codec_create (self)) {
1040 GST_ERROR_OBJECT (self, "could not create codec");
1041 gst_buffer_unref (buf);
1042 gst_buffer_unref (outbuf);
1043 return GST_FLOW_ERROR;
1044 }
1045 }
1047 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
1048 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
1050 /* Pass new output buffer to the decoder to decode into. Use buffers from the
1051 * internal pool while self->first_out_buffer == TRUE in order to simplify
1052 * things in case we need to renegotiate */
1053 self->inArgs->inputID =
1054 codec_prepare_outbuf (self, &outbuf, self->first_out_buffer);
1055 if (!self->inArgs->inputID) {
1056 GST_ERROR_OBJECT (self, "could not prepare output buffer");
1057 gst_buffer_unref (buf);
1058 return GST_FLOW_ERROR;
1059 }
1061 have_out_buf:
1062 buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
1064 #ifdef USE_DTS_PTS_CODE
1065 if (ts != GST_CLOCK_TIME_NONE) {
1066 self->dts_queue[self->dts_widx++ % NDTS] = ts;
1067 /* if next buffer has earlier ts than previous, then the ts
1068 * we are getting are definitely decode order (DTS):
1069 */
1070 if ((self->last_dts != GST_CLOCK_TIME_NONE) && (self->last_dts > ts)) {
1071 GST_DEBUG_OBJECT (self, "input timestamp definitely DTS");
1072 self->ts_may_be_pts = FALSE;
1073 }
1074 self->last_dts = ts;
1075 }
1076 #endif
1078 if (self->in_size == 0 && outbuf) {
1079 GST_DEBUG_OBJECT (self, "no input, skipping process");
1081 gst_buffer_unref (outbuf);
1082 return GST_FLOW_OK;
1083 }
1085 self->inArgs->numBytes = self->in_size;
1086 self->inBufs->descs[0].bufSize.bytes = self->in_size;
1087 self->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
1089 err = codec_process (self, TRUE, FALSE, &ret);
1090 if (err) {
1091 GST_ELEMENT_ERROR (self, STREAM, DECODE, (NULL),
1092 ("process returned error: %d %08x", err, self->outArgs->extendedError));
1093 gst_ducati_log_extended_error_info (self->outArgs->extendedError);
1095 return GST_FLOW_ERROR;
1096 }
1098 if (ret != GST_FLOW_OK) {
1099 GST_WARNING_OBJECT (self, "push from codec_process failed %s",
1100 gst_flow_get_name (ret));
1102 return ret;
1103 }
1105 self->first_in_buffer = FALSE;
1107 if (self->params->inputDataMode != IVIDEO_ENTIREFRAME) {
1108 /* The copy could be avoided by playing with the buffer pointer,
1109 but it seems to be rare and for not many bytes */
1110 GST_DEBUG_OBJECT (self, "Consumed %d/%d (%d) bytes, %d left",
1111 self->outArgs->bytesConsumed, self->in_size,
1112 self->inArgs->numBytes, self->in_size - self->outArgs->bytesConsumed);
1113 if (self->outArgs->bytesConsumed > 0) {
1114 if (self->outArgs->bytesConsumed > self->in_size) {
1115 GST_WARNING_OBJECT (self,
1116 "Codec claims to have used more bytes than supplied");
1117 self->in_size = 0;
1118 } else {
1119 if (self->outArgs->bytesConsumed < self->in_size) {
1120 memmove (self->input, self->input + self->outArgs->bytesConsumed,
1121 self->in_size - self->outArgs->bytesConsumed);
1122 }
1123 self->in_size -= self->outArgs->bytesConsumed;
1124 }
1125 }
1126 } else {
1127 self->in_size = 0;
1128 }
1130 if (self->outArgs->outBufsInUseFlag) {
1131 GST_DEBUG_OBJECT (self, "outBufsInUseFlag set");
1132 self->need_out_buf = FALSE;
1133 } else {
1134 self->need_out_buf = TRUE;
1135 }
1137 if (buf) {
1138 GST_DEBUG_OBJECT (self, "found remaining data: %d bytes",
1139 GST_BUFFER_SIZE (buf));
1140 ts = GST_BUFFER_TIMESTAMP (buf);
1141 goto allocate_buffer;
1142 }
1144 if (self->needs_flushing)
1145 gst_ducati_viddec_codec_flush (self, FALSE);
1147 return GST_FLOW_OK;
1148 }
1150 static gboolean
1151 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
1152 {
1153 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1154 gboolean ret = TRUE;
1156 GST_DEBUG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1158 switch (GST_EVENT_TYPE (event)) {
1159 case GST_EVENT_NEWSEGMENT:
1160 {
1161 gboolean update;
1162 GstFormat fmt;
1163 gint64 start, stop, time;
1164 gdouble rate, arate;
1166 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &fmt,
1167 &start, &stop, &time);
1168 gst_segment_set_newsegment_full (&self->segment, update,
1169 rate, arate, fmt, start, stop, time);
1170 break;
1171 }
1172 case GST_EVENT_EOS:
1173 if (!gst_ducati_viddec_codec_flush (self, TRUE)) {
1174 GST_ERROR_OBJECT (self, "could not flush on eos");
1175 ret = FALSE;
1176 }
1177 break;
1178 case GST_EVENT_FLUSH_STOP:
1179 if (!gst_ducati_viddec_codec_flush (self, FALSE)) {
1180 GST_ERROR_OBJECT (self, "could not flush");
1181 gst_event_unref (event);
1182 ret = FALSE;
1183 }
1184 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1185 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1186 self->qos_proportion = 1;
1187 self->need_out_buf = TRUE;
1188 break;
1189 default:
1190 break;
1191 }
1193 if (ret)
1194 ret = gst_pad_push_event (self->srcpad, event);
1195 GST_LOG_OBJECT (self, "end");
1197 return ret;
1198 }
1200 static gboolean
1201 gst_ducati_viddec_src_event (GstPad * pad, GstEvent * event)
1202 {
1203 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
1204 gboolean ret = TRUE;
1206 GST_LOG_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
1208 switch (GST_EVENT_TYPE (event)) {
1209 case GST_EVENT_QOS:
1210 {
1211 gdouble proportion;
1212 GstClockTimeDiff diff;
1213 GstClockTime timestamp;
1215 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
1217 GST_OBJECT_LOCK (self);
1218 self->qos_proportion = proportion;
1219 self->qos_earliest_time = timestamp + 2 * diff;
1220 GST_OBJECT_UNLOCK (self);
1222 GST_DEBUG_OBJECT (self,
1223 "got QoS proportion %f %" GST_TIME_FORMAT ", %" G_GINT64_FORMAT,
1224 proportion, GST_TIME_ARGS (timestamp), diff);
1226 ret = gst_pad_push_event (self->sinkpad, event);
1227 break;
1228 }
1229 default:
1230 ret = gst_pad_push_event (self->sinkpad, event);
1231 break;
1232 }
1234 GST_LOG_OBJECT (self, "end");
1236 return ret;
1237 }
1239 static GstStateChangeReturn
1240 gst_ducati_viddec_change_state (GstElement * element, GstStateChange transition)
1241 {
1242 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1243 GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
1244 gboolean supported;
1246 GST_DEBUG_OBJECT (self, "begin: changing state %s -> %s",
1247 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1248 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1250 switch (transition) {
1251 case GST_STATE_CHANGE_NULL_TO_READY:
1252 if (!engine_open (self)) {
1253 GST_ERROR_OBJECT (self, "could not open");
1254 return GST_STATE_CHANGE_FAILURE;
1255 }
1256 /* try to create/destroy the codec here, it may not be supported */
1257 supported = codec_create (self);
1258 codec_delete (self);
1259 self->codec = NULL;
1260 if (!supported) {
1261 GST_ERROR_OBJECT (element, "Failed to create codec %s, not supported",
1262 GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name);
1263 engine_close (self);
1264 return GST_STATE_CHANGE_FAILURE;
1265 }
1266 break;
1267 default:
1268 break;
1269 }
1271 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1273 if (ret == GST_STATE_CHANGE_FAILURE)
1274 goto leave;
1276 switch (transition) {
1277 case GST_STATE_CHANGE_PAUSED_TO_READY:
1278 self->interlaced = FALSE;
1279 self->send_crop_event = TRUE;
1280 gst_ducati_viddec_codec_flush (self, FALSE);
1281 break;
1282 case GST_STATE_CHANGE_READY_TO_NULL:
1283 codec_delete (self);
1284 engine_close (self);
1285 break;
1286 default:
1287 break;
1288 }
1290 leave:
1291 GST_LOG_OBJECT (self, "end");
1293 return ret;
1294 }
1296 /* GObject vmethod implementations */
1298 #define VERSION_LENGTH 256
1300 static void
1301 gst_ducati_viddec_get_property (GObject * obj,
1302 guint prop_id, GValue * value, GParamSpec * pspec)
1303 {
1304 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1307 switch (prop_id) {
1308 case PROP_VERSION:{
1309 int err;
1310 char *version = NULL;
1312 if (!self->engine)
1313 engine_open (self);
1315 if (!self->codec)
1316 codec_create (self);
1318 if (self->codec) {
1319 version = dce_alloc (VERSION_LENGTH);
1320 self->status->data.buf = (XDAS_Int8 *) version;
1321 self->status->data.bufSize = VERSION_LENGTH;
1323 err = VIDDEC3_control (self->codec, XDM_GETVERSION,
1324 self->dynParams, self->status);
1325 if (err) {
1326 GST_ERROR_OBJECT (self, "failed XDM_GETVERSION");
1327 }
1329 self->status->data.buf = NULL;
1330 self->status->data.bufSize = 0;
1331 }
1333 g_value_set_string (value, version);
1334 if (version)
1335 dce_free (version);
1337 break;
1338 }
1339 default:{
1340 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
1341 break;
1342 }
1343 }
1344 }
1346 static void
1347 gst_ducati_viddec_finalize (GObject * obj)
1348 {
1349 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
1351 codec_delete (self);
1352 engine_close (self);
1354 /* Will unref the remaining buffers if needed */
1355 g_hash_table_unref (self->passed_in_bufs);
1356 if (self->codec_data) {
1357 gst_buffer_unref (self->codec_data);
1358 self->codec_data = NULL;
1359 }
1361 G_OBJECT_CLASS (parent_class)->finalize (obj);
1362 }
1364 static void
1365 gst_ducati_viddec_base_init (gpointer gclass)
1366 {
1367 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
1369 gst_element_class_add_pad_template (element_class,
1370 gst_static_pad_template_get (&src_factory));
1371 }
1373 static void
1374 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
1375 {
1376 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1377 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1379 gobject_class->get_property =
1380 GST_DEBUG_FUNCPTR (gst_ducati_viddec_get_property);
1381 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ducati_viddec_finalize);
1382 gstelement_class->change_state =
1383 GST_DEBUG_FUNCPTR (gst_ducati_viddec_change_state);
1385 klass->parse_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_parse_caps);
1386 klass->allocate_params =
1387 GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
1388 klass->push_input = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
1389 klass->handle_error = GST_DEBUG_FUNCPTR (gst_ducati_viddec_handle_error);
1390 klass->can_drop_frame = GST_DEBUG_FUNCPTR (gst_ducati_viddec_can_drop_frame);
1391 klass->query = GST_DEBUG_FUNCPTR (gst_ducati_viddec_query);
1392 klass->push_output = GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_output);
1393 klass->on_flush = GST_DEBUG_FUNCPTR (gst_ducati_viddec_on_flush);
1394 klass->set_sink_caps = GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_sink_caps);
1396 g_object_class_install_property (gobject_class, PROP_VERSION,
1397 g_param_spec_string ("version", "Version",
1398 "The codec version string", "",
1399 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1400 }
1402 static void
1403 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
1404 {
1405 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1407 self->sinkpad =
1408 gst_pad_new_from_template (gst_element_class_get_pad_template
1409 (gstelement_class, "sink"), "sink");
1410 gst_pad_set_setcaps_function (self->sinkpad,
1411 GST_DEBUG_FUNCPTR (gst_ducati_viddec_sink_setcaps));
1412 gst_pad_set_chain_function (self->sinkpad,
1413 GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
1414 gst_pad_set_event_function (self->sinkpad,
1415 GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
1417 self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
1418 gst_pad_set_event_function (self->srcpad,
1419 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_event));
1420 gst_pad_set_query_function (self->srcpad,
1421 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_query));
1422 gst_pad_set_getcaps_function (self->srcpad,
1423 GST_DEBUG_FUNCPTR (gst_ducati_viddec_src_getcaps));
1425 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
1426 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
1428 self->input_width = 0;
1429 self->input_height = 0;
1430 /* sane defaults in case we need to create codec without caps negotiation
1431 * (for example, to get 'version' property)
1432 */
1433 self->width = 128;
1434 self->height = 128;
1435 self->fps_n = -1;
1436 self->fps_d = -1;
1438 self->first_in_buffer = TRUE;
1439 self->first_out_buffer = TRUE;
1440 self->interlaced = FALSE;
1441 self->send_crop_event = TRUE;
1443 #ifdef USE_DTS_PTS_CODE
1444 self->dts_ridx = self->dts_widx = 0;
1445 self->last_dts = self->last_pts = GST_CLOCK_TIME_NONE;
1446 self->ts_may_be_pts = TRUE;
1447 self->ts_is_pts = FALSE;
1448 #endif
1450 self->pageMemType = XDM_MEMTYPE_TILEDPAGE;
1452 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
1454 self->qos_proportion = 1;
1455 self->qos_earliest_time = GST_CLOCK_TIME_NONE;
1456 self->wait_keyframe = TRUE;
1458 self->need_out_buf = TRUE;
1459 self->device = NULL;
1460 self->input_bo = NULL;
1462 self->passed_in_bufs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1463 NULL, (GDestroyNotify) gst_buffer_unref);
1464 }