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