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