827d51f1ea1355937c99f93cbad84ca55a8f00d4
[profile/ivi/gst-openmax0.10.git] / omx / gstomx_base_videodec.c
1 /*
2  * Copyright (C) 2007-2009 Nokia Corporation.
3  *
4  * Author: Felipe Contreras <felipe.contreras@nokia.com>
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  *
20  */
21
22 #include "gstomx_base_videodec.h"
23 #include "gstomx.h"
24
25 GSTOMX_BOILERPLATE (GstOmxBaseVideoDec, gst_omx_base_videodec, GstOmxBaseFilter,
26     GST_OMX_BASE_FILTER_TYPE);
27
28 static void
29 type_base_init (gpointer g_class)
30 {
31 }
32
33 static void
34 type_class_init (gpointer g_class, gpointer class_data)
35 {
36 }
37
38 static void
39 settings_changed_cb (GOmxCore * core)
40 {
41   GstOmxBaseFilter *omx_base;
42   GstOmxBaseVideoDec *self;
43   guint width;
44   guint height;
45   guint32 format = 0;
46
47   omx_base = core->object;
48   self = GST_OMX_BASE_VIDEODEC (omx_base);
49
50   GST_DEBUG_OBJECT (omx_base, "settings changed");
51
52   {
53     OMX_PARAM_PORTDEFINITIONTYPE param;
54
55     G_OMX_INIT_PARAM (param);
56
57     param.nPortIndex = omx_base->out_port->port_index;
58     OMX_GetParameter (omx_base->gomx->omx_handle, OMX_IndexParamPortDefinition,
59         &param);
60
61     width = param.format.video.nFrameWidth;
62     height = param.format.video.nFrameHeight;
63     switch ((guint)param.format.video.eColorFormat) {
64       case OMX_COLOR_FormatYUV420Planar:
65       case OMX_COLOR_FormatYUV420PackedPlanar:
66         format = GST_MAKE_FOURCC ('I', '4', '2', '0');
67         break;
68       case OMX_COLOR_FormatYCbYCr:
69         format = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
70         break;
71       case OMX_COLOR_FormatCbYCrY:
72         format = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
73         break;
74       /* Add extended_color_format */
75       case OMX_EXT_COLOR_FormatNV12TPhysicalAddress:
76         format = GST_MAKE_FOURCC ('S', 'T', '1', '2');
77         break;
78       case OMX_EXT_COLOR_FormatNV12LPhysicalAddress:
79         format = GST_MAKE_FOURCC ('S', 'N', '1', '2');
80         break;
81       case OMX_COLOR_FormatYUV420SemiPlanar:
82         format = GST_MAKE_FOURCC ('N', 'V', '1', '2');
83         break;
84       default:
85         break;
86     }
87   }
88
89   {
90     GstCaps *new_caps;
91     GstStructure *struc;
92
93     new_caps = gst_caps_new_empty ();
94     struc = gst_structure_new ("video/x-raw-yuv",
95         "width", G_TYPE_INT, width,
96         "height", G_TYPE_INT, height, "format", GST_TYPE_FOURCC, format, NULL);
97
98     if (self->framerate_denom != 0)
99       gst_structure_set (struc, "framerate", GST_TYPE_FRACTION,
100           self->framerate_num, self->framerate_denom, NULL);
101     else
102       /* FIXME this is a workaround for xvimagesink */
103       gst_structure_set (struc, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
104
105     gst_caps_append_structure (new_caps, struc);
106
107     GST_INFO_OBJECT (omx_base, "caps are: %" GST_PTR_FORMAT, new_caps);
108     gst_pad_set_caps (omx_base->srcpad, new_caps);
109   }
110 }
111
112 static gboolean
113 sink_setcaps (GstPad * pad, GstCaps * caps)
114 {
115   GstStructure *structure;
116   GstOmxBaseVideoDec *self;
117   GstOmxBaseFilter *omx_base;
118   GOmxCore *gomx;
119   OMX_PARAM_PORTDEFINITIONTYPE param;
120   gint width = 0;
121   gint height = 0;
122
123   self = GST_OMX_BASE_VIDEODEC (GST_PAD_PARENT (pad));
124   omx_base = GST_OMX_BASE_FILTER (self);
125
126   gomx = (GOmxCore *) omx_base->gomx;
127
128   GST_INFO_OBJECT (self, "setcaps (sink): %" GST_PTR_FORMAT, caps);
129
130   g_return_val_if_fail (gst_caps_get_size (caps) == 1, FALSE);
131
132   structure = gst_caps_get_structure (caps, 0);
133
134   gst_structure_get_int (structure, "width", &width);
135   gst_structure_get_int (structure, "height", &height);
136
137   {
138     const GValue *framerate = NULL;
139     framerate = gst_structure_get_value (structure, "framerate");
140     if (framerate) {
141       self->framerate_num = gst_value_get_fraction_numerator (framerate);
142       self->framerate_denom = gst_value_get_fraction_denominator (framerate);
143     }
144   }
145
146   G_OMX_INIT_PARAM (param);
147
148   {
149     const GValue *codec_data;
150     GstBuffer *buffer;
151
152     codec_data = gst_structure_get_value (structure, "codec_data");
153     if (codec_data) {
154       buffer = gst_value_get_buffer (codec_data);
155       omx_base->codec_data = buffer;
156       gst_buffer_ref (buffer);
157     }
158   }
159
160   /* Input port configuration. */
161   {
162     param.nPortIndex = omx_base->in_port->port_index;
163     OMX_GetParameter (gomx->omx_handle, OMX_IndexParamPortDefinition, &param);
164
165     param.format.video.nFrameWidth = width;
166     param.format.video.nFrameHeight = height;
167
168     OMX_SetParameter (gomx->omx_handle, OMX_IndexParamPortDefinition, &param);
169   }
170
171   return gst_pad_set_caps (pad, caps);
172 }
173
174 static void
175 omx_setup (GstOmxBaseFilter * omx_base)
176 {
177   GstOmxBaseVideoDec *self;
178   GOmxCore *gomx;
179
180   self = GST_OMX_BASE_VIDEODEC (omx_base);
181   gomx = (GOmxCore *) omx_base->gomx;
182
183   GST_INFO_OBJECT (omx_base, "begin");
184
185   {
186     OMX_PARAM_PORTDEFINITIONTYPE param;
187
188     G_OMX_INIT_PARAM (param);
189
190     /* Input port configuration. */
191     {
192       param.nPortIndex = omx_base->in_port->port_index;
193       OMX_GetParameter (gomx->omx_handle, OMX_IndexParamPortDefinition, &param);
194
195       param.format.video.eCompressionFormat = self->compression_format;
196
197       OMX_SetParameter (gomx->omx_handle, OMX_IndexParamPortDefinition, &param);
198     }
199   }
200
201   /* STATE_TUNING */
202   if (omx_base->use_state_tuning && omx_base->sink_set_caps) {
203     sink_setcaps(omx_base->sinkpad, omx_base->sink_set_caps);
204   }
205
206   GST_INFO_OBJECT (omx_base, "end");
207 }
208
209 static void
210 type_instance_init (GTypeInstance * instance, gpointer g_class)
211 {
212   GstOmxBaseFilter *omx_base;
213
214   omx_base = GST_OMX_BASE_FILTER (instance);
215
216   omx_base->omx_setup = omx_setup;
217
218   omx_base->gomx->settings_changed_cb = settings_changed_cb;
219
220   gst_pad_set_setcaps_function (omx_base->sinkpad, sink_setcaps);
221 }