Replace g_malloc to malloc
[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 _mm_create_pipeline(gstreamer_s* pGstreamer_s)
117 {
118         int ret = GSTCS_ERROR_NONE;
119         pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
120         if (!pGstreamer_s->pipeline) {
121                 gstcs_error("pipeline could not be created. Exiting.\n");
122                 ret = GSTCS_ERROR_INVALID_PARAMETER;
123         }
124         pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
125         if (!pGstreamer_s->appsrc) {
126                 gstcs_error("appsrc could not be created. Exiting.\n");
127                 ret = GSTCS_ERROR_INVALID_PARAMETER;
128         }
129         pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
130         if (!pGstreamer_s->colorspace) {
131                 gstcs_error("colorspace could not be created. Exiting.\n");
132                 ret = GSTCS_ERROR_INVALID_PARAMETER;
133         }
134         pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
135         if (!pGstreamer_s->videoscale) {
136                 gstcs_error("videoscale could not be created. Exiting.\n");
137                 ret = GSTCS_ERROR_INVALID_PARAMETER;
138         }
139         pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
140         if (!pGstreamer_s->videoflip) {
141                 gstcs_error("videoflip could not be created. Exiting.\n");
142                 ret = GSTCS_ERROR_INVALID_PARAMETER;
143         }
144         pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
145         if (!pGstreamer_s->appsink) {
146                 gstcs_error("appsink could not be created. Exiting.\n");
147                 ret = GSTCS_ERROR_INVALID_PARAMETER;
148         }
149         return ret;
150 }
151
152 static void _mm_destroy_notify(gpointer data)
153 {
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_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL);
174         if (!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL))
175                 gstcs_error("Fail to link pipeline");
176         else
177                 gstcs_debug("Success to link pipeline");
178
179         g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
180         g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
181         g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
182
183         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 */
184
185         /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
186         g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
187 }
188
189 static void
190 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
191 {
192         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
193         GstVideoFormat videoFormat;
194         int _bpp = 0;
195         int _depth = 0;
196         int _red_mask = 0;
197         int _green_mask = 0;
198         int _blue_mask = 0;
199         int _alpha_mask = 0;
200         int _endianness = 0;
201
202         if (__format == NULL) {
203                 gstcs_error("Image format is NULL\n");
204                 return;
205         }
206
207         gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
208         memset(_format_name, 0, sizeof(_format_name));
209
210         if (strcmp(__format->colorspace, "YUV") == 0) {
211                 if (strcmp(__format->format_label, "I420") == 0
212                 || strcmp(__format->format_label, "Y42B") == 0
213                 || strcmp(__format->format_label, "Y444") == 0
214                 || strcmp(__format->format_label, "YV12") == 0
215                 || strcmp(__format->format_label, "NV12") == 0
216                 || strcmp(__format->format_label, "UYVY") == 0) {
217                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
218                 } else if (strcmp(__format->format_label, "YUYV") == 0) {
219                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
220                 }
221                 gstcs_debug("Chosen video format: %s", _format_name);
222                 __format->caps = gst_caps_new_simple("video/x-raw",
223                         "format", G_TYPE_STRING, _format_name,
224                         "framerate", GST_TYPE_FRACTION, 25, 1,
225                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
226                         "width", G_TYPE_INT, __format->width,
227                         "height", G_TYPE_INT, __format->height,
228                         "framerate", GST_TYPE_FRACTION, 1, 1,
229                         NULL);
230         }
231
232         else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
233                 if (strcmp(__format->format_label, "RGB888") == 0) {
234                         _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
235                 } else if (strcmp(__format->format_label, "BGR888") == 0) {
236                         _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
237                 } else if (strcmp(__format->format_label, "RGB565") == 0) {
238                         _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
239                 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
240                         _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
241                 }
242
243                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
244                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
245
246                 __format->caps = gst_caps_new_simple("video/x-raw",
247                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
248                         "width", G_TYPE_INT, __format->stride,
249                         "height", G_TYPE_INT, __format->elevation,
250                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
251         }
252
253         else if (strcmp(__format->colorspace, "RGBA") == 0) {
254                 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
255                         gstcs_debug("ARGB8888");
256                         _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
257                 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
258                         gstcs_debug("BGRA8888");
259                         _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
260                 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
261                         gstcs_debug("RGBA8888");
262                         _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
263                 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
264                         gstcs_debug("ABGR8888");
265                         _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
266                 } else {
267                         gstcs_error("***Wrong format cs type***\n");
268                 }
269
270                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
271                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
272
273                 __format->caps = gst_caps_new_simple("video/x-raw",
274                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
275                         "width", G_TYPE_INT, __format->width,
276                         "height", G_TYPE_INT, __format->elevation,
277                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
278         }
279
280         if (__format->caps) {
281                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
282                 _mm_check_caps_format(__format->caps);
283         } else {
284                 gstcs_error("__format->caps is NULL");
285         }
286 }
287
288 static void
289 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
290 {
291         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
292         GstVideoFormat videoFormat;
293         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;
294
295         if (__format == NULL) {
296                 gstcs_error("Image format is NULL\n");
297                 return;
298         }
299
300         gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
301         memset(_format_name, 0, sizeof(_format_name));
302
303         if (strcmp(__format->colorspace, "YUV") == 0) {
304                 if (strcmp(__format->format_label, "I420") == 0
305                 || strcmp(__format->format_label, "Y42B") == 0
306                 || strcmp(__format->format_label, "Y444") == 0
307                 || strcmp(__format->format_label, "YV12") == 0
308                 || strcmp(__format->format_label, "NV12") == 0
309                 || strcmp(__format->format_label, "UYVY") == 0) {
310                         strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
311                 } else if (strcmp(__format->format_label, "YUYV") == 0) {
312                         strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
313                 }
314                 gstcs_debug("Chosen video format: %s", _format_name);
315                 __format->caps = gst_caps_new_simple("video/x-raw",
316                         "format", G_TYPE_STRING, _format_name,
317                         "width", G_TYPE_INT, __format->width,
318                         "height", G_TYPE_INT, __format->height,
319                         "framerate", GST_TYPE_FRACTION, 1, 1,
320                         NULL);
321         }
322
323         else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
324                 if (strcmp(__format->format_label, "RGB888") == 0) {
325                         _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
326                 } else if (strcmp(__format->format_label, "BGR888") == 0) {
327                         _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
328                 } else if (strcmp(__format->format_label, "RGB565") == 0) {
329                         _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
330                 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
331                         _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
332                 }
333
334                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
335                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
336
337                 __format->caps = gst_caps_new_simple("video/x-raw",
338                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
339                         "width", G_TYPE_INT, __format->width,
340                         "height", G_TYPE_INT, __format->height,
341                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
342         }
343
344         else if (strcmp(__format->colorspace, "RGBA") == 0) {
345                 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
346                         gstcs_debug("ARGB8888");
347                         _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
348                 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
349                         gstcs_debug("BGRA8888");
350                         _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
351                 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
352                         gstcs_debug("RGBA8888");
353                         _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
354                 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
355                         gstcs_debug("ABGR8888");
356                         _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
357                 } else {
358                         gstcs_error("***Wrong format cs type***\n");
359                 }
360
361                 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
362                 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
363
364                 __format->caps = gst_caps_new_simple("video/x-raw",
365                         "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
366                         "width", G_TYPE_INT, __format->width,
367                         "height", G_TYPE_INT, __format->height,
368                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
369         }
370
371         if (__format->caps) {
372                 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
373                 _mm_check_caps_format(__format->caps);
374         } else {
375                 gstcs_error("__format->caps is NULL");
376         }
377 }
378
379 static void
380 _mm_set_image_colorspace(image_format_s* __format)
381 {
382         gstcs_debug("format_label: %s\n", __format->format_label);
383
384         __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
385         if (__format->colorspace == NULL) {
386                 gstcs_error("memory allocation failed");
387                 return;
388         }
389         memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
390         if ((strcmp(__format->format_label, "I420") == 0) || (strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
391                 || (strcmp(__format->format_label, "YV12") == 0) || (strcmp(__format->format_label, "NV12") == 0) || (strcmp(__format->format_label, "UYVY") == 0) || (strcmp(__format->format_label, "YUYV") == 0)) {
392                 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
393         } else if ((strcmp(__format->format_label, "RGB888") == 0) || (strcmp(__format->format_label, "BGR888") == 0) || (strcmp(__format->format_label, "RGB565") == 0)) {
394                 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
395         } 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)) {
396                 strncpy(__format->colorspace, "RGBA", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
397         } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
398                 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
399         } else {
400                 gstcs_error("Check your colorspace format label");
401                 GSTCS_FREE(__format->colorspace);
402         }
403 }
404
405 static int _gstcs_create_image_format(image_format_s **format)
406 {
407         int ret = GSTCS_ERROR_NONE;
408         image_format_s *_format = NULL;
409
410         if (format == NULL) {
411                 gstcs_error("format is wrong value");
412                 return GSTCS_ERROR_INVALID_OPERATION;
413         }
414
415         _format = (image_format_s*)malloc(sizeof(image_format_s));
416         if (_format == NULL) {
417                 gstcs_error("memory allocation failed");
418                 return GSTCS_ERROR_OUT_OF_MEMORY;
419         }
420         memset(_format, 0, sizeof(image_format_s));
421         *format = _format;
422
423         return ret;
424 }
425
426 static void _gstcs_destroy_image_format(image_format_s *format)
427 {
428         if (format != NULL) {
429                 GSTCS_FREE(format->format_label);
430                 GSTCS_FREE(format->colorspace);
431                 GSTCS_FREE(format);
432         }
433 }
434
435 static void
436 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
437 {
438         if (strcmp(pFormat->colorspace, "YUV") == 0) {
439                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
440                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
441         } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
442                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
443                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
444         } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
445                 pFormat->stride = pFormat->width;
446                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
447         }
448         gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
449 }
450
451 static image_format_s*
452 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
453 {
454         int ret = GSTCS_ERROR_NONE;
455         image_format_s* __format = NULL;
456
457         ret = _gstcs_create_image_format(&__format);
458         if (ret != GSTCS_ERROR_NONE) {
459                 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
460                 return NULL;
461         }
462
463         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
464         if (__format->format_label == NULL) {
465                 gstcs_error("memory allocation failed");
466                 _gstcs_destroy_image_format(__format);
467                 return NULL;
468         }
469         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
470         strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
471         gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
472         _mm_set_image_colorspace(__format);
473
474         __format->width = pImgp_info->src_width;
475         __format->height = pImgp_info->src_height;
476         _mm_round_up_input_image_widh_height(__format);
477
478         __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
479         gstcs_debug("blocksize: %d\n", __format->blocksize);
480         _mm_set_image_input_format_s_capabilities(__format);
481
482         return __format;
483 }
484
485 static void
486 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
487 {
488         if (strcmp(pFormat->colorspace, "YUV") == 0) {
489                 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
490                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
491         } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
492                 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
493                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
494                 pFormat->width = pFormat->stride;
495                 if (input_format->height != input_format->elevation)
496                         pFormat->height = pFormat->elevation;
497         } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
498                 pFormat->stride = pFormat->width;
499                 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
500                 if (input_format->height != input_format->elevation)
501                         pFormat->height = pFormat->elevation;
502         }
503         gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
504 }
505
506 static image_format_s*
507 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
508 {
509         int ret = GSTCS_ERROR_NONE;
510         image_format_s* __format = NULL;
511
512         ret = _gstcs_create_image_format(&__format);
513         if (ret != GSTCS_ERROR_NONE) {
514                 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
515                 return NULL;
516         }
517
518         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
519         if (__format->format_label == NULL) {
520                 gstcs_error("memory allocation failed");
521                 _gstcs_destroy_image_format(__format);
522                 return NULL;
523         }
524         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
525         strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
526         _mm_set_image_colorspace(__format);
527
528         __format->width = pImgp_info->dst_width;
529         __format->height = pImgp_info->dst_height;
530         _mm_round_up_output_image_widh_height(__format, input_format);
531
532         pImgp_info->output_stride = __format->stride;
533         pImgp_info->output_elevation = __format->elevation;
534
535         __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
536         gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
537         _mm_set_image_output_format_s_capabilities(__format);
538         return __format;
539 }
540
541 static int
542 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
543 {
544         int ret = GSTCS_ERROR_NONE;
545
546         if (pGstreamer_s->pipeline == NULL) {
547                 gstcs_error("pipeline is NULL\n");
548                 return GSTCS_ERROR_INVALID_PARAMETER;
549         }
550
551         gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
552         GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
553
554         if (gst_buf == NULL) {
555                 gstcs_error("buffer is NULL\n");
556                 return GSTCS_ERROR_INVALID_PARAMETER;
557         }
558
559         gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
560         return ret;
561 }
562
563 static int
564 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
565 {
566         int ret = GSTCS_ERROR_NONE;
567         GstBuffer *gst_buf = NULL;
568         unsigned int src_size = 0;
569         unsigned char *data = NULL;
570         unsigned int stride = input_format->stride;
571         unsigned int elevation = input_format->elevation;
572
573         if (pGstreamer_s->pipeline == NULL) {
574                 gstcs_error("pipeline is NULL\n");
575                 return GSTCS_ERROR_INVALID_PARAMETER;
576         }
577
578         gstcs_debug("stride: %d, elevation: %d", stride, elevation);
579         src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
580         gstcs_debug("buffer size (src): %d", src_size);
581
582         int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
583         unsigned int src_row = input_format->width * byte_per_pixcel;
584         unsigned int stride_row = stride * byte_per_pixcel;
585         unsigned int i = 0, y = 0;
586         gstcs_debug("padding will be inserted to buffer");
587         data = (unsigned char *) malloc(src_size);
588         if (data == NULL) {
589                 gstcs_error("app_buffer is NULL\n");
590                 return GSTCS_ERROR_INVALID_PARAMETER;
591         }
592         for (y = 0; y < (unsigned int)(input_format->height); y++) {
593                 guint8 *pLine = (guint8 *) &(src[src_row * y]);
594                 for (i = 0; i < src_row; i++)
595                         data[y * stride_row + i] = pLine[i];
596
597                 for (i = src_row; i < stride_row; i++)
598                         data[y * stride_row + i] = 0x00;
599         }
600         for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
601                 for (i = 0; i < stride_row; i++)
602                         data[y * stride_row + i] = 0x00;
603         }
604         gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
605
606         if (gst_buf == NULL) {
607                 gstcs_error("buffer is NULL\n");
608                 return GSTCS_ERROR_INVALID_PARAMETER;
609         }
610
611         gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
612         return ret;
613 }
614
615 static int
616 _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)
617 {
618         GstBus *bus = NULL;
619         GstStateChangeReturn ret_state;
620         int ret = GSTCS_ERROR_NONE;
621
622         /*create pipeline*/
623         ret = _mm_create_pipeline(pGstreamer_s);
624         if (ret != GSTCS_ERROR_NONE)
625                 gstcs_error("ERROR - mm_create_pipeline ");
626
627         /* 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. */
628         gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
629
630         bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
631         gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
632         gst_object_unref(bus);
633
634         gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
635         gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
636
637         if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
638                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
639                 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
640                 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
641         } else {
642                 gstcs_debug("Start mm_push_buffer_into_pipeline");
643                 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
644         }
645         if (ret != GSTCS_ERROR_NONE) {
646                 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
647                 gst_object_unref(pGstreamer_s->pipeline);
648                 return ret;
649         }
650         gstcs_debug("End mm_push_buffer_into_pipeline");
651
652         /*link pipeline*/
653         gstcs_debug("Start mm_link_pipeline");
654         _mm_link_pipeline(pGstreamer_s, input_format, output_format, pImgp_info->angle);
655         gstcs_debug("End mm_link_pipeline");
656
657         /* Conecting to the new-sample signal emited by the appsink*/
658         gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
659         g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
660         gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
661
662         /* GST_STATE_PLAYING*/
663         gstcs_debug("Start GST_STATE_PLAYING");
664         ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
665         gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
666
667         /*g_main_loop_run*/
668         gstcs_debug("g_main_loop_run");
669         g_main_loop_run(pGstreamer_s->loop);
670
671         gstcs_debug("Sucess GST_STATE_CHANGE");
672
673         /*GST_STATE_NULL*/
674         gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
675         gstcs_debug("End GST_STATE_NULL");
676
677         gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
678
679         ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
680
681         if (ret_state == GST_STATE_CHANGE_SUCCESS)
682                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
683         else if (ret_state == GST_STATE_CHANGE_ASYNC)
684                 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
685
686         gstcs_debug("Success gst_element_get_state\n");
687
688         if (ret_state == GST_STATE_CHANGE_FAILURE) {
689                 gstcs_error("GST_STATE_CHANGE_FAILURE");
690         } else {
691                 if (pGstreamer_s->output_buffer != NULL) {
692                         GstMapInfo mapinfo = GST_MAP_INFO_INIT;
693                         gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
694                         int buffer_size = mapinfo.size;
695                         int calc_buffer_size = 0;
696                         if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
697                                 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
698                                 gstcs_debug("calculate image size with stride & elevation");
699                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
700                         } else {
701                                 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
702                         }
703                         gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
704                         if (buffer_size != calc_buffer_size) {
705                                 gstcs_debug("Buffer size is different \n");
706                                 gstcs_debug("unref output buffer");
707                                 gst_buffer_unref(pGstreamer_s->output_buffer);
708                                 gst_object_unref(pGstreamer_s->pipeline);
709                                 pGstreamer_s->output_buffer = NULL;
710                                 return GSTCS_ERROR_INVALID_OPERATION;
711                         }
712                         gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
713                         memcpy(dst, mapinfo.data, buffer_size);
714                         pImgp_info->buffer_size = buffer_size;
715                         gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
716                 } else {
717                         gstcs_debug("pGstreamer_s->output_buffer is NULL");
718                 }
719         }
720         gstcs_debug("unref output buffer");
721         gst_buffer_unref(pGstreamer_s->output_buffer);
722         gst_object_unref(pGstreamer_s->pipeline);
723         pGstreamer_s->output_buffer = NULL;
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 _gstcs_create_default_thread(gstreamer_s *gstreamer)
776 {
777         if (gstreamer == NULL) {
778                 gstcs_error("ERROR - gstreamer is null ");
779                 return GSTCS_ERROR_INVALID_OPERATION;
780         }
781
782         gstreamer->context = g_main_context_new();
783         if (gstreamer->context == NULL) {
784                 gstcs_error("ERROR - g_main_context_new ");
785                 return GSTCS_ERROR_INVALID_OPERATION;
786         }
787         gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
788         if (gstreamer->loop == NULL) {
789                 gstcs_error("ERROR - g_main_loop_new ");
790                 g_main_context_unref(gstreamer->context);
791                 return GSTCS_ERROR_INVALID_OPERATION;
792         }
793
794         g_main_context_push_thread_default(gstreamer->context);
795         return GSTCS_ERROR_NONE;
796 }
797
798 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
799 {
800         if (gstreamer == NULL) {
801                 gstcs_error("ERROR - gstreamer is null ");
802                 return GSTCS_ERROR_INVALID_OPERATION;
803         }
804
805         if (gstreamer->loop != NULL)
806                 g_main_loop_unref(gstreamer->loop);
807
808         if (gstreamer->context != NULL)
809                 g_main_context_unref(gstreamer->context);
810
811         return GSTCS_ERROR_NONE;
812 }
813
814 static int _gstcs_init(gstreamer_s** gstreamer)
815 {
816         static const int max_argc = 50;
817         gint argc = 0;
818         gchar** argv = NULL;
819         int i = 0;
820         int ret = GSTCS_ERROR_NONE;
821
822         argv = malloc(sizeof(gchar*) * max_argc);
823
824         if (!argv) {
825                 gstcs_error("argv is not allocated");
826                 return GSTCS_ERROR_OUT_OF_MEMORY;
827         }
828         memset(argv, 0, sizeof(gchar*) * max_argc);
829
830         argv[argc] = g_strdup("mmutil_gstcs");
831         if (argv[argc] == NULL) {
832                 gstcs_error("argv[%d] is not allocated", argc);
833                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
834         }
835         argc++;
836         /* check disable registry scan */
837         argv[argc] = g_strdup("--gst-disable-registry-update");
838         if (argv[argc] == NULL) {
839                 gstcs_error("argv[%d] is not allocated", argc);
840                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
841         }
842         argc++;
843         if (ret != GSTCS_ERROR_NONE) {
844                 for (i = 0; i < argc; i++)
845                         GSTCS_FREE(argv[i]);
846
847                 GSTCS_FREE(argv);
848                 return ret;
849         }
850
851         gst_init(&argc, &argv);
852
853         *gstreamer = g_new0(gstreamer_s, 1);
854         if (*gstreamer == NULL) {
855                 gstcs_error("gstreamer structure is not allocated");
856                 ret = GSTCS_ERROR_OUT_OF_MEMORY;
857         }
858
859         for (i = 0; i < argc; i++)
860                 GSTCS_FREE(argv[i]);
861
862         GSTCS_FREE(argv);
863         return ret;
864 }
865
866 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
867 {
868         image_format_s* input_format = NULL, *output_format = NULL;
869         gstreamer_s* pGstreamer_s;
870         int ret = GSTCS_ERROR_NONE;
871
872         /* Print debug message for inout structure */
873         gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
874         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);
875
876         /* Initialize gstreamer */
877         ret = _gstcs_init(&pGstreamer_s);
878         if (ret != GSTCS_ERROR_NONE) {
879                 gstcs_error("Error: _gstcs_new is failed");
880                 return ret;
881         }
882
883         /* Create input/output format for gstreamer processing */
884         input_format = _mm_set_input_image_format_s_struct(pImgp_info);
885         if (input_format == NULL) {
886                 gstcs_error("Error: memory allocation failed");
887                 GSTCS_FREE(pGstreamer_s);
888                 return GSTCS_ERROR_OUT_OF_MEMORY;
889         }
890
891         output_format = _mm_set_output_image_format_s_struct(pImgp_info, input_format);
892         if (output_format == NULL) {
893                 gstcs_error("Error: memory allocation failed");
894                 _gstcs_destroy_image_format(input_format);
895                 GSTCS_FREE(pGstreamer_s);
896                 return GSTCS_ERROR_OUT_OF_MEMORY;
897         }
898
899         /* Create default thread for async behavior */
900         ret = _gstcs_create_default_thread(pGstreamer_s);
901         if (ret != GSTCS_ERROR_NONE) {
902                 gstcs_error("Error: _gstcs_create_default_thread is failed");
903                 _gstcs_destroy_image_format(input_format);
904                 _gstcs_destroy_image_format(output_format);
905                 GSTCS_FREE(pGstreamer_s);
906                 return ret;
907         }
908
909         /* Do gstreamer processing */
910         gstcs_debug("Start _mm_imgp_gstcs_processing ");
911         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 */
912
913         if (ret == GSTCS_ERROR_NONE)
914                 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
915         else if (ret != GSTCS_ERROR_NONE)
916                 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
917
918         /* Free resouces */
919         ret = _gstcs_destroy_default_thread(pGstreamer_s);
920         if (ret != GSTCS_ERROR_NONE)
921                 gstcs_error("Error: _gstcs_create_default_thread is failed");
922
923         _gstcs_destroy_image_format(input_format);
924         _gstcs_destroy_image_format(output_format);
925         GSTCS_FREE(pGstreamer_s);
926
927         return ret;
928 }
929
930 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
931 {
932         if (pImgp_info == NULL) {
933                 gstcs_error("Error: input vaule is NULL");
934                 return GSTCS_ERROR_INVALID_PARAMETER;
935         }
936
937         if (src == NULL || dst == NULL) {
938                 gstcs_error("Error: src | dst is NULL");
939                 return GSTCS_ERROR_INVALID_PARAMETER;
940         }
941
942         if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
943                 gstcs_error("Error: imgp_type is wrong");
944                 return GSTCS_ERROR_INVALID_PARAMETER;
945         }
946
947         gstcs_debug("[src %p] [dst %p]", src, dst);
948
949         return _mm_imgp_gstcs(pImgp_info, src, dst);
950 }