7b0151a662566cd8d3446f2fbeaca2b53ae35ff7
1 /*
2 * GStreamer
3 * Copyright (c) 2010, Texas Instruments Incorporated
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
27 #include "gstducatividdec.h"
29 GST_BOILERPLATE (GstDucatiVidDec, gst_ducati_viddec, GstElement,
30 GST_TYPE_ELEMENT);
32 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
33 GST_PAD_SRC,
34 GST_PAD_ALWAYS,
35 GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV_STRIDED ("NV12", "[ 0, max ]"))
36 );
38 /* helper functions */
40 static void
41 engine_close (GstDucatiVidDec * self)
42 {
43 if (self->engine) {
44 Engine_close (self->engine);
45 self->engine = NULL;
46 }
48 if (self->params) {
49 dce_free (self->params);
50 self->params = NULL;
51 }
53 if (self->dynParams) {
54 dce_free (self->dynParams);
55 self->dynParams = NULL;
56 }
58 if (self->status) {
59 dce_free (self->status);
60 self->status = NULL;
61 }
63 if (self->inBufs) {
64 dce_free (self->inBufs);
65 self->inBufs = NULL;
66 }
68 if (self->outBufs) {
69 dce_free (self->outBufs);
70 self->outBufs = NULL;
71 }
73 if (self->inArgs) {
74 dce_free (self->inArgs);
75 self->inArgs = NULL;
76 }
78 if (self->outArgs) {
79 dce_free (self->outArgs);
80 self->outArgs = NULL;
81 }
82 }
84 static gboolean
85 engine_open (GstDucatiVidDec * self)
86 {
87 gboolean ret;
89 if (G_UNLIKELY (self->engine)) {
90 return TRUE;
91 }
93 GST_DEBUG_OBJECT (self, "opening engine");
95 self->engine = Engine_open ("ivahd_vidsvr", NULL, NULL);
96 if (G_UNLIKELY (!self->engine)) {
97 GST_ERROR_OBJECT (self, "could not create engine");
98 return FALSE;
99 }
101 ret = GST_DUCATIVIDDEC_GET_CLASS (self)->allocate_params (self,
102 sizeof (IVIDDEC3_Params), sizeof (IVIDDEC3_DynamicParams),
103 sizeof (IVIDDEC3_Status), sizeof (IVIDDEC3_InArgs),
104 sizeof (IVIDDEC3_OutArgs));
106 return ret;
107 }
109 static void
110 codec_delete (GstDucatiVidDec * self)
111 {
112 if (self->codec) {
113 //XXX this crashes ducati:
114 //VIDDEC3_delete(self->codec);
115 self->codec = NULL;
116 }
118 if (self->input) {
119 MemMgr_Free (self->input);
120 self->input = NULL;
121 }
122 }
124 static gboolean
125 codec_create (GstDucatiVidDec * self)
126 {
127 gint err;
128 const gchar *codec_name;
130 codec_delete (self);
132 if (G_UNLIKELY (!self->engine)) {
133 GST_ERROR_OBJECT (self, "no engine");
134 return FALSE;
135 }
137 /* these need to be set before VIDDEC3_create */
138 self->params->maxWidth = (self->width + 15) & ~0xf; /* round up to MB */
139 self->params->maxHeight = (self->height + 15) & ~0xf; /* round up to MB */
141 codec_name = GST_DUCATIVIDDEC_GET_CLASS (self)->codec_name;
143 /* create codec: */
144 GST_DEBUG_OBJECT (self, "creating codec: %s", codec_name);
145 self->codec = VIDDEC3_create (self->engine, (char *)codec_name, self->params);
147 if (!self->codec) {
148 return FALSE;
149 }
151 err = VIDDEC3_control(self->codec, XDM_SETPARAMS, self->dynParams, self->status);
152 if (err) {
153 GST_ERROR_OBJECT (self, "failed XDM_SETPARAMS");
154 return FALSE;
155 }
157 #if 0
158 /* not entirely sure why we need to call this here.. just copying omx.. */
159 err = VIDDEC3_control(self->codec, XDM_GETBUFINFO, self->dynParams, self->status);
160 if (err) {
161 GST_ERROR_OBJECT (self, "failed XDM_GETBUFINFO");
162 return FALSE;
163 }
164 #endif
166 self->first_buffer = TRUE;
168 /* allocate input buffer and initialize inBufs: */
169 self->inBufs->numBufs = 1;
170 self->input = gst_ducati_alloc_1d (self->width * self->height);
171 self->inBufs->descs[0].buf = (XDAS_Int8 *) TilerMem_VirtToPhys (self->input);
172 self->inBufs->descs[0].memType = XDM_MEMTYPE_RAW;
174 return TRUE;
175 }
177 static XDAS_Int32
178 codec_prepare_outbuf (GstDucatiVidDec * self, GstBuffer * buf)
179 {
180 XDAS_Int16 y_type, uv_type;
181 guint8 *y_vaddr, *uv_vaddr;
182 SSPtr y_paddr, uv_paddr;
184 y_vaddr = GST_BUFFER_DATA (buf);
185 uv_vaddr = y_vaddr + self->stride * self->padded_height;
187 y_paddr = TilerMem_VirtToPhys (y_vaddr);
188 uv_paddr = TilerMem_VirtToPhys (uv_vaddr);
190 y_type = gst_ducati_get_mem_type (y_paddr);
191 uv_type = gst_ducati_get_mem_type (uv_paddr);
193 if ((y_type < 0) || (uv_type < 0)) {
194 return 0;
195 }
197 if (!self->outBufs->numBufs) {
198 /* initialize output buffer type */
199 self->outBufs->numBufs = 2;
200 self->outBufs->descs[0].memType = y_type;
201 self->outBufs->descs[0].bufSize.tileMem.width = self->padded_width;
202 self->outBufs->descs[0].bufSize.tileMem.height = self->padded_height;
203 self->outBufs->descs[1].memType = uv_type;
204 /* note that UV interleaved width is same a Y: */
205 self->outBufs->descs[1].bufSize.tileMem.width = self->padded_width;
206 self->outBufs->descs[1].bufSize.tileMem.height = self->padded_height / 2;
207 } else {
208 /* verify output buffer type matches what we've already given
209 * to the codec
210 */
211 // TODO
212 }
214 self->outBufs->descs[0].buf = (XDAS_Int8 *)y_paddr;
215 self->outBufs->descs[1].buf = (XDAS_Int8 *)uv_paddr;
217 return (XDAS_Int32) buf; // XXX use lookup table
218 }
220 static GstBuffer *
221 codec_get_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
222 {
223 GstBuffer *buf = (GstBuffer *) id; // XXX use lookup table
224 if (buf) {
225 gst_buffer_ref (buf);
226 }
227 return buf;
228 }
230 static void
231 codec_unlock_outbuf (GstDucatiVidDec * self, XDAS_Int32 id)
232 {
233 GstBuffer *buf = (GstBuffer *) id; // XXX use lookup table
234 if (buf) {
235 gst_buffer_unref (buf);
236 }
237 }
239 static gboolean
240 codec_flush (GstDucatiVidDec * self)
241 {
242 if (G_UNLIKELY (!self->codec)) {
243 GST_WARNING_OBJECT (self, "no codec");
244 return TRUE;
245 }
247 GST_WARNING_OBJECT (self, "TODO");
249 return FALSE;
250 }
252 /* GstDucatiVidDec vmethod default implementations */
254 static gboolean
255 gst_ducati_viddec_allocate_params (GstDucatiVidDec * self, gint params_sz,
256 gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz)
257 {
259 /* allocate params: */
260 self->params = dce_alloc (params_sz);
261 if (G_UNLIKELY (!self->params)) {
262 return FALSE;
263 }
264 self->params->size = params_sz;
265 self->params->maxFrameRate = 30000;
266 //h264, mpeg4:
267 //note mpeg4 and h263 are same..
268 self->params->maxBitRate = 10000000;
269 //vc1:
270 //self->params->maxBitRate = 45000000;
272 self->params->dataEndianness = XDM_BYTE;
273 self->params->forceChromaFormat = XDM_YUV_420SP;
274 self->params->operatingMode = IVIDEO_DECODE_ONLY;
276 //mpeg4:
277 //self->params->displayDelay = IVIDDEC3_DISPLAY_DELAY_1;
279 //vc1:
280 //self->params->displayDelay = IVIDDEC3_DISPLAY_DELAY_1;
283 self->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
284 self->params->inputDataMode = IVIDEO_ENTIREFRAME;
285 self->params->outputDataMode = IVIDEO_ENTIREFRAME;
286 self->params->numInputDataUnits = 0;
287 self->params->numOutputDataUnits = 0;
289 //vp6, vp7:
290 //self->params->numInputDataUnits = 1;
291 //self->params->numOutputDataUnits = 1;
293 self->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
294 self->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
295 self->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
296 self->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
298 //mpeg4:
299 //((IMPEG4VDEC_Params *) self->params)->outloopDeBlocking = 0;
300 //((IMPEG4VDEC_Params *) self->params)->sorensonSparkStream = 0;
301 //((IMPEG4VDEC_Params *) self->params)->ErrorConcealmentON = 1;
304 /* allocate dynParams: */
305 self->dynParams = dce_alloc (dynparams_sz);
306 if (G_UNLIKELY (!self->dynParams)) {
307 return FALSE;
308 }
309 self->dynParams->size = dynparams_sz;
310 self->dynParams->decodeHeader = XDM_DECODE_AU;
311 self->dynParams->displayWidth = 0;
312 self->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
313 self->dynParams->newFrameFlag = XDAS_TRUE;
315 //mpeg4:
316 //self->dynParams->lateAcquireArg = IRES_HDVICP2_UNKNOWNLATEACQUIREARG;
318 /* allocate status: */
319 self->status = dce_alloc (status_sz);
320 if (G_UNLIKELY (!self->status)) {
321 return FALSE;
322 }
323 self->status->size = status_sz;
325 /* allocate inBufs/outBufs: */
326 self->inBufs = dce_alloc (sizeof (XDM2_BufDesc));
327 self->outBufs = dce_alloc (sizeof (XDM2_BufDesc));
328 if (G_UNLIKELY (!self->inBufs) || G_UNLIKELY (!self->outBufs)) {
329 return FALSE;
330 }
332 /* allocate inArgs/outArgs: */
333 self->inArgs = dce_alloc (inargs_sz);
334 self->outArgs = dce_alloc (outargs_sz);
335 if (G_UNLIKELY (!self->inArgs) || G_UNLIKELY (!self->outArgs)) {
336 return FALSE;
337 }
338 self->inArgs->size = inargs_sz;
339 self->outArgs->size = outargs_sz;
340 }
342 static GstBuffer *
343 gst_ducati_viddec_push_input (GstDucatiVidDec * self, GstBuffer * buf)
344 {
345 /* just copy entire buffer */
346 self->inArgs->numBytes = GST_BUFFER_SIZE (buf);
347 self->inBufs->descs[0].bufSize.bytes = self->inArgs->numBytes;
348 GST_DEBUG_OBJECT (self, "push: %d bytes)", self->inArgs->numBytes);
349 memcpy (self->input, GST_BUFFER_DATA (buf), self->inArgs->numBytes);
350 gst_buffer_unref (buf);
351 return NULL;
352 }
354 /* GstElement vmethod implementations */
356 static gboolean
357 gst_ducati_viddec_set_caps (GstPad * pad, GstCaps * caps)
358 {
359 gboolean ret = TRUE;
360 GstDucatiVidDec *self = GST_DUCATIVIDDEC (gst_pad_get_parent (pad));
361 GstStructure *s;
363 g_return_val_if_fail (caps, FALSE);
364 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
366 s = gst_caps_get_structure (caps, 0);
368 if (pad == self->sinkpad) {
369 gint width, height, frn, frd;
370 GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
372 if (gst_structure_get_int (s, "width", &width) &&
373 gst_structure_get_int (s, "height", &height) &&
374 gst_structure_get_fraction (s, "framerate", &frn, &frd)) {
375 GstCaps *outcaps;
377 /* ok, these caps seem sane.. grab the required values and construct
378 * appropriate output caps
379 */
380 self->width = width;
381 self->height = height;
382 self->stride = 4096; /* TODO: don't hardcode */
384 /* update output/padded sizes:
385 */
386 GST_DUCATIVIDDEC_GET_CLASS (self)->update_buffer_size (self);
388 self->outsize =
389 GST_ROUND_UP_2 (self->stride * self->padded_height * 3) / 2;
391 outcaps = gst_caps_new_simple ("video/x-raw-yuv-strided",
392 "rowstride", G_TYPE_INT, self->stride,
393 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC ('N','V','1','2'),
394 "width", G_TYPE_INT, self->padded_width,
395 "height", G_TYPE_INT, self->padded_height,
396 "framerate", GST_TYPE_FRACTION, frn, frd,
397 NULL);
399 GST_DEBUG_OBJECT (self, "outcaps: %" GST_PTR_FORMAT, outcaps);
401 ret = gst_pad_set_caps (self->srcpad, outcaps);
402 gst_caps_unref (outcaps);
404 if (!ret) {
405 GST_WARNING_OBJECT (self, "failed to set caps");
406 return FALSE;
407 }
408 } else {
409 GST_WARNING_OBJECT (self, "missing required fields");
410 return FALSE;
411 }
412 } else {
413 GST_INFO_OBJECT (self, "setcaps (src): %" GST_PTR_FORMAT, caps);
414 // XXX check to make sure caps are ok.. keep track if we
415 // XXX need to handle unstrided buffers..
416 GST_WARNING_OBJECT (self, "TODO");
417 }
419 gst_object_unref (self);
421 return gst_pad_set_caps (pad, caps);
422 }
424 static gboolean
425 gst_ducati_viddec_query (GstPad * pad, GstQuery * query)
426 {
427 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
429 switch (GST_QUERY_TYPE (query)) {
430 case GST_QUERY_BUFFERS:
431 GST_DEBUG_OBJECT (self, "min buffers: %d", self->min_buffers);
432 gst_query_set_buffers_count (query, self->min_buffers);
434 GST_DEBUG_OBJECT (self, "min dimensions: %dx%d",
435 self->padded_width, self->padded_height);
436 gst_query_set_buffers_dimensions (query,
437 self->padded_width, self->padded_height);
438 return TRUE;
439 default:
440 return FALSE;
441 }
442 }
444 static GstFlowReturn
445 gst_ducati_viddec_chain (GstPad * pad, GstBuffer * buf)
446 {
447 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
448 GstFlowReturn ret;
449 Int32 err;
450 gint i;
451 GstBuffer *outbuf = NULL;
452 GstClockTime t;
454 if (G_UNLIKELY (!self->engine)) {
455 GST_ERROR_OBJECT (self, "no engine");
456 return GST_FLOW_ERROR;
457 }
459 /* do this before creating codec to ensure reverse caps negotiation
460 * happens first:
461 */
462 ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad, 0, self->outsize,
463 GST_PAD_CAPS (self->srcpad), &outbuf);
465 if (ret != GST_FLOW_OK) {
466 /* TODO: if we had our own buffer class, we could allocate our own
467 * output buffer from TILER...
468 */
469 GST_WARNING_OBJECT (self, "ret=%d", ret);
470 GST_WARNING_OBJECT (self, "TODO: allocate output TILER buffer");
471 return ret;
472 }
474 if (G_UNLIKELY (!self->codec)) {
475 if (!codec_create (self)) {
476 GST_ERROR_OBJECT (self, "could not create codec");
477 return GST_FLOW_ERROR;
478 }
479 }
481 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
482 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
484 self->inArgs->inputID = codec_prepare_outbuf (self, outbuf);
485 if (!self->inArgs->inputID) {
486 GST_ERROR_OBJECT (self, "could not prepare output buffer");
487 return GST_FLOW_ERROR;
488 }
490 buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
492 if (buf) {
493 // XXX
494 GST_WARNING_OBJECT (self, "TODO.. can't push more than one.. need loop");
495 gst_buffer_unref (buf);
496 buf = NULL;
497 }
499 t = gst_util_get_timestamp ();
500 err = VIDDEC3_process (self->codec,
501 self->inBufs, self->outBufs,
502 self->inArgs, self->outArgs);
503 GST_INFO_OBJECT (self, "processed returned in: %8dns",
504 (gint) (gst_util_get_timestamp () - t));
505 if (err) {
506 GST_ERROR_OBJECT (self, "process returned error: %d %08x",
507 err, self->outArgs->extendedError);
508 return GST_FLOW_ERROR;
509 }
511 for (i = 0; self->outArgs->outputID[i]; i++) {
512 if (G_UNLIKELY (self->first_buffer)) {
513 /* send region of interest to sink on first buffer: */
514 XDM_Rect *r =
515 &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
517 gst_pad_push_event (self->srcpad,
518 gst_event_new_crop (r->topLeft.y, r->topLeft.x,
519 r->bottomRight.x - r->topLeft.x,
520 r->bottomRight.y - r->topLeft.y));
522 self->first_buffer = FALSE;
523 }
525 outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
526 gst_pad_push (self->srcpad, outbuf);
527 }
529 for (i = 0; self->outArgs->freeBufID[i]; i++) {
530 codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
531 }
533 if (self->outArgs->outBufsInUseFlag) {
534 GST_WARNING_OBJECT (self, "TODO... outBufsInUseFlag"); // XXX
535 }
537 return GST_FLOW_OK;
538 }
540 static gboolean
541 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
542 {
543 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
544 gboolean ret = TRUE;
546 GST_INFO_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
548 switch (GST_EVENT_TYPE (event)) {
549 case GST_EVENT_EOS:
550 case GST_EVENT_FLUSH_STOP:
551 if (!codec_flush (self)) {
552 GST_ERROR_OBJECT (self, "could not flush");
553 return FALSE;
554 }
555 /* fall-through */
556 default:
557 ret = gst_pad_push_event (self->srcpad, event);
558 break;
559 }
561 GST_LOG_OBJECT (self, "end");
563 return ret;
564 }
566 static GstStateChangeReturn
567 gst_ducati_viddec_change_state (GstElement * element,
568 GstStateChange transition)
569 {
570 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
571 GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
573 GST_INFO_OBJECT (self, "begin: changing state %s -> %s",
574 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
575 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
577 switch (transition) {
578 case GST_STATE_CHANGE_NULL_TO_READY:
579 if (!engine_open (self)) {
580 GST_ERROR_OBJECT (self, "could not open");
581 return GST_STATE_CHANGE_FAILURE;
582 }
583 break;
584 default:
585 break;
586 }
588 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
590 if (ret == GST_STATE_CHANGE_FAILURE)
591 goto leave;
593 switch (transition) {
594 case GST_STATE_CHANGE_READY_TO_NULL:
595 codec_delete (self);
596 engine_close (self);
597 break;
598 default:
599 break;
600 }
602 leave:
603 GST_LOG_OBJECT (self, "end");
605 return ret;
606 }
608 /* GObject vmethod implementations */
610 static void
611 gst_ducati_viddec_finalize (GObject * obj)
612 {
613 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
615 codec_delete (self);
616 engine_close (self);
618 G_OBJECT_CLASS (parent_class)->finalize (obj);
619 }
621 static void
622 gst_ducati_viddec_base_init (gpointer gclass)
623 {
624 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
626 gst_element_class_add_pad_template (element_class,
627 gst_static_pad_template_get (&src_factory));
628 }
630 static void
631 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
632 {
633 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
634 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
636 gobject_class->finalize = gst_ducati_viddec_finalize;
637 gstelement_class->change_state = gst_ducati_viddec_change_state;
639 klass->allocate_params =
640 GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
641 klass->push_input =
642 GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
643 }
645 static void
646 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
647 {
648 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
650 self->sinkpad = gst_pad_new_from_template (
651 gst_element_class_get_pad_template (gstelement_class, "sink"), "sink");
652 gst_pad_set_setcaps_function (self->sinkpad,
653 GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_caps));
654 gst_pad_set_chain_function (self->sinkpad,
655 GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
656 gst_pad_set_event_function (self->sinkpad,
657 GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
659 self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
660 gst_pad_set_setcaps_function (self->srcpad,
661 GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_caps));
662 gst_pad_set_query_function (self->srcpad,
663 GST_DEBUG_FUNCPTR (gst_ducati_viddec_query));
665 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
666 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
667 }