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.
21 #include "mm_util_gstcs_internal.h"
22 #include <gst/check/gstcheck.h>
23 #include <gst/video/video-format.h>
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)
30 #define SAFE_STRCPY(dst, src, n) g_strlcpy(dst, src, n)
34 _mm_sink_sample(GstElement * appsink, gpointer user_data)
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);
41 _buf = gst_sample_get_buffer(_sample);
43 pGstreamer_s->output_buffer = _buf;
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);
51 gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
55 gst_buffer_ref(pGstreamer_s->output_buffer); /* when you want to avoid flushing */
56 gst_sample_unref(_sample);
62 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
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);
73 case GST_MESSAGE_ERROR: {
75 gchar *dbg_info = NULL;
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");
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");
93 static int _mm_get_byte_per_pixcel(mm_util_color_format_e color_format)
95 int byte_per_pixcel = 1;
97 switch (color_format) {
98 case MM_UTIL_COLOR_YUV420:
99 case MM_UTIL_COLOR_YUV422:
100 case MM_UTIL_COLOR_I420:
101 case MM_UTIL_COLOR_NV12:
102 case MM_UTIL_COLOR_UYVY:
103 case MM_UTIL_COLOR_YUYV:
106 case MM_UTIL_COLOR_RGB16:
109 case MM_UTIL_COLOR_RGB24:
112 case MM_UTIL_COLOR_ARGB:
113 case MM_UTIL_COLOR_BGRA:
114 case MM_UTIL_COLOR_RGBA:
115 case MM_UTIL_COLOR_BGRX:
119 gstcs_error("Not supported format");
123 gstcs_debug("color_format [%d] byte per pixcel [%d]", color_format, byte_per_pixcel);
125 return byte_per_pixcel;
128 static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
130 int ret = GSTCS_ERROR_NONE;
131 pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
132 if (!pGstreamer_s->pipeline) {
133 gstcs_error("pipeline could not be created. Exiting.\n");
134 ret = GSTCS_ERROR_INVALID_PARAMETER;
136 pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
137 if (!pGstreamer_s->appsrc) {
138 gstcs_error("appsrc could not be created. Exiting.\n");
139 ret = GSTCS_ERROR_INVALID_PARAMETER;
141 pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
142 if (!pGstreamer_s->colorspace) {
143 gstcs_error("colorspace could not be created. Exiting.\n");
144 ret = GSTCS_ERROR_INVALID_PARAMETER;
146 pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
147 if (!pGstreamer_s->videoscale) {
148 gstcs_error("videoscale could not be created. Exiting.\n");
149 ret = GSTCS_ERROR_INVALID_PARAMETER;
151 pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
152 if (!pGstreamer_s->videoflip) {
153 gstcs_error("videoflip could not be created. Exiting.\n");
154 ret = GSTCS_ERROR_INVALID_PARAMETER;
156 pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
157 if (!pGstreamer_s->appsink) {
158 gstcs_error("appsink could not be created. Exiting.\n");
159 ret = GSTCS_ERROR_INVALID_PARAMETER;
164 static void _mm_destroy_notify(gpointer data)
166 unsigned char *_data = (unsigned char *)data;
174 _mm_check_caps_format(GstCaps* caps)
176 GstStructure *caps_structure = gst_caps_get_structure(caps, 0);
177 const gchar* formatInfo = gst_structure_get_string(caps_structure, "format");
178 gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
182 _mm_link_pipeline(gstreamer_s* pGstreamer_s, int value)
185 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL);
186 if (!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL))
187 gstcs_error("Fail to link pipeline");
189 gstcs_debug("Success to link pipeline");
191 g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
192 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
193 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
195 g_object_set(pGstreamer_s->videoflip, "method", value, NULL); /* GST_VIDEO_FLIP_METHOD_IDENTITY (0): none- Identity (no rotation) (1): clockwise - Rotate clockwise 90 degrees (2): rotate-180 - Rotate 180 degrees (3): counterclockwise - Rotate counter-clockwise 90 degrees (4): horizontal-flip - Flip horizontally (5): vertical-flip - Flip vertically (6): upper-left-diagonal - Flip across upper left/lower right diagonal (7): upper-right-diagonal - Flip across upper right/lower left diagonal */
197 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
198 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
201 static GstVideoFormat _mm_get_video_format(mm_util_color_format_e color_format)
203 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
212 switch (color_format) {
213 case MM_UTIL_COLOR_YUV420:
214 videoFormat = GST_VIDEO_FORMAT_YV12;
216 case MM_UTIL_COLOR_YUV422:
217 videoFormat = GST_VIDEO_FORMAT_Y42B;
219 case MM_UTIL_COLOR_I420:
220 videoFormat = GST_VIDEO_FORMAT_I420;
222 case MM_UTIL_COLOR_NV12:
223 videoFormat = GST_VIDEO_FORMAT_NV12;
225 case MM_UTIL_COLOR_UYVY:
226 videoFormat = GST_VIDEO_FORMAT_UYVY;
228 case MM_UTIL_COLOR_YUYV:
229 videoFormat = GST_VIDEO_FORMAT_YVYU;
231 case MM_UTIL_COLOR_RGB16:
232 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234; _alpha_mask = 0;
233 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
235 case MM_UTIL_COLOR_RGB24:
236 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321; _alpha_mask = 0;
237 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
239 case MM_UTIL_COLOR_ARGB:
240 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
241 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
243 case MM_UTIL_COLOR_BGRA:
244 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
245 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
247 case MM_UTIL_COLOR_RGBA:
248 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
249 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
251 case MM_UTIL_COLOR_BGRX:
252 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321; _alpha_mask = 0;
253 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
256 gstcs_error("Not supported format");
260 gstcs_debug("color_format [%d], Chosen video format [%s]", color_format, gst_video_format_to_string(videoFormat));
266 static void _mm_set_capabilities(image_format_s* __format)
268 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
270 if (__format == NULL) {
271 gstcs_error("Image format is NULL\n");
275 videoFormat = _mm_get_video_format(__format->color_format);
277 __format->caps = gst_caps_new_simple("video/x-raw",
278 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
279 "framerate", GST_TYPE_FRACTION, 25, 1,
280 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
281 "width", G_TYPE_INT, __format->stride,
282 "height", G_TYPE_INT, __format->elevation,
283 "framerate", GST_TYPE_FRACTION, 1, 1,
286 if (__format->caps) {
287 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
288 _mm_check_caps_format(__format->caps);
290 gstcs_error("__format->caps is NULL");
294 static int _gstcs_create_image_format(image_format_s **format)
296 int ret = GSTCS_ERROR_NONE;
297 image_format_s *_format = NULL;
299 if (format == NULL) {
300 gstcs_error("format is wrong value");
301 return GSTCS_ERROR_INVALID_OPERATION;
304 _format = (image_format_s*)malloc(sizeof(image_format_s));
305 if (_format == NULL) {
306 gstcs_error("memory allocation failed");
307 return GSTCS_ERROR_OUT_OF_MEMORY;
309 memset(_format, 0, sizeof(image_format_s));
315 static void _gstcs_destroy_image_format(image_format_s *format)
317 if (format != NULL) {
318 gst_caps_unref(format->caps);
323 static void _mm_round_up_widh_height(image_format_s* pFormat)
325 switch (pFormat->color_format) {
326 case MM_UTIL_COLOR_YUV420:
327 case MM_UTIL_COLOR_YUV422:
328 case MM_UTIL_COLOR_I420:
329 case MM_UTIL_COLOR_NV12:
330 case MM_UTIL_COLOR_UYVY:
331 case MM_UTIL_COLOR_YUYV:
332 pFormat->stride = pFormat->width;
333 pFormat->elevation = pFormat->height;
335 case MM_UTIL_COLOR_RGB16:
336 case MM_UTIL_COLOR_RGB24:
337 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
338 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
340 case MM_UTIL_COLOR_ARGB:
341 case MM_UTIL_COLOR_BGRA:
342 case MM_UTIL_COLOR_RGBA:
343 case MM_UTIL_COLOR_BGRX:
344 pFormat->stride = pFormat->width;
345 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
348 gstcs_error("Not supported format");
352 gstcs_debug("color_format [%d] stride [%d], elevation [%d]", pFormat->color_format, pFormat->stride, pFormat->elevation);
355 static image_format_s* _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info)
357 int ret = GSTCS_ERROR_NONE;
358 image_format_s* __format = NULL;
360 ret = _gstcs_create_image_format(&__format);
361 if (ret != GSTCS_ERROR_NONE) {
362 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
366 __format->color_format = pImgp_info->src_format;
367 __format->width = pImgp_info->src_width;
368 __format->height = pImgp_info->src_height;
369 _mm_round_up_widh_height(__format);
371 _mm_set_capabilities(__format);
376 static image_format_s* _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
378 int ret = GSTCS_ERROR_NONE;
379 image_format_s* __format = NULL;
381 ret = _gstcs_create_image_format(&__format);
382 if (ret != GSTCS_ERROR_NONE) {
383 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
387 __format->color_format = pImgp_info->dst_format;
388 __format->width = pImgp_info->dst_width;
389 __format->height = pImgp_info->dst_height;
390 _mm_round_up_widh_height(__format);
392 _mm_set_capabilities(__format);
394 pImgp_info->output_stride = __format->stride;
395 pImgp_info->output_elevation = __format->elevation;
400 static int _mm_setup_image_size(mm_util_color_format_e color_format, int width, int height)
404 gstcs_debug("color_format: %d", color_format);
406 switch (color_format) {
407 case MM_UTIL_COLOR_YUV420:
408 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) + MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) / 2); /* width * height *1; */
410 case MM_UTIL_COLOR_YUV422:
411 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
413 case MM_UTIL_COLOR_I420:
414 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) + MM_UTIL_ROUND_UP_8(width) * MM_UTIL_ROUND_UP_2(height) /2); /*width * height *1.5; */
416 case MM_UTIL_COLOR_NV12:
417 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
419 case MM_UTIL_COLOR_UYVY:
420 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
422 case MM_UTIL_COLOR_YUYV:
423 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
425 case MM_UTIL_COLOR_RGB16:
426 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
428 case MM_UTIL_COLOR_RGB24:
429 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
431 case MM_UTIL_COLOR_ARGB:
432 case MM_UTIL_COLOR_BGRA:
433 case MM_UTIL_COLOR_RGBA:
434 case MM_UTIL_COLOR_BGRX:
435 size = width * height *4;
438 gstcs_error("Not supported format");
443 gstcs_debug("Image size: %d", size);
449 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
451 int ret = GSTCS_ERROR_NONE;
453 if (pGstreamer_s->pipeline == NULL) {
454 gstcs_error("pipeline is NULL\n");
455 return GSTCS_ERROR_INVALID_PARAMETER;
458 gsize data_size = _mm_setup_image_size(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height);
459 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
461 if (gst_buf == NULL) {
462 gstcs_error("buffer is NULL\n");
463 return GSTCS_ERROR_INVALID_PARAMETER;
466 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
471 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsigned char *src, gstreamer_s * pGstreamer_s)
473 int ret = GSTCS_ERROR_NONE;
474 GstBuffer *gst_buf = NULL;
475 unsigned int src_size = 0;
476 unsigned char *data = NULL;
477 unsigned int stride = input_format->stride;
478 unsigned int elevation = input_format->elevation;
480 if (pGstreamer_s->pipeline == NULL) {
481 gstcs_error("pipeline is NULL\n");
482 return GSTCS_ERROR_INVALID_PARAMETER;
485 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
486 src_size = _mm_setup_image_size(input_format->color_format, stride, elevation);
487 gstcs_debug("buffer size (src): %d", src_size);
489 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->color_format);
490 unsigned int src_row = input_format->width * byte_per_pixcel;
491 unsigned int stride_row = stride * byte_per_pixcel;
492 unsigned int i = 0, y = 0;
493 gstcs_debug("padding will be inserted to buffer");
494 data = (unsigned char *) malloc(src_size);
496 gstcs_error("app_buffer is NULL\n");
497 return GSTCS_ERROR_INVALID_PARAMETER;
499 for (y = 0; y < (unsigned int)(input_format->height); y++) {
500 guint8 *pLine = (guint8 *) &(src[src_row * y]);
501 for (i = 0; i < src_row; i++)
502 data[y * stride_row + i] = pLine[i];
503 guint8 stride_row_color = pLine[i - 1];
504 for (i = src_row; i < stride_row; i++)
505 data[y * stride_row + i] = stride_row_color;
507 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
508 for (i = 0; i < stride_row; i++)
509 data[y * stride_row + i] = data[(y - 1) * stride_row + i];
511 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
513 if (gst_buf == NULL) {
514 gstcs_error("buffer is NULL\n");
516 return GSTCS_ERROR_INVALID_PARAMETER;
519 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
524 _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char **dst, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
527 GstStateChangeReturn ret_state;
528 int ret = GSTCS_ERROR_NONE;
531 ret = _mm_create_pipeline(pGstreamer_s);
532 if (ret != GSTCS_ERROR_NONE)
533 gstcs_error("ERROR - mm_create_pipeline ");
535 /* Make appsink emit the "new-preroll" and "new-sample" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
536 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
538 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
539 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
540 gst_object_unref(bus);
542 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
543 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
545 if ((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) {
546 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
547 ret = _mm_push_buffer_into_pipeline_new(input_format, src, pGstreamer_s);
549 gstcs_debug("Start mm_push_buffer_into_pipeline");
550 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
553 if (ret != GSTCS_ERROR_NONE) {
554 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
555 gst_object_unref(pGstreamer_s->pipeline);
558 gstcs_debug("End mm_push_buffer_into_pipeline");
561 gstcs_debug("Start mm_link_pipeline");
562 _mm_link_pipeline(pGstreamer_s, pImgp_info->angle);
563 gstcs_debug("End mm_link_pipeline");
565 /* Conecting to the new-sample signal emited by the appsink*/
566 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
567 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
568 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
570 /* GST_STATE_PLAYING*/
571 gstcs_debug("Start GST_STATE_PLAYING");
572 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
573 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
576 gstcs_debug("g_main_loop_run");
577 g_main_loop_run(pGstreamer_s->loop);
579 gstcs_debug("Sucess GST_STATE_CHANGE");
582 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
583 gstcs_debug("End GST_STATE_NULL");
585 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
587 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
589 if (ret_state == GST_STATE_CHANGE_SUCCESS)
590 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
591 else if (ret_state == GST_STATE_CHANGE_ASYNC)
592 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
594 gstcs_debug("Success gst_element_get_state\n");
596 if (ret_state == GST_STATE_CHANGE_FAILURE) {
597 gstcs_error("GST_STATE_CHANGE_FAILURE");
599 if (pGstreamer_s->output_buffer != NULL) {
600 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
601 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
602 int buffer_size = mapinfo.size;
603 int calc_buffer_size = 0;
605 calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, output_format->stride, output_format->elevation);
607 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
608 if (buffer_size != calc_buffer_size) {
609 gstcs_debug("Buffer size is different \n");
610 gstcs_debug("unref output buffer");
611 gst_buffer_unref(pGstreamer_s->output_buffer);
612 gst_object_unref(pGstreamer_s->pipeline);
613 pGstreamer_s->output_buffer = NULL;
614 return GSTCS_ERROR_INVALID_OPERATION;
616 gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);
617 *dst = calloc(1, buffer_size);
619 gstcs_error("ERROR - calloc ");
620 gst_buffer_unref(pGstreamer_s->output_buffer);
621 gst_object_unref(pGstreamer_s->pipeline);
622 pGstreamer_s->output_buffer = NULL;
623 return GSTCS_ERROR_INVALID_OPERATION;
626 memcpy(*dst, mapinfo.data, buffer_size);
627 pImgp_info->buffer_size = buffer_size;
628 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
630 gstcs_debug("pGstreamer_s->output_buffer is NULL");
633 gstcs_debug("unref output buffer");
634 gst_buffer_unref(pGstreamer_s->output_buffer);
635 gst_object_unref(pGstreamer_s->pipeline);
636 pGstreamer_s->output_buffer = NULL;
638 gstcs_debug("End gstreamer processing");
639 gstcs_debug("dst: %p", *dst);
643 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
645 if (gstreamer == NULL) {
646 gstcs_error("ERROR - gstreamer is null ");
647 return GSTCS_ERROR_INVALID_OPERATION;
650 gstreamer->context = g_main_context_new();
651 if (gstreamer->context == NULL) {
652 gstcs_error("ERROR - g_main_context_new ");
653 return GSTCS_ERROR_INVALID_OPERATION;
655 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
656 if (gstreamer->loop == NULL) {
657 gstcs_error("ERROR - g_main_loop_new ");
658 g_main_context_unref(gstreamer->context);
659 return GSTCS_ERROR_INVALID_OPERATION;
662 g_main_context_push_thread_default(gstreamer->context);
663 return GSTCS_ERROR_NONE;
666 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
668 if (gstreamer == NULL) {
669 gstcs_error("ERROR - gstreamer is null ");
670 return GSTCS_ERROR_INVALID_OPERATION;
673 if (gstreamer->loop != NULL)
674 g_main_loop_unref(gstreamer->loop);
676 if (gstreamer->context != NULL)
677 g_main_context_unref(gstreamer->context);
679 return GSTCS_ERROR_NONE;
682 static int _gstcs_init(gstreamer_s** gstreamer)
684 static const int max_argc = 50;
688 int ret = GSTCS_ERROR_NONE;
690 argv = malloc(sizeof(gchar*) * max_argc);
693 gstcs_error("argv is not allocated");
694 return GSTCS_ERROR_OUT_OF_MEMORY;
696 memset(argv, 0, sizeof(gchar*) * max_argc);
698 argv[argc] = g_strdup("mmutil_gstcs");
699 if (argv[argc] == NULL) {
700 gstcs_error("argv[%d] is not allocated", argc);
701 ret = GSTCS_ERROR_OUT_OF_MEMORY;
704 /* check disable registry scan */
705 argv[argc] = g_strdup("--gst-disable-registry-update");
706 if (argv[argc] == NULL) {
707 gstcs_error("argv[%d] is not allocated", argc);
708 ret = GSTCS_ERROR_OUT_OF_MEMORY;
711 if (ret != GSTCS_ERROR_NONE) {
712 for (i = 0; i < argc; i++)
719 gst_init(&argc, &argv);
721 *gstreamer = g_new0(gstreamer_s, 1);
722 if (*gstreamer == NULL) {
723 gstcs_error("gstreamer structure is not allocated");
724 ret = GSTCS_ERROR_OUT_OF_MEMORY;
727 for (i = 0; i < argc; i++)
734 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
736 image_format_s* input_format = NULL, *output_format = NULL;
737 gstreamer_s* pGstreamer_s;
738 int ret = GSTCS_ERROR_NONE;
740 /* Print debug message for inout structure */
741 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
742 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);
744 /* Initialize gstreamer */
745 ret = _gstcs_init(&pGstreamer_s);
746 if (ret != GSTCS_ERROR_NONE) {
747 gstcs_error("Error: _gstcs_new is failed");
751 /* Create input/output format for gstreamer processing */
752 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
753 if (input_format == NULL) {
754 gstcs_error("Error: memory allocation failed");
755 GSTCS_FREE(pGstreamer_s);
756 return GSTCS_ERROR_OUT_OF_MEMORY;
759 output_format = _mm_set_output_image_format_s_struct(pImgp_info);
760 if (output_format == NULL) {
761 gstcs_error("Error: memory allocation failed");
762 _gstcs_destroy_image_format(input_format);
763 GSTCS_FREE(pGstreamer_s);
764 return GSTCS_ERROR_OUT_OF_MEMORY;
767 /* Create default thread for async behavior */
768 ret = _gstcs_create_default_thread(pGstreamer_s);
769 if (ret != GSTCS_ERROR_NONE) {
770 gstcs_error("Error: _gstcs_create_default_thread is failed");
771 _gstcs_destroy_image_format(input_format);
772 _gstcs_destroy_image_format(output_format);
773 GSTCS_FREE(pGstreamer_s);
777 /* Do gstreamer processing */
778 gstcs_debug("Start _mm_imgp_gstcs_processing ");
779 ret = _mm_imgp_gstcs_processing(pGstreamer_s, src, dst, input_format, output_format, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */
781 if (ret == GSTCS_ERROR_NONE)
782 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst);
783 else if (ret != GSTCS_ERROR_NONE)
784 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
787 ret = _gstcs_destroy_default_thread(pGstreamer_s);
788 if (ret != GSTCS_ERROR_NONE)
789 gstcs_error("Error: _gstcs_create_default_thread is failed");
791 _gstcs_destroy_image_format(input_format);
792 _gstcs_destroy_image_format(output_format);
793 GSTCS_FREE(pGstreamer_s);
798 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
800 if (pImgp_info == NULL) {
801 gstcs_error("Error: input vaule is NULL");
802 return GSTCS_ERROR_INVALID_PARAMETER;
805 if (src == NULL || dst == NULL) {
806 gstcs_error("Error: src | dst is NULL");
807 return GSTCS_ERROR_INVALID_PARAMETER;
810 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
811 gstcs_error("Error: imgp_type is wrong");
812 return GSTCS_ERROR_INVALID_PARAMETER;
815 gstcs_debug("[src %p] [dst %p]", src, dst);
817 return _mm_imgp_gstcs(pImgp_info, src, dst);