update the prevent issue
[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 #define setup_image_size_I420(width, height) { \
32         int size=0; \
33         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); \
34         return size; \
35 }
36
37 #define setup_image_size_Y42B(width, height) { \
38         int size=0; \
39         size = (MM_UTIL_ROUND_UP_4 (width) * height + MM_UTIL_ROUND_UP_8 (width) * height); \
40         return size; \
41 }
42
43 #define setup_image_size_Y444(width, height) { \
44         int size=0; \
45         size = (MM_UTIL_ROUND_UP_4 (width) * height * 3); \
46         return size; \
47 }
48
49 #define setup_image_size_UYVY(width, height) { \
50         int size=0; \
51         size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
52         return size; \
53 }
54
55 #define setup_image_size_YUYV(width, height) { \
56         int size=0; \
57         size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
58         return size; \
59 }
60
61 #define setup_image_size_YV12(width, height) { \
62         int size=0; \
63         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); \
64         return size; \
65 }
66
67 #define setup_image_size_NV12(width, height) { \
68         int size=0; \
69         size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5); \
70         return size; \
71 }
72
73 #define setup_image_size_RGB565(width, height) { \
74         int size=0; \
75         size = (MM_UTIL_ROUND_UP_4 (width * 2) *height); \
76         return size; \
77 }
78
79 #define setup_image_size_RGB888(width, height) { \
80         int size=0; \
81         size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
82         return size; \
83 }
84
85 #define setup_image_size_BGR888(width, height) { \
86         int size=0; \
87         size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
88         return size; \
89 }
90 /*########################################################################################*/
91
92 static void
93 _mm_sink_buffer (GstElement * appsink, gpointer user_data)
94 {
95         GstBuffer *_buf=NULL;
96         GstSample *_sample = NULL;
97         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
98         _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
99         _buf = gst_sample_get_buffer(_sample);
100
101         pGstreamer_s->output_buffer = _buf;
102
103         if(pGstreamer_s->output_buffer != NULL) {
104             GstMapInfo mapinfo = GST_MAP_INFO_INIT;
105             gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
106             debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n", mapinfo.data, mapinfo.size);
107             gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
108         }else {
109                 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
110         }
111         gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
112         gst_sample_unref(_sample);
113 }
114
115 static void
116 _mm_sink_preroll (GstElement * appsink, gpointer user_data)
117 {
118         GstSample *_sample = NULL;
119         GstBuffer *_buf=NULL;
120         gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
121
122         _sample = gst_app_sink_pull_preroll((GstAppSink*)appsink);
123         _buf = gst_sample_get_buffer(_sample);
124
125         pGstreamer_s->output_buffer = _buf;
126         /* pGstreamer_s->output_image_format_s->caps = GST_BUFFER_CAPS(_buf); */
127         if(pGstreamer_s->output_buffer != NULL) {
128                 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
129                 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
130                 debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n",
131                 mapinfo.data, mapinfo.size);
132                 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
133         } else {
134                 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
135         }
136         gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushings */
137         gst_sample_unref(_sample);
138 }
139
140 static gboolean
141 _mm_on_sink_message (GstBus * bus, GstMessage * message, gstreamer_s * pGstreamer_s)
142 {
143         switch (GST_MESSAGE_TYPE (message)) {
144                 case GST_MESSAGE_EOS:
145                         debug_log("Finished playback\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
146                         break;
147                 case GST_MESSAGE_ERROR:
148                         debug_error("Received error\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
149                         break;
150                 case GST_MESSAGE_STATE_CHANGED:
151                         debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
152                         break;
153                 case GST_MESSAGE_STREAM_STATUS:
154                         debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
155                         break;
156                 default:
157                         debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
158                         break;
159         }
160         return TRUE;
161 }
162
163
164 static int
165 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
166 {
167         int ret = MM_ERROR_NONE;
168         pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
169         pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
170         pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorconverter");
171
172         pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
173         pGstreamer_s->videoflip=gst_element_factory_make( "videoflip", "flip" );
174
175         pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
176
177         if (!pGstreamer_s->pipeline || !pGstreamer_s->appsrc||!pGstreamer_s->colorspace || !pGstreamer_s->appsink) {
178                 debug_error("One element could not be created. Exiting.\n");
179                 ret = MM_ERROR_IMAGE_INVALID_VALUE;
180         }
181         return ret;
182 }
183
184 static gboolean
185 _mm_check_resize_format(int src_width, int src_height, int dst_width, int dst_height)
186 {
187         gboolean _bool=FALSE;
188
189         if( (src_width != dst_width) || (src_height != dst_height) ) {
190                 _bool = TRUE;
191         }
192
193         return _bool;
194 }
195
196 static gboolean
197 _mm_check_rotate_format(int GstVideoFlipMethod)
198 {
199         gboolean _bool=FALSE;
200
201         if((GstVideoFlipMethod >= 1) && ( GstVideoFlipMethod<= 7)) {
202                 _bool = TRUE;
203         }
204
205         return _bool;
206 }
207
208 static gboolean
209 _mm_check_resize_format_label(char* __format_label)
210 {
211         gboolean _bool = FALSE;
212
213         if(strcmp(__format_label, "AYUV") == 0
214                 || strcmp(__format_label, "UYVY") == 0 ||strcmp(__format_label, "Y800") == 0 || strcmp(__format_label, "I420") == 0 || strcmp(__format_label, "YV12") == 0
215                 || strcmp(__format_label, "RGB888") == 0 || strcmp(__format_label, "RGB565") == 0 || strcmp(__format_label, "BGR888") == 0 || strcmp(__format_label, "RGBA8888") == 0
216                 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ||strcmp(__format_label, "RGBX") == 0
217                 ||strcmp(__format_label, "XRGB") == 0 ||strcmp(__format_label, "BGRX") == 0 ||strcmp(__format_label, "XBGR") == 0 ||strcmp(__format_label, "Y444") == 0
218                 ||strcmp(__format_label, "Y42B") == 0 ||strcmp(__format_label, "YUY2") == 0 ||strcmp(__format_label, "YUYV") == 0 ||strcmp(__format_label, "UYVY") == 0
219                 ||strcmp(__format_label, "Y41B") == 0 ||strcmp(__format_label, "Y16") == 0 ||strcmp(__format_label, "Y800") == 0 ||strcmp(__format_label, "Y8") == 0
220                 ||strcmp(__format_label, "GREY") == 0 ||strcmp(__format_label, "AY64") == 0 || strcmp(__format_label, "YUV422") == 0) {
221
222                 _bool=TRUE;
223         }
224
225         return _bool;
226 }
227
228 static gboolean
229 _mm_check_rotate_format_label(const char* __format_label)
230 {
231         gboolean _bool = FALSE;
232
233         if(strcmp(__format_label, "I420") == 0 ||strcmp(__format_label, "YV12") == 0 || strcmp(__format_label, "IYUV")
234                 || strcmp(__format_label, "RGB888") ||strcmp(__format_label, "BGR888") == 0 ||strcmp(__format_label, "RGBA8888") == 0
235                 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ) {
236                 _bool=TRUE;
237         }
238
239         return _bool;
240 }
241
242 static void
243 _mm_link_pipeline_order_csc_rsz(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
244 {
245         if(_mm_check_resize_format(input_format->width,input_format->height, output_format->width, output_format->height))      {
246                 debug_log("check_for_resize");
247                 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
248                 debug_log("gst_bin_add_many");
249                 if(_mm_check_resize_format_label( input_format->format_label)) {
250                         debug_log(" input_format->format_label: %s", input_format->format_label);
251                         if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
252                                 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
253                         }
254                 }else if(_mm_check_resize_format_label(output_format->format_label)) {
255                         debug_log("output_format->format_label: %s", output_format->format_label);
256                         if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->appsink, NULL)) {
257                                 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
258                         }
259                 }
260         }else {
261                 debug_log("check_for_convert");
262                 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
263                 debug_log("gst_bin_add_many");
264                 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
265                         debug_error("Fail to link b/w ffmpeg and appsink except rsz & rot\n");
266                 }
267         }
268 }
269
270 static void
271 _mm_link_pipeline_order_csc_rsz_rot(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
272 {
273         if(_mm_check_rotate_format_label(input_format->format_label)) {
274                 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
275                 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
276                         debug_error("Fail to link b/w appsrc and ffmpeg in rotate\n");
277                 }
278         }else if(_mm_check_rotate_format_label(output_format->format_label)) {
279                 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);
280                 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
281                         debug_error("Fail to link b/w ffmpeg and appsink in rotate\n");
282                 }
283         }
284 }
285
286
287 static void
288 _mm_link_pipeline( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int _valuepGstreamer_sVideoFlipMethod)
289 {
290         /* set property */
291         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); you can use appsrc'cap property */
292         g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
293         g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
294         g_object_set (pGstreamer_s->appsrc, "format", GST_FORMAT_TIME, NULL);
295         g_object_set(pGstreamer_s->appsrc, "stream-type", 0 /*stream*/, NULL);
296
297         g_object_set(pGstreamer_s->videoflip, "method", _valuepGstreamer_sVideoFlipMethod, 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 */
298
299         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); */
300         g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);
301         g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
302
303         if(_mm_check_rotate_format(_valuepGstreamer_sVideoFlipMethod)) { /* when you want to rotate image */
304                 /* because IYUV, I420, YV12 format can use vidoeflip*/
305                 debug_log("set_link_pipeline_order_csc_rsz_rot");
306                 _mm_link_pipeline_order_csc_rsz_rot(pGstreamer_s, input_format, output_format);
307         }else {
308                 debug_log("set_link_pipeline_order_csc_rsz");
309                 _mm_link_pipeline_order_csc_rsz(pGstreamer_s, input_format, output_format);
310         }
311         /* Conecting to the new-sample signal emited by the appsink*/
312         debug_log("Start G_CALLBACK (mm_sink_buffer)");
313         g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
314         debug_log("End G_CALLBACK (mm_sink_buffer)");
315 }
316
317
318
319 static void
320 _mm_set_image_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
321 {
322         char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
323         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;
324
325         if(__format == NULL) {
326                 debug_error("Image format is NULL\n");
327                 return;
328         }
329
330         debug_log("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
331
332         if(strcmp(__format->colorspace,"YUV") == 0) {
333                 if(strcmp(__format->format_label,"I420") == 0
334                 || strcmp(__format->format_label,"Y42B") == 0
335                 || strcmp(__format->format_label,"Y444") == 0
336                 || strcmp(__format->format_label,"YV12") == 0
337                 || strcmp(__format->format_label,"NV12") == 0
338                 || strcmp(__format->format_label,"UYVY") == 0) {
339                         strcpy(_format_name, __format->format_label);
340                 }else if(strcmp(__format->format_label,"YUYV") == 0) {
341                         strcpy(_format_name, "YVYU");
342                 }
343
344                 __format->caps = gst_caps_new_simple ("video/x-raw",
345                         "format", G_TYPE_STRING, _format_name,
346                         "framerate", GST_TYPE_FRACTION, 25, 1,
347                         "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
348                         "width", G_TYPE_INT, __format->width,
349                         "height", G_TYPE_INT, __format->height,
350                         "framerate", GST_TYPE_FRACTION, 1, 1,
351                         NULL);
352         }
353
354         else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
355                 if(strcmp(__format->format_label,"RGB888") == 0) {
356                         _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
357                 }else if(strcmp(__format->format_label,"BGR888") == 0) {
358                         _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
359                 }else if(strcmp(__format->format_label,"RGB565") == 0) {
360                         _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
361                 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
362                         _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
363                 }
364                 __format->caps = gst_caps_new_simple ("video/x-raw",
365                         "format", G_TYPE_STRING,  gst_video_format_to_string(
366                         gst_video_format_from_masks(_depth, _bpp, _endianness,
367                         _red_mask, _green_mask, _blue_mask, 0)),
368                         "width", G_TYPE_INT, __format->width,
369                         "height", G_TYPE_INT, __format->height,
370                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
371         }
372
373         else if(strcmp(__format->colorspace,"RGBA") ==0) {
374                 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
375                         _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
376                 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
377                 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
378                 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
379                         _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
380                 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
381                 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
382                 }else {
383                         debug_error("***Wrong format cs type***\n");
384                 }
385
386                 __format->caps = gst_caps_new_simple ("video/x-raw",
387                         "format", G_TYPE_STRING,  gst_video_format_to_string(
388                         gst_video_format_from_masks(_depth, _bpp, _endianness,
389                         _red_mask, _green_mask, _blue_mask, _alpha_mask)),
390                         "width", G_TYPE_INT, __format->width,
391                         "height", G_TYPE_INT, __format->height,
392                         "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
393         }
394         if(__format->caps) {
395                 debug_log("###__format->caps is not NULL###, %p", __format->caps);
396         }else {
397                 debug_error("__format->caps is NULL");
398         }
399 }
400
401 static void
402 _mm_set_image_colorspace( image_format_s* __format)
403 {
404         debug_log("format_label: %s\n", __format->format_label);
405
406         __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
407         memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
408         if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
409                 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
410                 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
411         }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
412                 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
413         }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)) {
414                 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
415         }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
416                 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
417         }else {
418                 debug_error("Check your colorspace format label");
419                 GSTCS_FREE(__format->colorspace);
420         }
421 }
422
423 static image_format_s*
424 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
425 {
426         image_format_s* __format = NULL;
427
428         __format=(image_format_s*)malloc(sizeof(image_format_s));
429
430         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
431         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
432         strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
433         debug_log("input_format_label: %s\n", pImgp_info->input_format_label);
434         _mm_set_image_colorspace(__format);
435
436         __format->width=pImgp_info->src_width;
437         __format->height=pImgp_info->src_height;
438
439         __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
440         debug_log("blocksize: %d\n", __format->blocksize);
441         _mm_set_image_format_s_capabilities(__format);
442
443         return __format;
444 }
445
446 static void
447 _mm_round_up_output_image_widh_height(image_format_s* pFormat)
448 {
449         if(strcmp(pFormat->colorspace,"YUV") ==0) {
450                 pFormat->stride=MM_UTIL_ROUND_UP_8(pFormat->width);
451                 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
452         } else if(strcmp(pFormat->colorspace, "RGB") ==0) {
453                 pFormat->stride=MM_UTIL_ROUND_UP_4(pFormat->width);
454                 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
455         }
456 }
457
458 static image_format_s*
459 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
460 {
461         image_format_s* __format = NULL;
462
463         __format=(image_format_s*)malloc(sizeof(image_format_s));
464
465         __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
466         memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
467         strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
468         _mm_set_image_colorspace(__format);
469
470         __format->width=pImgp_info->dst_width;
471         __format->height=pImgp_info->dst_height;
472         _mm_round_up_output_image_widh_height(__format);
473
474         __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
475         debug_log("output_format_label: %s", pImgp_info->output_format_label);
476         _mm_set_image_format_s_capabilities(__format);
477         debug_log("pImgp_info->dst: %p", pImgp_info->dst);
478         return __format;
479 }
480
481 static gboolean
482 __mm_check_resize_format( char* _in_format_label, int _in_w, int _in_h, char* _out_format_label, int _out_w, int _out_h)
483 {
484         gboolean _bool=TRUE;
485         debug_log("input image format: %s input image width: %d input image height: %d output image format: %s output image width: %d output image height: %d\n",
486         _in_format_label, _in_w,_in_h, _out_format_label, _out_w, _out_h);
487         if(_mm_check_resize_format(_in_w, _in_h, _out_w, _out_h)) {
488                 if( !( _mm_check_resize_format_label(_in_format_label) ||_mm_check_resize_format_label(_out_format_label) ) ) {
489                         debug_error("ERROR-%s | %s Resize Only [AYUV] [YUY2] [YVYU] [UYVY] [Y800] [I420][YV12] format can use vidoeflip ERROR- Resize!!!#####\n", _in_format_label, _out_format_label);
490                         _bool = FALSE;
491                 }
492         }
493         return _bool;
494 }
495
496 static gboolean
497 __mm_check_rotate_format(int angle, const char* input_format_label, const char* output_format_label)
498 {
499         gboolean _bool=TRUE;
500         debug_log("rotate value: %d input_format_label: %s output_format_label: %s\n",
501         angle, input_format_label, output_format_label);
502         if(_mm_check_rotate_format(angle)) {
503                 if(!(_mm_check_rotate_format_label(input_format_label) || _mm_check_rotate_format_label(output_format_label))) {
504                         debug_error("ERROR- %s | %s Rotate Only IYUV, I420, YV12 format can use vidoeflip ERROR -Rotate", input_format_label, output_format_label);
505                         _bool = FALSE;
506                 }
507         }
508         return _bool;
509 }
510
511 static int
512 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, gstreamer_s * pGstreamer_s)
513 {
514         int ret = MM_ERROR_NONE;
515
516         if(pGstreamer_s->pipeline == NULL) {
517                 debug_error("pipeline is NULL\n");
518                 return MM_ERROR_IMAGE_INVALID_VALUE;
519         }
520
521         gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
522     GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, pImgp_info->src, data_size, 0, data_size, NULL, NULL);
523
524         if(gst_buf==NULL) {
525                 debug_error("buffer is NULL\n");
526                 return MM_ERROR_IMAGE_INVALID_VALUE;
527         }
528
529         gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
530         return ret;
531 }
532
533
534 static int
535 _mm_imgp_gstcs_processing( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
536 {
537         GstBus *bus = NULL;
538         GstStateChangeReturn ret_state;
539         int ret = MM_ERROR_NONE;
540         /*create pipeline*/
541         ret = _mm_create_pipeline(pGstreamer_s);
542         if(ret != MM_ERROR_NONE) {
543                 debug_error("ERROR - mm_create_pipeline ");
544         }
545
546
547         /* 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. */
548          gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
549
550         bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline)); /* GST_PIPELINE (pipeline)); */
551         /* gst_bus_add_watch (bus, (GstBusFunc) _mm_on_sink_message, pGstreamer_s); thow to appplicaton */
552         gst_object_unref(bus);
553
554         debug_log("Start mm_push_buffer_into_pipeline");
555         ret = _mm_push_buffer_into_pipeline(pImgp_info, pGstreamer_s);
556         if(ret != MM_ERROR_NONE) {
557                 debug_error("ERROR - mm_push_buffer_into_pipeline ");
558         }
559         debug_log("End mm_push_buffer_into_pipeline");
560
561         /*link pipeline*/
562         debug_log("Start mm_link_pipeline");
563         _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
564         debug_log("End mm_link_pipeline");
565
566         /* Conecting to the new-sample signal emited by the appsink*/
567         debug_log("Start G_CALLBACK (_mm_sink_preroll)");
568         g_signal_connect (pGstreamer_s->appsink, "new-preroll", G_CALLBACK (_mm_sink_preroll), pGstreamer_s);
569         debug_log("End G_CALLBACK (mm_sink_preroll)");
570
571         /* GST_STATE_PLAYING*/
572         debug_log("Start GST_STATE_PLAYING");
573         ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
574         debug_log("End GST_STATE_PLAYING ret_state: %d", ret_state);
575         ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
576         debug_log("Success Get GST_STATE_PLAYING ret_state: %d", ret_state);
577         #if 0
578         /* Conecting to the new-sample signal emited by the appsink*/
579         debug_log("Start G_CALLBACK (mm_sink_buffer)");
580         g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
581         debug_log("End G_CALLBACK (mm_sink_buffer)");
582         #endif
583         if(ret_state == 1) {
584                 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_SUCCESS)", ret_state);
585         }else if( ret_state == 2) {
586                 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_ASYNC)", ret_state);
587         }
588
589         debug_log("Sucess GST_STATE_CHANGE");
590
591         /* error */
592         if (ret_state == 0)     {
593                 debug_error("GST_STATE_CHANGE_FAILURE");
594                 return MM_ERROR_IMAGE_INVALID_VALUE;
595         }else {
596                 #if 0
597                         g_main_loop_run (pGstreamer_s->loop);
598                 #endif
599                 debug_log("Set GST_STATE_NULL");
600
601                 /*GST_STATE_NULL*/
602                 gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
603                 debug_log("End GST_STATE_NULL");
604
605                 debug_log("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
606
607                 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
608
609                 if(ret_state == 1) {
610                         debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
611                 }else if( ret_state == 2) {
612                         debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
613                 }
614
615                 debug_log("Success gst_element_get_state\n");
616
617                 if (ret_state == 0)     {
618                         debug_error("GST_STATE_CHANGE_FAILURE");
619                 }else {
620                         if(pGstreamer_s->output_buffer != NULL) {
621                                 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
622                                 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
623                                 int buffer_size = mapinfo.size;
624                                 debug_log("buffer size: %d\n", buffer_size);
625                                 if( buffer_size != mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation)) {
626                                         debug_log("Buffer size is different stride:%d elevation: %d\n", pImgp_info->output_stride, pImgp_info->output_elevation);
627                                 }
628                                 debug_log("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
629                                 memcpy( pImgp_info->dst, mapinfo.data, buffer_size);
630                                 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
631                         }else {
632                                 debug_log("pGstreamer_s->output_buffer is NULL");
633                         }
634                 }
635                 gst_object_unref (pGstreamer_s->pipeline);
636                 pGstreamer_s->output_buffer = NULL;
637                 g_free (pGstreamer_s);
638
639                 debug_log("End mm_convert_colorspace \n");
640         }
641         debug_log("pImgp_info->dst: %p", pImgp_info->dst);
642         return ret;
643 }
644
645
646 static int
647 mm_setup_image_size(const char* _format_label, int width, int height)
648 {
649         int size=0;
650
651         if(strcmp(_format_label, "I420") == 0) {
652                 setup_image_size_I420(width, height); /*width * height *1.5; */
653         }else if(strcmp(_format_label, "Y42B") == 0) {
654                 setup_image_size_Y42B(width, height); /*width * height *2; */
655         }else if(strcmp(_format_label, "YUV422") == 0) {
656                 setup_image_size_Y42B(width, height); /*width * height *2; */
657         }else if(strcmp(_format_label, "Y444") == 0) {
658                 setup_image_size_Y444(width, height); /* width * height *3; */
659         }else if(strcmp(_format_label, "YV12") == 0) {
660                 setup_image_size_YV12(width, height); /* width * height *1; */
661         }else if(strcmp(_format_label, "NV12") == 0) {
662                 setup_image_size_NV12(width, height); /* width * height *1.5; */
663         }else if(strcmp(_format_label, "RGB565") == 0) {
664                 setup_image_size_RGB565(width, height); /* width * height *2; */
665         }else if(strcmp(_format_label, "RGB888") == 0) {
666                 setup_image_size_RGB888(width, height); /* width * height *3; */
667         }else if(strcmp(_format_label, "BGR888") == 0) {
668                 setup_image_size_BGR888(width, height); /* width * height *3; */
669         }else if(strcmp(_format_label, "UYVY") == 0) {
670                 setup_image_size_UYVY(width, height); /* width * height *2; */
671         }else if(strcmp(_format_label, "YUYV") == 0) {
672                 setup_image_size_YUYV(width, height); /* width * height *2; */
673         }else if(strcmp(_format_label, "ARGB8888") == 0) {
674         size = width * height *4; debug_log("file_size: %d\n", size);
675         }else if(strcmp(_format_label, "BGRA8888") == 0) {
676                 size = width * height *4; debug_log("file_size: %d\n", size);
677         }else if(strcmp(_format_label, "RGBA8888") == 0) {
678                 size = width * height *4; debug_log("file_size: %d\n", size);
679         }else if(strcmp(_format_label, "ABGR8888") == 0) {
680                 size = width * height *4; debug_log("file_size: %d\n", size);
681         }else if(strcmp(_format_label, "BGRX") == 0) {
682                 size = width * height *4; debug_log("file_size: %d\n", size);
683         }
684
685         return size;
686 }
687
688 static int
689 _mm_imgp_gstcs(imgp_info_s* pImgp_info)
690 {
691         image_format_s* input_format=NULL, *output_format=NULL;
692         gstreamer_s* pGstreamer_s;
693         int ret = MM_ERROR_NONE;
694         static const int max_argc = 50;
695         gint* argc = NULL;
696         gchar** argv = NULL;
697
698         g_type_init();
699         if(pImgp_info == NULL) {
700                 debug_error("imgp_info_s is NULL");
701                 return MM_ERROR_IMAGE_INVALID_VALUE;
702         }
703
704         debug_log("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d dst: 0x%2x",
705                 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, pImgp_info->dst);
706
707         if(pImgp_info->dst == NULL) {
708                 debug_error("imgp_info_s->dst is NULL");
709         }
710         input_format= _mm_set_input_image_format_s_struct(pImgp_info);
711         output_format= _mm_set_output_image_format_s_struct(pImgp_info);
712
713         pImgp_info->output_stride = output_format->stride;
714         pImgp_info->output_elevation = output_format->elevation;
715
716         debug_log("mm_check_resize_format&&mm_check_rotate_format ");
717
718         if(__mm_check_resize_format(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)
719                 && __mm_check_rotate_format(pImgp_info->angle, pImgp_info->input_format_label, pImgp_info->output_format_label) ) {
720                 argc = malloc(sizeof(int));
721                 argv = malloc(sizeof(gchar*) * max_argc);
722
723                 if (!argc || !argv) {
724                         debug_error("argc ||argv is NULL");
725                         GSTCS_FREE(input_format);
726                         GSTCS_FREE(output_format);
727                         if (argc != NULL) {
728                                 free(argc);
729                         }
730                         if (argv != NULL) {
731                                 free(argv);
732                         }
733                         return MM_ERROR_IMAGE_INVALID_VALUE;
734                 }
735                 memset(argv, 0, sizeof(gchar*) * max_argc);
736                 debug_log("memset argv");
737
738                 /* add initial */
739                 *argc = 1;
740                 argv[0] = g_strdup( "mmutil_gstcs" );
741                 /* check disable registry scan */
742                 argv[*argc] = g_strdup("--gst-disable-registry-update");
743                 (*argc)++;
744                 debug_log("--gst-disable-registry-update");
745
746                 gst_init(argc, &argv);
747
748                 pGstreamer_s = g_new0 (gstreamer_s, 1);
749
750                 /* _format_label : I420, RGB888 etc*/
751                 debug_log("Start mm_convert_colorspace ");
752                 ret =_mm_imgp_gstcs_processing(pGstreamer_s, 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 */
753
754                 if(ret == MM_ERROR_NONE) {
755                         debug_log("End mm_convert_colorspace [pImgp_info->dst: %p]", pImgp_info->dst);
756                 }else if (ret != MM_ERROR_NONE) {
757                         debug_error("ERROR - mm_convert_colorspace");
758                 }
759         }else {
760                 debug_error("Error - Check your input / ouput image input_format_label: %s src_width: %d src_height: %d output_format_label: %s output_stride: %d output_elevation: %d angle: %d ",
761                 pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation, pImgp_info->angle);
762                 ret = MM_ERROR_IMAGE_INVALID_VALUE;
763         }
764
765         GSTCS_FREE(argv);
766         GSTCS_FREE(argc)
767
768         GSTCS_FREE(input_format->format_label);
769         GSTCS_FREE(input_format->colorspace);
770         GSTCS_FREE(input_format);
771
772         GSTCS_FREE(output_format->format_label);
773         GSTCS_FREE(output_format->colorspace);
774         GSTCS_FREE(output_format);
775
776         return ret;
777 }
778
779 int
780 mm_imgp(imgp_info_s* pImgp_info, imgp_type_e _imgp_type)
781 {
782         if (pImgp_info == NULL) {
783                 debug_error("input vaule is error");
784         }
785         return _mm_imgp_gstcs(pImgp_info);
786 }