Remove dependency of mm-log & mm-common in libmm-imgp-gstcs library
[platform/core/multimedia/libmm-imgp-gstcs.git] / gstcs / mm_util_gstcs.c
1 /*
2  * libmm-imgp-gstcs
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include "mm_util_gstcs_internal.h"
22 #include <gst/check/gstcheck.h>
23 #include <gst/video/video-format.h>
24
25 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
26 #define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
27 #define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)
28 #define MM_UTIL_ROUND_UP_16(num) (((num)+15)&~15)
29
30
31 static GstFlowReturn
32 _mm_sink_sample(GstElement * appsink, gpointer user_data)
33 {
34         GstBuffer *_buf=NULL;
35         GstSample *_sample = NULL;
36         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
37         _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
38         if (_sample) {
39                 _buf = gst_sample_get_buffer(_sample);
40
41                 pGstreamer_s->output_buffer = _buf;
42
43                 if(pGstreamer_s->output_buffer != NULL) {
44                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
45                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
46                         gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
47                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
48                 } else {
49                         gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
50                 }
51         }
52
53         gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
54         gst_sample_unref(_sample);
55
56         return GST_FLOW_OK;
57 }
58
59 static gboolean
60 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
61 {
62         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
63         switch (GST_MESSAGE_TYPE (message)) {
64         case GST_MESSAGE_EOS: {
65                 gstcs_debug("The source got dry");
66                 gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
67                 g_main_context_pop_thread_default(pGstreamer_s->context);
68                 g_main_loop_quit (pGstreamer_s->loop);
69                 break;
70                 }
71         case GST_MESSAGE_ERROR: {
72                 GError *err = NULL;
73                 gchar *dbg_info = NULL;
74
75                 gst_message_parse_error (message, &err, &dbg_info);
76                 gstcs_error ("ERROR from element %s: %s\n", GST_OBJECT_NAME (message->src), err->message);
77                 gstcs_error ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
78                 g_error_free (err);
79                 g_free (dbg_info);
80                 g_main_context_pop_thread_default(pGstreamer_s->context);
81                 g_main_loop_quit (pGstreamer_s->loop);
82                 gstcs_debug("Quit GST_CS\n");
83                 break;
84                 }
85         default:
86                 break;
87         }
88         return TRUE;
89 }
90
91 static int
92 _mm_get_byte_per_pixcel(const char *__format_label)
93 {
94         int byte_per_pixcel = 1;
95         if (strcmp(__format_label, "RGB565") == 0) {
96                 byte_per_pixcel = 2;
97         } else if (strcmp(__format_label, "RGB888") == 0 ||
98                 strcmp(__format_label, "BGR888") == 0) {
99                 byte_per_pixcel = 3;
100         } else if (strcmp(__format_label, "RGBA8888") == 0 ||
101                 strcmp(__format_label, "ARGB8888") == 0 ||
102                 strcmp(__format_label, "BGRA8888") == 0 ||
103                 strcmp(__format_label, "ABGR8888") == 0 ||
104                 strcmp(__format_label, "RGBX") == 0 ||
105                 strcmp(__format_label, "XRGB") == 0 ||
106                 strcmp(__format_label, "BGRX") == 0 ||
107                 strcmp(__format_label, "XBGR") == 0) {
108                 byte_per_pixcel = 4;
109         }
110
111         gstcs_debug("byte per pixcel : %d", byte_per_pixcel);
112
113         return byte_per_pixcel;
114 }
115
116 static int
117 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
118 {
119         int ret = GSTCS_ERROR_NONE;
120         pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
121         if (!pGstreamer_s->pipeline) {
122                 gstcs_error("pipeline could not be created. Exiting.\n");
123                 ret = GSTCS_ERROR_INVALID_PARAMETER;
124         }
125         pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
126         if (!pGstreamer_s->appsrc) {
127                 gstcs_error("appsrc could not be created. Exiting.\n");
128                 ret = GSTCS_ERROR_INVALID_PARAMETER;
129         }
130         pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorspace");
131         if (!pGstreamer_s->colorspace) {
132                 gstcs_error("colorspace could not be created. Exiting.\n");
133                 ret = GSTCS_ERROR_INVALID_PARAMETER;
134         }
135         pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
136         if (!pGstreamer_s->videoscale) {
137                 gstcs_error("videoscale could not be created. Exiting.\n");
138                 ret = GSTCS_ERROR_INVALID_PARAMETER;
139         }
140         pGstreamer_s->videoflip=gst_element_factory_make("videoflip", "flip");
141         if (!pGstreamer_s->videoflip) {
142                 gstcs_error("videoflip could not be created. Exiting.\n");
143                 ret = GSTCS_ERROR_INVALID_PARAMETER;
144         }
145         pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
146         if (!pGstreamer_s->appsink) {
147                 gstcs_error("appsink could not be created. Exiting.\n");
148                 ret = GSTCS_ERROR_INVALID_PARAMETER;
149         }
150         return ret;
151 }
152
153 static void _mm_destroy_notify(gpointer data) {
154         unsigned char *_data = (unsigned char *)data;
155         if (_data != NULL) {
156                 free(_data);
157                 _data = NULL;
158         }
159 }
160
161 static void
162 _mm_check_caps_format(GstCaps* caps)
163 {
164         GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
165         const gchar* formatInfo = gst_structure_get_string (caps_structure, "format");
166         gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
167 }
168
169 static void
170 _mm_link_pipeline(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int value)
171 {
172         /* set property */
173         gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps); /*g_object_set(pGstreamer_s->appsrc, "caps", input_format->caps, NULL);*/
174         gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps); /*g_object_set(pGstreamer_s->appsink, "caps", output_format->caps, NULL); */
175         gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL);
176         if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
177                 gstcs_error("Fail to link pipeline");
178         } else {
179                 gstcs_debug("Success to link pipeline");
180         }
181
182         g_object_set (G_OBJECT (pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
183         g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
184         g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
185
186         g_object_set(pGstreamer_s->videoflip, "method", value, NULL ); /* GST_VIDEO_FLIP_METHOD_IDENTITY (0): none- Identity (no rotation) (1): clockwise - Rotate clockwise 90 degrees (2): rotate-180 - Rotate 180 degrees (3): counterclockwise - Rotate counter-clockwise 90 degrees (4): horizontal-flip - Flip horizontally (5): vertical-flip - Flip vertically (6): upper-left-diagonal - Flip across upper left/lower right diagonal (7): upper-right-diagonal - Flip across upper right/lower left diagonal */
187
188         /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
189         g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
190 }
191
192 static void
193 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
194 {
195         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
196         GstVideoFormat videoFormat;
197         int _bpp=0; int _depth=0; int _red_mask=0; int _green_mask=0; int _blue_mask=0; int _alpha_mask=0; int _endianness=0;
198
199         if(__format == NULL) {
200                 gstcs_error("Image format is NULL\n");
201                 return;
202         }
203
204         gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
205         memset(_format_name, 0, sizeof(_format_name));
206
207         if(strcmp(__format->colorspace,"YUV") == 0) {
208                 if(strcmp(__format->format_label,"I420") == 0
209                 || strcmp(__format->format_label,"Y42B") == 0
210                 || strcmp(__format->format_label,"Y444") == 0
211                 || strcmp(__format->format_label,"YV12") == 0
212                 || strcmp(__format->format_label,"NV12") == 0
213                 || strcmp(__format->format_label,"UYVY") == 0) {
214                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
215                 }else if(strcmp(__format->format_label,"YUYV") == 0) {
216                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
217                 }
218                 gstcs_debug("Chosen video format: %s", _format_name);
219                 __format->caps = gst_caps_new_simple ("video/x-raw",
220                         "format", G_TYPE_STRING, _format_name,
221                         "framerate", GST_TYPE_FRACTION, 25, 1,
222                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
223                         "width", G_TYPE_INT, __format->width,
224                         "height", G_TYPE_INT, __format->height,
225                         "framerate", GST_TYPE_FRACTION, 1, 1,
226                         NULL);
227         }
228
229         else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
230                 if(strcmp(__format->format_label,"RGB888") == 0) {
231                         _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
232                 }else if(strcmp(__format->format_label,"BGR888") == 0) {
233                         _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
234                 }else if(strcmp(__format->format_label,"RGB565") == 0) {
235                         _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
236                 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
237                         _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
238                 }
239
240                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
241                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
242
243                 __format->caps = gst_caps_new_simple ("video/x-raw",
244                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
245                         "width", G_TYPE_INT, __format->stride,
246                         "height", G_TYPE_INT, __format->elevation,
247                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
248         }
249
250         else if(strcmp(__format->colorspace,"RGBA") ==0) {
251                 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
252                         gstcs_debug("ARGB8888");
253                         _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
254                 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
255                         gstcs_debug("BGRA8888");
256                         _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
257                 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
258                         gstcs_debug("RGBA8888");
259                         _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
260                 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
261                         gstcs_debug("ABGR8888");
262                         _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
263                 }else {
264                         gstcs_error("***Wrong format cs type***\n");
265                 }
266
267                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
268                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
269
270                 __format->caps = gst_caps_new_simple ("video/x-raw",
271                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
272                         "width", G_TYPE_INT, __format->width,
273                         "height", G_TYPE_INT, __format->elevation,
274                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
275         }
276
277         if(__format->caps) {
278                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
279                 _mm_check_caps_format(__format->caps);
280         }else {
281                 gstcs_error("__format->caps is NULL");
282         }
283 }
284
285 static void
286 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
287 {
288         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
289         GstVideoFormat videoFormat;
290         int _bpp=0; int _depth=0; int _red_mask=0; int _green_mask=0; int _blue_mask=0; int _alpha_mask=0; int _endianness=0;
291
292         if(__format == NULL) {
293                 gstcs_error("Image format is NULL\n");
294                 return;
295         }
296
297         gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
298         memset(_format_name, 0, sizeof(_format_name));
299
300         if(strcmp(__format->colorspace,"YUV") == 0) {
301                 if(strcmp(__format->format_label,"I420") == 0
302                 || strcmp(__format->format_label,"Y42B") == 0
303                 || strcmp(__format->format_label,"Y444") == 0
304                 || strcmp(__format->format_label,"YV12") == 0
305                 || strcmp(__format->format_label,"NV12") == 0
306                 || strcmp(__format->format_label,"UYVY") == 0) {
307                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
308                 }else if(strcmp(__format->format_label,"YUYV") == 0) {
309                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
310                 }
311                 gstcs_debug("Chosen video format: %s", _format_name);
312                 __format->caps = gst_caps_new_simple ("video/x-raw",
313                         "format", G_TYPE_STRING, _format_name,
314                         "width", G_TYPE_INT, __format->width,
315                         "height", G_TYPE_INT, __format->height,
316                         "framerate", GST_TYPE_FRACTION, 1, 1,
317                         NULL);
318         }
319
320         else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
321                 if(strcmp(__format->format_label,"RGB888") == 0) {
322                         _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
323                 }else if(strcmp(__format->format_label,"BGR888") == 0) {
324                         _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
325                 }else if(strcmp(__format->format_label,"RGB565") == 0) {
326                         _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
327                 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
328                         _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
329                 }
330
331                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
332                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
333
334                 __format->caps = gst_caps_new_simple ("video/x-raw",
335                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
336                         "width", G_TYPE_INT, __format->width,
337                         "height", G_TYPE_INT, __format->height,
338                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
339         }
340
341         else if(strcmp(__format->colorspace,"RGBA") ==0) {
342                 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
343                         gstcs_debug("ARGB8888");
344                         _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
345                 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
346                         gstcs_debug("BGRA8888");
347                         _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
348                 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
349                         gstcs_debug("RGBA8888");
350                         _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
351                 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
352                         gstcs_debug("ABGR8888");
353                         _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
354                 }else {
355                         gstcs_error("***Wrong format cs type***\n");
356                 }
357
358                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
359                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
360
361                 __format->caps = gst_caps_new_simple ("video/x-raw",
362                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
363                         "width", G_TYPE_INT, __format->width,
364                         "height", G_TYPE_INT, __format->height,
365                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
366         }
367
368         if(__format->caps) {
369                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
370                 _mm_check_caps_format(__format->caps);
371         }else {
372                 gstcs_error("__format->caps is NULL");
373         }
374 }
375
376 static void
377 _mm_set_image_colorspace( image_format_s* __format)
378 {
379         gstcs_debug("format_label: %s\n", __format->format_label);
380
381         __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
382         if (__format->colorspace == NULL) {
383                 gstcs_error("memory allocation failed");
384                 return;
385         }
386         memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
387         if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
388                 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
389                 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
390         }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
391                 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
392         }else if( (strcmp(__format->format_label, "ARGB8888") == 0) || (strcmp(__format->format_label, "BGRA8888") == 0) ||(strcmp(__format->format_label, "RGBA8888") == 0)    || (strcmp(__format->format_label, "ABGR8888") == 0)) {
393                 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
394         }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
395                 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
396         }else {
397                 gstcs_error("Check your colorspace format label");
398                 GSTCS_FREE(__format->colorspace);
399         }
400 }
401
402 static void
403 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
404 {
405         if(strcmp(pFormat->colorspace,"YUV") == 0) {
406                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
407                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
408         } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
409                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
410                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
411         } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
412                 pFormat->stride = pFormat->width;
413                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
414         }
415         gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
416 }
417
418 static image_format_s*
419 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
420 {
421         image_format_s* __format = NULL;
422
423         __format=(image_format_s*)malloc(sizeof(image_format_s));
424         if (__format == NULL) {
425                 gstcs_error("memory allocation failed");
426                 return NULL;
427         }
428         memset(__format, 0, sizeof(image_format_s));
429
430         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
431         if (__format->format_label == NULL) {
432                 gstcs_error("memory allocation failed");
433                 GSTCS_FREE(__format);
434                 return NULL;
435         }
436         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
437         strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
438         gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
439         _mm_set_image_colorspace(__format);
440
441         __format->width=pImgp_info->src_width;
442         __format->height=pImgp_info->src_height;
443         _mm_round_up_input_image_widh_height(__format);
444
445         __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
446         gstcs_debug("blocksize: %d\n", __format->blocksize);
447         _mm_set_image_input_format_s_capabilities(__format);
448
449         return __format;
450 }
451
452 static void
453 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
454 {
455         if(strcmp(pFormat->colorspace,"YUV") == 0) {
456                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
457                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
458         } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
459                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
460                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
461                 pFormat->width = pFormat->stride;
462                 if (input_format->height != input_format->elevation)
463                         pFormat->height = pFormat->elevation;
464         } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
465                 pFormat->stride = pFormat->width;
466                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
467                 if (input_format->height != input_format->elevation)
468                         pFormat->height = pFormat->elevation;
469         }
470         gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
471 }
472
473 static image_format_s*
474 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
475 {
476         image_format_s* __format = NULL;
477
478         __format=(image_format_s*)malloc(sizeof(image_format_s));
479         if (__format == NULL) {
480                 gstcs_error("memory allocation failed");
481                 return NULL;
482         }
483         memset(__format, 0, sizeof(image_format_s));
484
485         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
486         if (__format->format_label == NULL) {
487                 gstcs_error("memory allocation failed");
488                 GSTCS_FREE(__format);
489                 return NULL;
490         }
491         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
492         strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
493         _mm_set_image_colorspace(__format);
494
495         __format->width=pImgp_info->dst_width;
496         __format->height=pImgp_info->dst_height;
497         _mm_round_up_output_image_widh_height(__format, input_format);
498
499         pImgp_info->output_stride = __format->stride;
500         pImgp_info->output_elevation = __format->elevation;
501
502         __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
503         gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
504         _mm_set_image_output_format_s_capabilities(__format);
505         return __format;
506 }
507
508 static int
509 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
510 {
511         int ret = GSTCS_ERROR_NONE;
512
513         if(pGstreamer_s->pipeline == NULL) {
514                 gstcs_error("pipeline is NULL\n");
515                 return GSTCS_ERROR_INVALID_PARAMETER;
516         }
517
518         gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
519         GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
520
521         if(gst_buf==NULL) {
522                 gstcs_error("buffer is NULL\n");
523                 return GSTCS_ERROR_INVALID_PARAMETER;
524         }
525
526         gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
527         return ret;
528 }
529
530 static int
531 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
532 {
533         int ret = GSTCS_ERROR_NONE;
534         GstBuffer *gst_buf = NULL;
535         unsigned int src_size = 0;
536         unsigned char *data = NULL;
537         unsigned int stride = input_format->stride;
538         unsigned int elevation = input_format->elevation;
539
540         if(pGstreamer_s->pipeline == NULL) {
541                 gstcs_error("pipeline is NULL\n");
542                 return GSTCS_ERROR_INVALID_PARAMETER;
543         }
544
545         gstcs_debug("stride: %d, elevation: %d", stride, elevation);
546         src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
547         gstcs_debug("buffer size (src): %d", src_size);
548
549         int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
550         unsigned int src_row = input_format->width * byte_per_pixcel;
551         unsigned int stride_row = stride * byte_per_pixcel;
552         unsigned int i = 0, y = 0;
553         gstcs_debug("padding will be inserted to buffer");
554         data =(unsigned char *) g_malloc (src_size);
555         if(data==NULL) {
556                 gstcs_error("app_buffer is NULL\n");
557                 return GSTCS_ERROR_INVALID_PARAMETER;
558         }
559         for (y = 0; y < (unsigned int)(input_format->height); y++) {
560                 guint8 *pLine = (guint8 *) &(src[src_row * y]);
561                 for(i = 0; i < src_row; i++) {
562                         data[y * stride_row + i] = pLine[i];
563                 }
564                 for(i = src_row; i < stride_row; i++) {
565                         data[y * stride_row + i] = 0x00;
566                 }
567         }
568         for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
569                 for(i = 0; i < stride_row; i++) {
570                         data[y * stride_row + i] = 0x00;
571                 }
572         }
573         gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
574
575         if(gst_buf==NULL) {
576                 gstcs_error("buffer is NULL\n");
577                 return GSTCS_ERROR_INVALID_PARAMETER;
578         }
579
580         gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
581         return ret;
582 }
583
584 static int
585 _mm_imgp_gstcs_processing( gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char *dst, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
586 {
587         GstBus *bus = NULL;
588         GstStateChangeReturn ret_state;
589         int ret = GSTCS_ERROR_NONE;
590
591         if(src== NULL || dst == NULL) {
592                 gstcs_error("src || dst is NULL");
593                 return GSTCS_ERROR_INVALID_PARAMETER;
594         }
595
596         /*create pipeline*/
597         ret = _mm_create_pipeline(pGstreamer_s);
598         if(ret != GSTCS_ERROR_NONE) {
599                 gstcs_error("ERROR - mm_create_pipeline ");
600         }
601
602         pGstreamer_s->context = g_main_context_new();
603         if (pGstreamer_s->context == NULL) {
604                 gstcs_error("ERROR - g_main_context_new ");
605                 gst_object_unref (pGstreamer_s->pipeline);
606                 g_free (pGstreamer_s);
607                 return GSTCS_ERROR_INVALID_OPERATION;
608         }
609         pGstreamer_s->loop = g_main_loop_new (pGstreamer_s->context, FALSE);
610         if (pGstreamer_s->loop == NULL) {
611                 gstcs_error("ERROR - g_main_loop_new ");
612                 gst_object_unref (pGstreamer_s->pipeline);
613                 g_main_context_unref(pGstreamer_s->context);
614                 g_free (pGstreamer_s);
615                 return GSTCS_ERROR_INVALID_OPERATION;
616         }
617
618         g_main_context_push_thread_default(pGstreamer_s->context);
619
620         /* Make appsink emit the "new-preroll" and "new-sample" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
621         gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
622
623         bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline));
624         gst_bus_add_watch (bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
625         gst_object_unref(bus);
626
627         if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
628                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
629                 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
630                 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
631         } else {
632                 gstcs_debug("Start mm_push_buffer_into_pipeline");
633                 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
634         }
635         if(ret != GSTCS_ERROR_NONE) {
636                 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
637                 gst_object_unref (pGstreamer_s->pipeline);
638                 g_main_context_unref(pGstreamer_s->context);
639                 g_main_loop_unref(pGstreamer_s->loop);
640                 g_free (pGstreamer_s);
641                 return ret;
642         }
643         gstcs_debug("End mm_push_buffer_into_pipeline");
644
645         /*link pipeline*/
646         gstcs_debug("Start mm_link_pipeline");
647         _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
648         gstcs_debug("End mm_link_pipeline");
649
650         /* Conecting to the new-sample signal emited by the appsink*/
651         gstcs_debug("Start G_CALLBACK (_mm_sink_sample)");
652         g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_sample), pGstreamer_s);
653         gstcs_debug("End G_CALLBACK (_mm_sink_sample)");
654
655         /* GST_STATE_PLAYING*/
656         gstcs_debug("Start GST_STATE_PLAYING");
657         ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
658         gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
659
660         /*g_main_loop_run*/
661         gstcs_debug("g_main_loop_run");
662         g_main_loop_run (pGstreamer_s->loop);
663
664         gstcs_debug("Sucess GST_STATE_CHANGE");
665
666         /*GST_STATE_NULL*/
667         gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
668         gstcs_debug("End GST_STATE_NULL");
669
670         gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
671
672         ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
673
674         if(ret_state == GST_STATE_CHANGE_SUCCESS) {
675                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
676         }else if( ret_state == GST_STATE_CHANGE_ASYNC) {
677                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
678         }
679
680         gstcs_debug("Success gst_element_get_state\n");
681
682         if (ret_state == GST_STATE_CHANGE_FAILURE) {
683                 gstcs_error("GST_STATE_CHANGE_FAILURE");
684         } else {
685                 if(pGstreamer_s->output_buffer != NULL) {
686                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
687                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
688                         int buffer_size = mapinfo.size;
689                         int calc_buffer_size = 0;
690                         if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
691                                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
692                                 gstcs_debug("calculate image size with stride & elevation");
693                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
694                         } else {
695                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
696                         }
697                         gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
698                         if( buffer_size != calc_buffer_size) {
699                                 gstcs_debug("Buffer size is different \n");
700                                 gstcs_debug("unref output buffer");
701                                 gst_buffer_unref (pGstreamer_s->output_buffer);
702                                 gst_object_unref (pGstreamer_s->pipeline);
703                                 pGstreamer_s->output_buffer = NULL;
704                                 g_main_context_unref(pGstreamer_s->context);
705                                 g_main_loop_unref(pGstreamer_s->loop);
706                                 g_free (pGstreamer_s);
707                                 return GSTCS_ERROR_INVALID_OPERATION;
708                         }
709                         gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
710                         memcpy(dst, mapinfo.data, buffer_size);
711                         pImgp_info->buffer_size = buffer_size;
712                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
713                 }else {
714                         gstcs_debug("pGstreamer_s->output_buffer is NULL");
715                 }
716         }
717         gstcs_debug("unref output buffer");
718         gst_buffer_unref (pGstreamer_s->output_buffer);
719         gst_object_unref (pGstreamer_s->pipeline);
720         pGstreamer_s->output_buffer = NULL;
721         g_main_context_unref(pGstreamer_s->context);
722         g_main_loop_unref(pGstreamer_s->loop);
723         g_free (pGstreamer_s);
724
725         gstcs_debug("End gstreamer processing");
726         gstcs_debug("dst: %p", dst);
727         return ret;
728 }
729
730
731 static int
732 mm_setup_image_size(const char* _format_label, int width, int height)
733 {
734         int size=0;
735
736         if(strcmp(_format_label, "I420") == 0) {
737                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) + MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) /2); /*width * height *1.5; */
738         }else if(strcmp(_format_label, "Y42B") == 0) {
739                 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
740         }else if(strcmp(_format_label, "YUV422") == 0) {
741                 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
742         }else if(strcmp(_format_label, "Y444") == 0) {
743                 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
744         }else if(strcmp(_format_label, "YV12") == 0) {
745                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height)+ MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) / 2); /* width * height *1; */
746         }else if(strcmp(_format_label, "NV12") == 0) {
747                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
748         }else if(strcmp(_format_label, "RGB565") == 0) {
749                 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
750         }else if(strcmp(_format_label, "RGB888") == 0) {
751                 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
752         }else if(strcmp(_format_label, "BGR888") == 0) {
753                 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
754         }else if(strcmp(_format_label, "UYVY") == 0) {
755                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
756         }else if(strcmp(_format_label, "YUYV") == 0) {
757                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
758         }else if(strcmp(_format_label, "ARGB8888") == 0) {
759                 size = width * height *4;
760         }else if(strcmp(_format_label, "BGRA8888") == 0) {
761                 size = width * height *4;
762         }else if(strcmp(_format_label, "RGBA8888") == 0) {
763                 size = width * height *4;
764         }else if(strcmp(_format_label, "ABGR8888") == 0) {
765                 size = width * height *4;
766         }else if(strcmp(_format_label, "BGRX") == 0) {
767                 size = width * height *4;
768         }
769
770         gstcs_debug("file_size: %d\n", size);
771
772         return size;
773 }
774
775 static int
776 _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
777 {
778         image_format_s* input_format=NULL, *output_format=NULL;
779         gstreamer_s* pGstreamer_s;
780         int ret = GSTCS_ERROR_NONE;
781         static const int max_argc = 50;
782         gint* argc = NULL;
783         gchar** argv = NULL;
784         int i = 0;
785
786         if(pImgp_info == NULL) {
787                 gstcs_error("imgp_info_s is NULL");
788                 return GSTCS_ERROR_INVALID_PARAMETER;
789         }
790
791         if(src== NULL || dst == NULL) {
792                 gstcs_error("src || dst is NULL");
793                 return GSTCS_ERROR_INVALID_PARAMETER;
794         }
795         gstcs_debug("[src %p] [dst %p]", src, dst);
796
797         argc = malloc(sizeof(int));
798         argv = malloc(sizeof(gchar*) * max_argc);
799
800         if (!argc || !argv) {
801                 gstcs_error("argc ||argv is NULL");
802                 GSTCS_FREE(input_format);
803                 GSTCS_FREE(output_format);
804                 if (argc != NULL) {
805                         free(argc);
806                 }
807                 if (argv != NULL) {
808                         free(argv);
809                 }
810                 return GSTCS_ERROR_INVALID_PARAMETER;
811         }
812         memset(argv, 0, sizeof(gchar*) * max_argc);
813         gstcs_debug("memset argv");
814
815         /* add initial */
816         *argc = 1;
817         argv[0] = g_strdup( "mmutil_gstcs" );
818         /* check disable registry scan */
819         argv[*argc] = g_strdup("--gst-disable-registry-update");
820         (*argc)++;
821         gstcs_debug("--gst-disable-registry-update");
822
823         gst_init(argc, &argv);
824
825         pGstreamer_s = g_new0 (gstreamer_s, 1);
826
827         for (i = 0; i < (*argc); i++) {
828                 GSTCS_FREE(argv[i]);
829         }
830         GSTCS_FREE(argv);
831         GSTCS_FREE(argc);
832
833         gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
834         pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height, pImgp_info->angle);
835
836         input_format= _mm_set_input_image_format_s_struct(pImgp_info);
837         if (input_format == NULL) {
838                 gstcs_error("memory allocation failed");
839                 return GSTCS_ERROR_OUT_OF_MEMORY;
840         }
841         output_format= _mm_set_output_image_format_s_struct(pImgp_info, input_format);
842         if (output_format == NULL) {
843                 gstcs_error("memory allocation failed");
844                 GSTCS_FREE(input_format->format_label);
845                 GSTCS_FREE(input_format->colorspace);
846                 GSTCS_FREE(input_format);
847                 return GSTCS_ERROR_OUT_OF_MEMORY;
848         }
849
850         /* _format_label : I420, RGB888 etc*/
851         gstcs_debug("Start _mm_imgp_gstcs_processing ");
852         ret =_mm_imgp_gstcs_processing(pGstreamer_s, src, dst, input_format, output_format, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */
853
854         if(ret == GSTCS_ERROR_NONE) {
855                 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
856         }else if (ret != GSTCS_ERROR_NONE) {
857                 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
858         }
859
860         GSTCS_FREE(input_format->format_label);
861         GSTCS_FREE(input_format->colorspace);
862         GSTCS_FREE(input_format);
863
864         GSTCS_FREE(output_format->format_label);
865         GSTCS_FREE(output_format->colorspace);
866         GSTCS_FREE(output_format);
867
868         return ret;
869 }
870
871 int
872 mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
873 {
874         if (pImgp_info == NULL) {
875                 gstcs_error("input vaule is error");
876         }
877         return _mm_imgp_gstcs(pImgp_info, src, dst);
878 }