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