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