36e9b8e92466bc725052a79f33c4f5e7c52a2c88
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;
453 if (G_UNLIKELY (!self->engine)) {
454 GST_ERROR_OBJECT (self, "no engine");
455 return GST_FLOW_ERROR;
456 }
458 /* do this before creating codec to ensure reverse caps negotiation
459 * happens first:
460 */
461 ret = gst_pad_alloc_buffer_and_set_caps (self->srcpad, 0, self->outsize,
462 GST_PAD_CAPS (self->srcpad), &outbuf);
464 if (ret != GST_FLOW_OK) {
465 /* TODO: if we had our own buffer class, we could allocate our own
466 * output buffer from TILER...
467 */
468 GST_WARNING_OBJECT (self, "ret=%d", ret);
469 GST_WARNING_OBJECT (self, "TODO: allocate output TILER buffer");
470 return ret;
471 }
473 if (G_UNLIKELY (!self->codec)) {
474 if (!codec_create (self)) {
475 GST_ERROR_OBJECT (self, "could not create codec");
476 return GST_FLOW_ERROR;
477 }
478 }
480 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
481 GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf);
483 self->inArgs->inputID = codec_prepare_outbuf (self, outbuf);
484 if (!self->inArgs->inputID) {
485 GST_ERROR_OBJECT (self, "could not prepare output buffer");
486 return GST_FLOW_ERROR;
487 }
489 buf = GST_DUCATIVIDDEC_GET_CLASS (self)->push_input (self, buf);
491 if (buf) {
492 // XXX
493 GST_WARNING_OBJECT (self, "TODO.. can't push more than one.. need loop");
494 gst_buffer_unref (buf);
495 buf = NULL;
496 }
498 //XXX t = mark (NULL);
499 err = VIDDEC3_process (self->codec,
500 self->inBufs, self->outBufs,
501 self->inArgs, self->outArgs);
502 //XXX GST_DEBUG_OBJECT (self, "processed returned in: %dus", mark (&t));
503 if (err) {
504 GST_ERROR_OBJECT (self, "process returned error: %d %08x",
505 err, self->outArgs->extendedError);
506 return GST_FLOW_ERROR;
507 }
509 for (i = 0; self->outArgs->outputID[i]; i++) {
510 if (G_UNLIKELY (self->first_buffer)) {
511 /* send region of interest to sink on first buffer: */
512 XDM_Rect *r =
513 &(self->outArgs->displayBufs.bufDesc[0].activeFrameRegion);
515 gst_pad_push_event (self->srcpad,
516 gst_event_new_crop (r->topLeft.y, r->topLeft.x,
517 r->bottomRight.x - r->topLeft.x,
518 r->bottomRight.y - r->topLeft.y));
520 self->first_buffer = FALSE;
521 }
523 outbuf = codec_get_outbuf (self, self->outArgs->outputID[i]);
524 gst_pad_push (self->srcpad, outbuf);
525 }
527 for (i = 0; self->outArgs->freeBufID[i]; i++) {
528 codec_unlock_outbuf (self, self->outArgs->freeBufID[i]);
529 }
531 if (self->outArgs->outBufsInUseFlag) {
532 GST_WARNING_OBJECT (self, "TODO... outBufsInUseFlag"); // XXX
533 }
535 return GST_FLOW_OK;
536 }
538 static gboolean
539 gst_ducati_viddec_event (GstPad * pad, GstEvent * event)
540 {
541 GstDucatiVidDec *self = GST_DUCATIVIDDEC (GST_OBJECT_PARENT (pad));
542 gboolean ret = TRUE;
544 GST_INFO_OBJECT (self, "begin: event=%s", GST_EVENT_TYPE_NAME (event));
546 switch (GST_EVENT_TYPE (event)) {
547 case GST_EVENT_EOS:
548 case GST_EVENT_FLUSH_STOP:
549 if (!codec_flush (self)) {
550 GST_ERROR_OBJECT (self, "could not flush");
551 return FALSE;
552 }
553 /* fall-through */
554 default:
555 ret = gst_pad_push_event (self->srcpad, event);
556 break;
557 }
559 GST_LOG_OBJECT (self, "end");
561 return ret;
562 }
564 static GstStateChangeReturn
565 gst_ducati_viddec_change_state (GstElement * element,
566 GstStateChange transition)
567 {
568 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
569 GstDucatiVidDec *self = GST_DUCATIVIDDEC (element);
571 GST_INFO_OBJECT (self, "begin: changing state %s -> %s",
572 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
573 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
575 switch (transition) {
576 case GST_STATE_CHANGE_NULL_TO_READY:
577 if (!engine_open (self)) {
578 GST_ERROR_OBJECT (self, "could not open");
579 return GST_STATE_CHANGE_FAILURE;
580 }
581 break;
582 default:
583 break;
584 }
586 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
588 if (ret == GST_STATE_CHANGE_FAILURE)
589 goto leave;
591 switch (transition) {
592 case GST_STATE_CHANGE_READY_TO_NULL:
593 codec_delete (self);
594 engine_close (self);
595 break;
596 default:
597 break;
598 }
600 leave:
601 GST_LOG_OBJECT (self, "end");
603 return ret;
604 }
606 /* GObject vmethod implementations */
608 static void
609 gst_ducati_viddec_finalize (GObject * obj)
610 {
611 GstDucatiVidDec *self = GST_DUCATIVIDDEC (obj);
613 codec_delete (self);
614 engine_close (self);
616 G_OBJECT_CLASS (parent_class)->finalize (obj);
617 }
619 static void
620 gst_ducati_viddec_base_init (gpointer gclass)
621 {
622 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
624 gst_element_class_add_pad_template (element_class,
625 gst_static_pad_template_get (&src_factory));
626 }
628 static void
629 gst_ducati_viddec_class_init (GstDucatiVidDecClass * klass)
630 {
631 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
632 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
634 gobject_class->finalize = gst_ducati_viddec_finalize;
635 gstelement_class->change_state = gst_ducati_viddec_change_state;
637 klass->allocate_params =
638 GST_DEBUG_FUNCPTR (gst_ducati_viddec_allocate_params);
639 klass->push_input =
640 GST_DEBUG_FUNCPTR (gst_ducati_viddec_push_input);
641 }
643 static void
644 gst_ducati_viddec_init (GstDucatiVidDec * self, GstDucatiVidDecClass * klass)
645 {
646 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
648 self->sinkpad = gst_pad_new_from_template (
649 gst_element_class_get_pad_template (gstelement_class, "sink"), "sink");
650 gst_pad_set_setcaps_function (self->sinkpad,
651 GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_caps));
652 gst_pad_set_chain_function (self->sinkpad,
653 GST_DEBUG_FUNCPTR (gst_ducati_viddec_chain));
654 gst_pad_set_event_function (self->sinkpad,
655 GST_DEBUG_FUNCPTR (gst_ducati_viddec_event));
657 self->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
658 gst_pad_set_setcaps_function (self->srcpad,
659 GST_DEBUG_FUNCPTR (gst_ducati_viddec_set_caps));
660 gst_pad_set_query_function (self->srcpad,
661 GST_DEBUG_FUNCPTR (gst_ducati_viddec_query));
663 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
664 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
665 }