Change unsigned int to size_t for buffer_size
[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 <stdbool.h>
22 #include "mm_util_gstcs_internal.h"
23 #include <gst/check/gstcheck.h>
24 #include <gst/video/video-format.h>
25
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 #define SAFE_STRCPY(dst, src, n)        g_strlcpy(dst, src, n)
32
33
34 static GstFlowReturn
35 _mm_sink_sample(GstElement * appsink, gpointer user_data)
36 {
37         GstBuffer *_buf = NULL;
38         GstSample *_sample = NULL;
39         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
40         _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
41         if (_sample) {
42                 _buf = gst_sample_get_buffer(_sample);
43
44                 pGstreamer_s->output_buffer = _buf;
45
46                 if (pGstreamer_s->output_buffer != NULL) {
47                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
48                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
49                         gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
50                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
51                 } else {
52                         gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
53                 }
54         }
55
56         gst_buffer_ref(pGstreamer_s->output_buffer); /* when you want to avoid flushing */
57         gst_sample_unref(_sample);
58
59         return GST_FLOW_OK;
60 }
61
62 static gboolean
63 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
64 {
65         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
66         switch (GST_MESSAGE_TYPE(message)) {
67         case GST_MESSAGE_EOS: {
68                 gstcs_debug("The source got dry");
69                 gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
70                 g_main_context_pop_thread_default(pGstreamer_s->context);
71                 g_main_loop_quit(pGstreamer_s->loop);
72                 break;
73                 }
74         case GST_MESSAGE_ERROR: {
75                 GError *err = NULL;
76                 gchar *dbg_info = NULL;
77
78                 gst_message_parse_error(message, &err, &dbg_info);
79                 gstcs_error("ERROR from element %s: %s\n", GST_OBJECT_NAME(message->src), err->message);
80                 gstcs_error("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
81                 g_error_free(err);
82                 g_free(dbg_info);
83                 g_main_context_pop_thread_default(pGstreamer_s->context);
84                 g_main_loop_quit(pGstreamer_s->loop);
85                 gstcs_debug("Quit GST_CS\n");
86                 break;
87                 }
88         default:
89                 break;
90         }
91         return TRUE;
92 }
93
94 static int _mm_get_byte_per_pixcel(mm_util_color_format_e color_format)
95 {
96         int byte_per_pixcel = 1;
97
98         switch (color_format) {
99         case MM_UTIL_COLOR_YUV420:
100         case MM_UTIL_COLOR_YUV422:
101         case MM_UTIL_COLOR_I420:
102         case MM_UTIL_COLOR_NV12:
103         case MM_UTIL_COLOR_UYVY:
104         case MM_UTIL_COLOR_YUYV:
105                 byte_per_pixcel = 1;
106                 break;
107         case MM_UTIL_COLOR_RGB16:
108                 byte_per_pixcel = 2;
109                 break;
110         case MM_UTIL_COLOR_RGB24:
111                 byte_per_pixcel = 3;
112                 break;
113         case MM_UTIL_COLOR_ARGB:
114         case MM_UTIL_COLOR_BGRA:
115         case MM_UTIL_COLOR_RGBA:
116         case MM_UTIL_COLOR_BGRX:
117                 byte_per_pixcel = 4;
118                 break;
119         default:
120                 gstcs_error("Not supported format");
121                 break;
122         }
123
124         gstcs_debug("color_format [%d] byte per pixcel [%d]", color_format, byte_per_pixcel);
125
126         return byte_per_pixcel;
127 }
128
129 static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
130 {
131         int ret = GSTCS_ERROR_NONE;
132         pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
133         if (!pGstreamer_s->pipeline) {
134                 gstcs_error("pipeline could not be created. Exiting.\n");
135                 ret = GSTCS_ERROR_INVALID_PARAMETER;
136         }
137         pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
138         if (!pGstreamer_s->appsrc) {
139                 gstcs_error("appsrc could not be created. Exiting.\n");
140                 ret = GSTCS_ERROR_INVALID_PARAMETER;
141         }
142         pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
143         if (!pGstreamer_s->colorspace) {
144                 gstcs_error("colorspace could not be created. Exiting.\n");
145                 ret = GSTCS_ERROR_INVALID_PARAMETER;
146         }
147         pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
148         if (!pGstreamer_s->videoscale) {
149                 gstcs_error("videoscale could not be created. Exiting.\n");
150                 ret = GSTCS_ERROR_INVALID_PARAMETER;
151         }
152         pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
153         if (!pGstreamer_s->videoflip) {
154                 gstcs_error("videoflip could not be created. Exiting.\n");
155                 ret = GSTCS_ERROR_INVALID_PARAMETER;
156         }
157         pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
158         if (!pGstreamer_s->appsink) {
159                 gstcs_error("appsink could not be created. Exiting.\n");
160                 ret = GSTCS_ERROR_INVALID_PARAMETER;
161         }
162         return ret;
163 }
164
165 static void _mm_destroy_notify(gpointer data)
166 {
167         unsigned char *_data = (unsigned char *)data;
168         if (_data != NULL) {
169                 free(_data);
170                 _data = NULL;
171         }
172 }
173
174 static void
175 _mm_check_caps_format(GstCaps* caps)
176 {
177         GstStructure *caps_structure = gst_caps_get_structure(caps, 0);
178         const gchar* formatInfo = gst_structure_get_string(caps_structure, "format");
179         gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
180 }
181
182 static void
183 _mm_link_pipeline(gstreamer_s* pGstreamer_s, int value)
184 {
185         gstcs_fenter();
186
187         /* set property */
188         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);
189         if (!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL))
190                 gstcs_error("Fail to link pipeline");
191         else
192                 gstcs_debug("Success to link pipeline");
193
194         g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
195         g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
196         g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
197
198         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 */
199
200         /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
201         g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
202
203         gstcs_fleave();
204 }
205
206 static GstVideoFormat _mm_get_video_format(mm_util_color_format_e color_format)
207 {
208         GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
209         int _bpp = 0;
210         int _depth = 0;
211         int _red_mask = 0;
212         int _green_mask = 0;
213         int _blue_mask = 0;
214         int _alpha_mask = 0;
215         int _endianness = 0;
216
217         switch (color_format) {
218         case MM_UTIL_COLOR_YUV420:
219                 videoFormat = GST_VIDEO_FORMAT_YV12;
220                 break;
221         case MM_UTIL_COLOR_YUV422:
222                 videoFormat = GST_VIDEO_FORMAT_Y42B;
223                 break;
224         case MM_UTIL_COLOR_I420:
225                 videoFormat = GST_VIDEO_FORMAT_I420;
226                 break;
227         case MM_UTIL_COLOR_NV12:
228                 videoFormat = GST_VIDEO_FORMAT_NV12;
229                 break;
230         case MM_UTIL_COLOR_UYVY:
231                 videoFormat = GST_VIDEO_FORMAT_UYVY;
232                 break;
233         case MM_UTIL_COLOR_YUYV:
234                 videoFormat = GST_VIDEO_FORMAT_YVYU;
235                 break;
236         case MM_UTIL_COLOR_RGB16:
237                 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234; _alpha_mask = 0;
238                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
239                 break;
240         case MM_UTIL_COLOR_RGB24:
241                 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321; _alpha_mask = 0;
242                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
243                 break;
244         case MM_UTIL_COLOR_ARGB:
245                 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
246                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
247                 break;
248         case MM_UTIL_COLOR_BGRA:
249                 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
250                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
251                 break;
252         case MM_UTIL_COLOR_RGBA:
253                 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
254                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
255                 break;
256         case MM_UTIL_COLOR_BGRX:
257                 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321; _alpha_mask = 0;
258                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
259                 break;
260         default:
261                 gstcs_error("Not supported format");
262                 break;
263         }
264
265         gstcs_debug("color_format [%d], Chosen video format [%s]", color_format, gst_video_format_to_string(videoFormat));
266
267         return videoFormat;
268
269 }
270
271 static GstCaps* _mm_get_capabilities(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
272 {
273         GstCaps *caps = NULL;
274         GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
275
276         videoFormat = _mm_get_video_format(color_format);
277
278         caps = gst_caps_new_simple("video/x-raw",
279                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
280                         "framerate", GST_TYPE_FRACTION, 25, 1,
281                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
282                         "width", G_TYPE_INT, width,
283                         "height", G_TYPE_INT, height,
284                         "framerate", GST_TYPE_FRACTION, 1, 1,
285                         NULL);
286
287         if (caps)
288                 _mm_check_caps_format(caps);
289         else
290                 gstcs_error("caps is NULL");
291
292         return caps;
293 }
294
295 static void _mm_get_round_up_width_height(mm_util_color_format_e color_format, unsigned int width, unsigned int height, unsigned int *stride, unsigned int *elevation)
296 {
297         switch (color_format) {
298         case MM_UTIL_COLOR_YUV420:
299         case MM_UTIL_COLOR_YUV422:
300         case MM_UTIL_COLOR_I420:
301         case MM_UTIL_COLOR_NV12:
302         case MM_UTIL_COLOR_UYVY:
303         case MM_UTIL_COLOR_YUYV:
304                 *stride = width;
305                 *elevation = height;
306                 break;
307         case MM_UTIL_COLOR_RGB16:
308         case MM_UTIL_COLOR_RGB24:
309                 *stride = MM_UTIL_ROUND_UP_4(width);
310                 *elevation = MM_UTIL_ROUND_UP_2(height);
311                 break;
312         case MM_UTIL_COLOR_ARGB:
313         case MM_UTIL_COLOR_BGRA:
314         case MM_UTIL_COLOR_RGBA:
315         case MM_UTIL_COLOR_BGRX:
316                 *stride = width;
317                 *elevation = MM_UTIL_ROUND_UP_2(height);
318                 break;
319         default:
320                 gstcs_error("Not supported format");
321                 break;
322         }
323
324         gstcs_debug("color_format[%d] width[%u] height[%u] stride[%u], elevation[%u]", color_format, width, height, *stride, *elevation);
325 }
326
327 static size_t _mm_setup_image_size(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
328 {
329         size_t size = 0;
330
331         gstcs_debug("color_format [%d] width [%u] height [%u]", color_format, width, height);
332
333         switch (color_format) {
334         case MM_UTIL_COLOR_YUV420:
335                 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; */
336                 break;
337         case MM_UTIL_COLOR_YUV422:
338                 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
339                 break;
340         case MM_UTIL_COLOR_I420:
341                 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; */
342                 break;
343         case MM_UTIL_COLOR_NV12:
344                 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
345                 break;
346         case MM_UTIL_COLOR_UYVY:
347                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
348                 break;
349         case MM_UTIL_COLOR_YUYV:
350                 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
351                 break;
352         case MM_UTIL_COLOR_RGB16:
353                 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
354                 break;
355         case MM_UTIL_COLOR_RGB24:
356                 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
357                 break;
358         case MM_UTIL_COLOR_ARGB:
359         case MM_UTIL_COLOR_BGRA:
360         case MM_UTIL_COLOR_RGBA:
361         case MM_UTIL_COLOR_BGRX:
362                 size = width * height *4;
363                 break;
364         default:
365                 gstcs_error("Not supported format");
366                 break;
367         }
368
369
370         gstcs_debug("Image size [%u]", size);
371
372         return size;
373 }
374
375 static int _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
376 {
377         int ret = GSTCS_ERROR_NONE;
378         size_t data_size = 0;
379         GstBuffer* gst_buf = NULL;
380
381         gstcs_fenter();
382
383         gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");
384
385         data_size = _mm_setup_image_size(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height);
386         gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
387
388         gstcs_retvm_if(gst_buf == NULL, GSTCS_ERROR_INVALID_OPERATION, "buffer is NULL");
389
390         gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
391
392         gstcs_fleave();
393
394         return ret;
395 }
396
397 static int _mm_push_buffer_into_pipeline_new(unsigned char *src, gstreamer_s * pGstreamer_s, mm_util_color_format_e color_format, unsigned int width, unsigned int height, unsigned int stride, unsigned int elevation)
398 {
399         int ret = GSTCS_ERROR_NONE;
400         GstBuffer *gst_buf = NULL;
401         size_t src_size = 0;
402         unsigned char *data = NULL;
403
404         gstcs_fenter();
405
406         gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");
407         gstcs_retvm_if((width == 0 || height == 0), GSTCS_ERROR_INVALID_PARAMETER, "Invalid width(%u) and height(%u)", width, height);
408
409         src_size = _mm_setup_image_size(color_format, stride, elevation);
410
411         int byte_per_pixcel = _mm_get_byte_per_pixcel(color_format);
412         unsigned int src_row = width * byte_per_pixcel;
413         unsigned int stride_row = stride * byte_per_pixcel;
414         unsigned int i = 0, y = 0;
415
416         data = (unsigned char *) malloc(src_size);
417         gstcs_retvm_if(data == NULL, GSTCS_ERROR_OUT_OF_MEMORY, "data is NULL");
418
419         for (y = 0; y < height; y++) {
420                 guint8 *pLine = (guint8 *) &(src[src_row * y]);
421                 for (i = 0; i < src_row; i++)
422                         data[y * stride_row + i] = pLine[i];
423                 guint8 stride_row_color = pLine[i - 1];
424                 for (i = src_row; i < stride_row; i++)
425                         data[y * stride_row + i] = stride_row_color;
426         }
427         for (y = height; y < elevation; y++) {
428                 for (i = 0; i < stride_row; i++)
429                         data[y * stride_row + i] = data[(y - 1) * stride_row + i];
430         }
431         gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
432
433         if (gst_buf == NULL) {
434                 gstcs_error("buffer is NULL\n");
435                 GSTCS_FREE(data);
436                 return GSTCS_ERROR_INVALID_PARAMETER;
437         }
438
439         gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
440
441         gstcs_fleave();
442
443         return ret;
444 }
445
446 static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char **dst, imgp_info_s* pImgp_info)
447 {
448         GstBus *bus = NULL;
449         GstStateChangeReturn ret_state;
450         int ret = GSTCS_ERROR_NONE;
451         GstCaps *src_caps = NULL;
452         GstCaps *dst_caps = NULL;
453         unsigned int src_stride = 0;
454         unsigned int src_elevation = 0;
455
456         gstcs_fenter();
457
458         /*create pipeline*/
459         ret = _mm_create_pipeline(pGstreamer_s);
460         if (ret != GSTCS_ERROR_NONE)
461                 gstcs_error("ERROR - mm_create_pipeline ");
462
463         /* 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. */
464         gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
465
466         bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
467         gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
468         gst_object_unref(bus);
469
470         _mm_get_round_up_width_height(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, &src_stride, &src_elevation);
471         _mm_get_round_up_width_height(pImgp_info->dst_format, pImgp_info->dst_width, pImgp_info->dst_height, &pImgp_info->output_stride, &pImgp_info->output_elevation);
472
473         src_caps = _mm_get_capabilities(pImgp_info->src_format, src_stride, src_elevation);
474         dst_caps = _mm_get_capabilities(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);
475
476         gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), src_caps);
477         gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), dst_caps);
478
479         if ((pImgp_info->src_width != src_stride) || (pImgp_info->src_height != src_elevation)) {
480                 ret = _mm_push_buffer_into_pipeline_new(src, pGstreamer_s, pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, src_stride, src_elevation);
481         } else {
482                 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
483         }
484
485         if (ret != GSTCS_ERROR_NONE) {
486                 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
487                 if (src_caps)
488                         gst_caps_unref(src_caps);
489                 if (dst_caps)
490                         gst_caps_unref(dst_caps);
491                 gst_object_unref(pGstreamer_s->pipeline);
492                 return ret;
493         }
494
495         /*link pipeline*/
496         _mm_link_pipeline(pGstreamer_s, pImgp_info->angle);
497
498         /* Conecting to the new-sample signal emited by the appsink*/
499         gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
500         g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
501         gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
502
503         /* GST_STATE_PLAYING*/
504         gstcs_debug("Start GST_STATE_PLAYING");
505         ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
506         gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
507
508         /*g_main_loop_run*/
509         gstcs_debug("g_main_loop_run");
510         g_main_loop_run(pGstreamer_s->loop);
511
512         gstcs_debug("Sucess GST_STATE_CHANGE");
513
514         /*GST_STATE_NULL*/
515         gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
516         gstcs_debug("End GST_STATE_NULL");
517
518         gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
519
520         ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
521
522         if (ret_state == GST_STATE_CHANGE_SUCCESS)
523                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
524         else if (ret_state == GST_STATE_CHANGE_ASYNC)
525                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
526
527         gstcs_debug("Success gst_element_get_state\n");
528
529         if (src_caps)
530                 gst_caps_unref(src_caps);
531         if (dst_caps)
532                 gst_caps_unref(dst_caps);
533
534         if (ret_state == GST_STATE_CHANGE_FAILURE) {
535                 gstcs_error("GST_STATE_CHANGE_FAILURE");
536         } else {
537                 if (pGstreamer_s->output_buffer != NULL) {
538                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
539                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
540                         size_t buffer_size = mapinfo.size;
541                         size_t calc_buffer_size = 0;
542
543                         calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);
544
545                         gstcs_debug("buffer size[%zu], calc[%zu]", buffer_size, calc_buffer_size);
546                         if (buffer_size != calc_buffer_size) {
547                                 gstcs_debug("Buffer size is different \n");
548                                 gstcs_debug("unref output buffer");
549                                 gst_buffer_unref(pGstreamer_s->output_buffer);
550                                 gst_object_unref(pGstreamer_s->pipeline);
551                                 pGstreamer_s->output_buffer = NULL;
552                                 return GSTCS_ERROR_INVALID_OPERATION;
553                         }
554                         gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);
555                         *dst = calloc(1, buffer_size);
556                         if (*dst == NULL) {
557                                 gstcs_error("ERROR - calloc ");
558                                 gst_buffer_unref(pGstreamer_s->output_buffer);
559                                 gst_object_unref(pGstreamer_s->pipeline);
560                                 pGstreamer_s->output_buffer = NULL;
561                                 return GSTCS_ERROR_INVALID_OPERATION;
562                         }
563
564                         memcpy(*dst, mapinfo.data, buffer_size);
565                         pImgp_info->buffer_size = buffer_size;
566                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
567                 } else {
568                         gstcs_debug("pGstreamer_s->output_buffer is NULL");
569                 }
570         }
571         gstcs_debug("unref output buffer");
572         gst_buffer_unref(pGstreamer_s->output_buffer);
573         gst_object_unref(pGstreamer_s->pipeline);
574         pGstreamer_s->output_buffer = NULL;
575
576         gstcs_debug("dst: %p", *dst);
577         gstcs_fleave();
578
579         return ret;
580 }
581
582 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
583 {
584         gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");
585
586         gstreamer->context = g_main_context_new();
587         gstcs_retvm_if(gstreamer->context == NULL, GSTCS_ERROR_INVALID_OPERATION, "ERROR - g_main_context_new");
588
589         gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
590         if (gstreamer->loop == NULL) {
591                 gstcs_error("ERROR - g_main_loop_new ");
592                 g_main_context_unref(gstreamer->context);
593                 return GSTCS_ERROR_INVALID_OPERATION;
594         }
595
596         g_main_context_push_thread_default(gstreamer->context);
597
598         return GSTCS_ERROR_NONE;
599 }
600
601 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
602 {
603         gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");
604
605         if (gstreamer->loop != NULL)
606                 g_main_loop_unref(gstreamer->loop);
607
608         if (gstreamer->context != NULL)
609                 g_main_context_unref(gstreamer->context);
610
611         return GSTCS_ERROR_NONE;
612 }
613
614 static int _gstcs_init(gstreamer_s** gstreamer)
615 {
616         static const int max_argc = 50;
617         gint argc = 0;
618         gchar** argv = NULL;
619         int i = 0;
620         int ret = GSTCS_ERROR_NONE;
621
622         argv = malloc(sizeof(gchar*) * max_argc);
623
624         gstcs_retvm_if(argv == NULL, GSTCS_ERROR_OUT_OF_MEMORY, "argv is not allocated");
625
626         memset(argv, 0, sizeof(gchar*) * max_argc);
627
628         argv[argc] = g_strdup("mmutil_gstcs");
629         if (argv[argc] == NULL) {
630                 gstcs_error("argv[%d] is not allocated", argc);
631                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
632         }
633         argc++;
634         /* check disable registry scan */
635         argv[argc] = g_strdup("--gst-disable-registry-update");
636         if (argv[argc] == NULL) {
637                 gstcs_error("argv[%d] is not allocated", argc);
638                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
639         }
640         argc++;
641         if (ret != GSTCS_ERROR_NONE) {
642                 for (i = 0; i < argc; i++)
643                         GSTCS_FREE(argv[i]);
644
645                 GSTCS_FREE(argv);
646                 return ret;
647         }
648
649         gst_init(&argc, &argv);
650
651         *gstreamer = g_new0(gstreamer_s, 1);
652         if (*gstreamer == NULL) {
653                 gstcs_error("gstreamer structure is not allocated");
654                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
655         }
656
657         for (i = 0; i < argc; i++)
658                 GSTCS_FREE(argv[i]);
659
660         GSTCS_FREE(argv);
661         return ret;
662 }
663
664 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
665 {
666         gstreamer_s* pGstreamer_s;
667         int ret = GSTCS_ERROR_NONE;
668
669         /* Print debug message for inout structure */
670         gstcs_debug("src_width [%d] src_height [%d] src_format [%d] dst_width [%d] dst_height [%d] dst_format [%d] rotation [%d]",
671         pImgp_info->src_width, pImgp_info->src_height, pImgp_info->src_format, pImgp_info->dst_width, pImgp_info->dst_height, pImgp_info->dst_format, pImgp_info->angle);
672
673         /* Initialize gstreamer */
674         ret = _gstcs_init(&pGstreamer_s);
675         if (ret != GSTCS_ERROR_NONE) {
676                 gstcs_error("Error: _gstcs_new is failed");
677                 return ret;
678         }
679
680         /* Create default thread for async behavior */
681         ret = _gstcs_create_default_thread(pGstreamer_s);
682         if (ret != GSTCS_ERROR_NONE) {
683                 gstcs_error("Error: _gstcs_create_default_thread is failed");
684                 GSTCS_FREE(pGstreamer_s);
685                 return ret;
686         }
687
688         /* Do gstreamer processing */
689         ret = _mm_imgp_gstcs_processing(pGstreamer_s, src, dst, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */
690
691         if (ret == GSTCS_ERROR_NONE)
692                 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst);
693         else if (ret != GSTCS_ERROR_NONE)
694                 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
695
696         /* Free resouces */
697         ret = _gstcs_destroy_default_thread(pGstreamer_s);
698         if (ret != GSTCS_ERROR_NONE)
699                 gstcs_error("Error: _gstcs_create_default_thread is failed");
700
701         GSTCS_FREE(pGstreamer_s);
702
703         return ret;
704 }
705
706 static bool _mm_imgp_check_format(mm_util_color_format_e color_format)
707 {
708         if ((color_format >= MM_UTIL_COLOR_YUV420) && (color_format <= MM_UTIL_COLOR_BGRX))
709                 return TRUE;
710
711         return FALSE;
712 }
713
714 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
715 {
716         gstcs_retvm_if(pImgp_info == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid info");
717         gstcs_retvm_if(src == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid src");
718         gstcs_retvm_if(dst == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid dst");
719         gstcs_retvm_if((_imgp_type < 0 || _imgp_type > IMGP_MAX), GSTCS_ERROR_INVALID_PARAMETER, "Invalid _imgp_type[%d]", _imgp_type);
720         gstcs_retvm_if((_mm_imgp_check_format(pImgp_info->src_format) == FALSE), GSTCS_ERROR_NOT_SUPPORTED_FORMAT, "not supported src_format [%d]", pImgp_info->src_format);
721         gstcs_retvm_if((_mm_imgp_check_format(pImgp_info->dst_format) == FALSE), GSTCS_ERROR_NOT_SUPPORTED_FORMAT, "not supported dst_format [%d]", pImgp_info->dst_format);
722
723         gstcs_debug("[src %p] [dst %p]", src, dst);
724
725         return _mm_imgp_gstcs(pImgp_info, src, dst);
726 }