4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "mm_util_gstcs_internal.h"
23 #include <gst/check/gstcheck.h>
24 #include <gst/video/video-format.h>
26 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
27 #define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
28 #define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)
29 #define MM_UTIL_ROUND_UP_16(num) (((num)+15)&~15)
31 #define SAFE_STRCPY(dst, src, n) g_strlcpy(dst, src, n)
35 _mm_sink_sample(GstElement * appsink, gpointer user_data)
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);
42 _buf = gst_sample_get_buffer(_sample);
44 pGstreamer_s->output_buffer = _buf;
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);
52 gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
56 gst_buffer_ref(pGstreamer_s->output_buffer); /* when you want to avoid flushing */
57 gst_sample_unref(_sample);
63 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
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);
74 case GST_MESSAGE_ERROR: {
76 gchar *dbg_info = NULL;
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");
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");
94 static int _mm_get_byte_per_pixcel(mm_util_color_format_e color_format)
96 int byte_per_pixcel = 1;
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:
107 case MM_UTIL_COLOR_RGB16:
110 case MM_UTIL_COLOR_RGB24:
113 case MM_UTIL_COLOR_ARGB:
114 case MM_UTIL_COLOR_BGRA:
115 case MM_UTIL_COLOR_RGBA:
116 case MM_UTIL_COLOR_BGRX:
120 gstcs_error("Not supported format");
124 gstcs_debug("color_format [%d] byte per pixcel [%d]", color_format, byte_per_pixcel);
126 return byte_per_pixcel;
129 static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
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;
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;
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;
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;
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;
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;
165 static void _mm_destroy_notify(gpointer data)
167 unsigned char *_data = (unsigned char *)data;
175 _mm_check_caps_format(GstCaps* caps)
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);
183 _mm_link_pipeline(gstreamer_s* pGstreamer_s, int value)
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");
192 gstcs_debug("Success to link pipeline");
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 */
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 */
200 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
201 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
206 static GstVideoFormat _mm_get_video_format(mm_util_color_format_e color_format)
208 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
217 switch (color_format) {
218 case MM_UTIL_COLOR_YUV420:
219 videoFormat = GST_VIDEO_FORMAT_YV12;
221 case MM_UTIL_COLOR_YUV422:
222 videoFormat = GST_VIDEO_FORMAT_Y42B;
224 case MM_UTIL_COLOR_I420:
225 videoFormat = GST_VIDEO_FORMAT_I420;
227 case MM_UTIL_COLOR_NV12:
228 videoFormat = GST_VIDEO_FORMAT_NV12;
230 case MM_UTIL_COLOR_UYVY:
231 videoFormat = GST_VIDEO_FORMAT_UYVY;
233 case MM_UTIL_COLOR_YUYV:
234 videoFormat = GST_VIDEO_FORMAT_YVYU;
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);
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);
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);
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);
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);
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);
261 gstcs_error("Not supported format");
265 gstcs_debug("color_format [%d], Chosen video format [%s]", color_format, gst_video_format_to_string(videoFormat));
271 static void _mm_set_capabilities(image_format_s* __format)
273 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
275 if (__format == NULL) {
276 gstcs_error("Image format is NULL\n");
280 videoFormat = _mm_get_video_format(__format->color_format);
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,
291 if (__format->caps) {
292 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
293 _mm_check_caps_format(__format->caps);
295 gstcs_error("__format->caps is NULL");
299 static int _gstcs_create_image_format(image_format_s **format)
301 int ret = GSTCS_ERROR_NONE;
302 image_format_s *_format = NULL;
304 if (format == NULL) {
305 gstcs_error("format is wrong value");
306 return GSTCS_ERROR_INVALID_OPERATION;
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;
314 memset(_format, 0, sizeof(image_format_s));
320 static void _gstcs_destroy_image_format(image_format_s *format)
322 if (format != NULL) {
323 gst_caps_unref(format->caps);
328 static void _mm_round_up_widh_height(image_format_s* pFormat)
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;
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);
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);
353 gstcs_error("Not supported format");
357 gstcs_debug("color_format [%d] stride [%d], elevation [%d]", pFormat->color_format, pFormat->stride, pFormat->elevation);
360 static image_format_s* _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info)
362 int ret = GSTCS_ERROR_NONE;
363 image_format_s* __format = NULL;
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);
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);
376 _mm_set_capabilities(__format);
381 static image_format_s* _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
383 int ret = GSTCS_ERROR_NONE;
384 image_format_s* __format = NULL;
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);
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);
397 _mm_set_capabilities(__format);
399 pImgp_info->output_stride = __format->stride;
400 pImgp_info->output_elevation = __format->elevation;
405 static int _mm_setup_image_size(mm_util_color_format_e color_format, int width, int height)
409 gstcs_debug("color_format [%d] width [%d] height [%d]", color_format, width, height);
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; */
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; */
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; */
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; */
424 case MM_UTIL_COLOR_UYVY:
425 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
427 case MM_UTIL_COLOR_YUYV:
428 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
430 case MM_UTIL_COLOR_RGB16:
431 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
433 case MM_UTIL_COLOR_RGB24:
434 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
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;
443 gstcs_error("Not supported format");
448 gstcs_debug("Image size: %d", size);
453 static int _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
455 int ret = GSTCS_ERROR_NONE;
457 GstBuffer* gst_buf = NULL;
461 gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");
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);
466 gstcs_retvm_if(gst_buf == NULL, GSTCS_ERROR_INVALID_OPERATION, "buffer is NULL");
468 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
475 static int _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsigned char *src, gstreamer_s * pGstreamer_s)
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;
486 gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline");
488 src_size = _mm_setup_image_size(input_format->color_format, stride, elevation);
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;
495 data = (unsigned char *) malloc(src_size);
496 gstcs_retvm_if(data == NULL, GSTCS_ERROR_OUT_OF_MEMORY, "data is NULL");
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;
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];
510 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
512 if (gst_buf == NULL) {
513 gstcs_error("buffer is NULL\n");
515 return GSTCS_ERROR_INVALID_PARAMETER;
518 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
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)
528 GstStateChangeReturn ret_state;
529 int ret = GSTCS_ERROR_NONE;
534 ret = _mm_create_pipeline(pGstreamer_s);
535 if (ret != GSTCS_ERROR_NONE)
536 gstcs_error("ERROR - mm_create_pipeline ");
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);
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);
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);
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);
551 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
554 if (ret != GSTCS_ERROR_NONE) {
555 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
556 gst_object_unref(pGstreamer_s->pipeline);
561 _mm_link_pipeline(pGstreamer_s, pImgp_info->angle);
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)");
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);
574 gstcs_debug("g_main_loop_run");
575 g_main_loop_run(pGstreamer_s->loop);
577 gstcs_debug("Sucess GST_STATE_CHANGE");
580 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
581 gstcs_debug("End GST_STATE_NULL");
583 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
585 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
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);
592 gstcs_debug("Success gst_element_get_state\n");
594 if (ret_state == GST_STATE_CHANGE_FAILURE) {
595 gstcs_error("GST_STATE_CHANGE_FAILURE");
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;
603 calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, output_format->stride, output_format->elevation);
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;
614 gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);
615 *dst = calloc(1, buffer_size);
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;
624 memcpy(*dst, mapinfo.data, buffer_size);
625 pImgp_info->buffer_size = buffer_size;
626 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
628 gstcs_debug("pGstreamer_s->output_buffer is NULL");
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;
636 gstcs_debug("dst: %p", *dst);
642 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
644 gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");
646 gstreamer->context = g_main_context_new();
647 gstcs_retvm_if(gstreamer->context == NULL, GSTCS_ERROR_INVALID_OPERATION, "ERROR - g_main_context_new");
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;
656 g_main_context_push_thread_default(gstreamer->context);
658 return GSTCS_ERROR_NONE;
661 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
663 gstcs_retvm_if(gstreamer == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid gstreamer");
665 if (gstreamer->loop != NULL)
666 g_main_loop_unref(gstreamer->loop);
668 if (gstreamer->context != NULL)
669 g_main_context_unref(gstreamer->context);
671 return GSTCS_ERROR_NONE;
674 static int _gstcs_init(gstreamer_s** gstreamer)
676 static const int max_argc = 50;
680 int ret = GSTCS_ERROR_NONE;
682 argv = malloc(sizeof(gchar*) * max_argc);
684 gstcs_retvm_if(argv == NULL, GSTCS_ERROR_OUT_OF_MEMORY, "argv is not allocated");
686 memset(argv, 0, sizeof(gchar*) * max_argc);
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;
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;
701 if (ret != GSTCS_ERROR_NONE) {
702 for (i = 0; i < argc; i++)
709 gst_init(&argc, &argv);
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;
717 for (i = 0; i < argc; i++)
724 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
726 image_format_s* input_format = NULL, *output_format = NULL;
727 gstreamer_s* pGstreamer_s;
728 int ret = GSTCS_ERROR_NONE;
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);
734 /* Initialize gstreamer */
735 ret = _gstcs_init(&pGstreamer_s);
736 if (ret != GSTCS_ERROR_NONE) {
737 gstcs_error("Error: _gstcs_new is failed");
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;
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;
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);
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 */
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");
776 ret = _gstcs_destroy_default_thread(pGstreamer_s);
777 if (ret != GSTCS_ERROR_NONE)
778 gstcs_error("Error: _gstcs_create_default_thread is failed");
780 _gstcs_destroy_image_format(input_format);
781 _gstcs_destroy_image_format(output_format);
782 GSTCS_FREE(pGstreamer_s);
787 static bool _mm_imgp_check_format(mm_util_color_format_e color_format)
789 if ((color_format >= MM_UTIL_COLOR_YUV420) && (color_format <= MM_UTIL_COLOR_BGRX))
795 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
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);
804 gstcs_debug("[src %p] [dst %p]", src, dst);
806 return _mm_imgp_gstcs(pImgp_info, src, dst);