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");
94 _mm_get_byte_per_pixcel(const char *__format_label)
96 int byte_per_pixcel = 1;
97 if (strcmp(__format_label, "RGB565") == 0) {
99 } else if (strcmp(__format_label, "RGB888") == 0 ||
100 strcmp(__format_label, "BGR888") == 0) {
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) {
113 gstcs_debug("byte per pixcel : %d", byte_per_pixcel);
115 return byte_per_pixcel;
118 static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
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;
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;
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;
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;
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;
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;
154 static void _mm_destroy_notify(gpointer data)
156 unsigned char *_data = (unsigned char *)data;
164 _mm_check_caps_format(GstCaps* caps)
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);
172 _mm_link_pipeline(gstreamer_s* pGstreamer_s, int value)
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");
179 gstcs_debug("Success to link pipeline");
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 */
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 */
187 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
188 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
191 static GstVideoFormat _mm_get_video_format(char *format_label)
193 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
202 if (format_label == NULL) {
203 gstcs_error("Wrong format label");
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;
247 gstcs_error("***Wrong format cs type***");
250 gstcs_debug("format_label [%s], Chosen video format [%s]", format_label, gst_video_format_to_string(videoFormat));
256 static void _mm_set_capabilities(image_format_s* __format)
258 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
260 if (__format == NULL) {
261 gstcs_error("Image format is NULL\n");
265 videoFormat = _mm_get_video_format(__format->format_label);
267 __format->caps = gst_caps_new_simple("video/x-raw",
268 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
269 "framerate", GST_TYPE_FRACTION, 25, 1,
270 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
271 "width", G_TYPE_INT, __format->stride,
272 "height", G_TYPE_INT, __format->elevation,
273 "framerate", GST_TYPE_FRACTION, 1, 1,
276 if (__format->caps) {
277 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
278 _mm_check_caps_format(__format->caps);
280 gstcs_error("__format->caps is NULL");
284 static void _mm_set_image_colorspace(image_format_s* __format)
286 gstcs_debug("format_label: %s\n", __format->format_label);
288 if ((strcmp(__format->format_label, "I420") == 0) || (strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
289 || (strcmp(__format->format_label, "YV12") == 0) || (strcmp(__format->format_label, "NV12") == 0) || (strcmp(__format->format_label, "UYVY") == 0) || (strcmp(__format->format_label, "YUYV") == 0)) {
290 __format->colorspace = g_strdup("YUV");
291 } else if ((strcmp(__format->format_label, "RGB888") == 0) || (strcmp(__format->format_label, "BGR888") == 0) || (strcmp(__format->format_label, "RGB565") == 0)) {
292 __format->colorspace = g_strdup("RGB");
293 } 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)) {
294 __format->colorspace = g_strdup("RGBA");
295 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
296 __format->colorspace = g_strdup("BGRX");
298 gstcs_error("Check your colorspace format label");
302 static int _gstcs_create_image_format(image_format_s **format)
304 int ret = GSTCS_ERROR_NONE;
305 image_format_s *_format = NULL;
307 if (format == NULL) {
308 gstcs_error("format is wrong value");
309 return GSTCS_ERROR_INVALID_OPERATION;
312 _format = (image_format_s*)malloc(sizeof(image_format_s));
313 if (_format == NULL) {
314 gstcs_error("memory allocation failed");
315 return GSTCS_ERROR_OUT_OF_MEMORY;
317 memset(_format, 0, sizeof(image_format_s));
323 static void _gstcs_destroy_image_format(image_format_s *format)
325 if (format != NULL) {
326 gst_caps_unref(format->caps);
327 GSTCS_FREE(format->format_label);
328 GSTCS_FREE(format->colorspace);
333 static void _mm_round_up_widh_height(image_format_s* pFormat)
335 if (strcmp(pFormat->colorspace, "YUV") == 0) {
336 pFormat->stride = pFormat->width;
337 pFormat->elevation = pFormat->height;
338 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
339 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
340 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
341 } else if ((strcmp(pFormat->colorspace, "RGBA") == 0) || (strcmp(pFormat->colorspace, "BGRX") == 0)) {
342 pFormat->stride = pFormat->width;
343 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
345 gstcs_debug("stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
348 static image_format_s* _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info)
350 int ret = GSTCS_ERROR_NONE;
351 image_format_s* __format = NULL;
353 ret = _gstcs_create_image_format(&__format);
354 if (ret != GSTCS_ERROR_NONE) {
355 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
359 __format->format_label = g_strdup(pImgp_info->input_format_label);
360 _mm_set_image_colorspace(__format);
362 __format->width = pImgp_info->src_width;
363 __format->height = pImgp_info->src_height;
364 _mm_round_up_widh_height(__format);
366 _mm_set_capabilities(__format);
368 gstcs_debug("input_format_label: %s", pImgp_info->input_format_label);
373 static image_format_s* _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
375 int ret = GSTCS_ERROR_NONE;
376 image_format_s* __format = NULL;
378 ret = _gstcs_create_image_format(&__format);
379 if (ret != GSTCS_ERROR_NONE) {
380 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
384 __format->format_label = g_strdup(pImgp_info->output_format_label);
385 _mm_set_image_colorspace(__format);
387 __format->width = pImgp_info->dst_width;
388 __format->height = pImgp_info->dst_height;
389 _mm_round_up_widh_height(__format);
391 _mm_set_capabilities(__format);
393 pImgp_info->output_stride = __format->stride;
394 pImgp_info->output_elevation = __format->elevation;
396 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
401 static int _mm_setup_image_size(const char* _format_label, int width, int height)
405 if (strcmp(_format_label, "I420") == 0)
406 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; */
407 else if (strcmp(_format_label, "Y42B") == 0)
408 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
409 else if (strcmp(_format_label, "YUV422") == 0)
410 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
411 else if (strcmp(_format_label, "Y444") == 0)
412 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
413 else if (strcmp(_format_label, "YV12") == 0)
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; */
415 else if (strcmp(_format_label, "NV12") == 0)
416 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
417 else if (strcmp(_format_label, "RGB565") == 0)
418 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
419 else if (strcmp(_format_label, "RGB888") == 0)
420 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
421 else if (strcmp(_format_label, "BGR888") == 0)
422 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
423 else if (strcmp(_format_label, "UYVY") == 0)
424 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
425 else if (strcmp(_format_label, "YUYV") == 0)
426 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
427 else if (strcmp(_format_label, "ARGB8888") == 0)
428 size = width * height *4;
429 else if (strcmp(_format_label, "BGRA8888") == 0)
430 size = width * height *4;
431 else if (strcmp(_format_label, "RGBA8888") == 0)
432 size = width * height *4;
433 else if (strcmp(_format_label, "ABGR8888") == 0)
434 size = width * height *4;
435 else if (strcmp(_format_label, "BGRX") == 0)
436 size = width * height *4;
438 gstcs_debug("file_size: %d\n", size);
444 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
446 int ret = GSTCS_ERROR_NONE;
448 if (pGstreamer_s->pipeline == NULL) {
449 gstcs_error("pipeline is NULL\n");
450 return GSTCS_ERROR_INVALID_PARAMETER;
453 gsize data_size = _mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
454 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
456 if (gst_buf == NULL) {
457 gstcs_error("buffer is NULL\n");
458 return GSTCS_ERROR_INVALID_PARAMETER;
461 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
466 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsigned char *src, gstreamer_s * pGstreamer_s)
468 int ret = GSTCS_ERROR_NONE;
469 GstBuffer *gst_buf = NULL;
470 unsigned int src_size = 0;
471 unsigned char *data = NULL;
472 unsigned int stride = input_format->stride;
473 unsigned int elevation = input_format->elevation;
475 if (pGstreamer_s->pipeline == NULL) {
476 gstcs_error("pipeline is NULL\n");
477 return GSTCS_ERROR_INVALID_PARAMETER;
480 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
481 src_size = _mm_setup_image_size(input_format->format_label, stride, elevation);
482 gstcs_debug("buffer size (src): %d", src_size);
484 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
485 unsigned int src_row = input_format->width * byte_per_pixcel;
486 unsigned int stride_row = stride * byte_per_pixcel;
487 unsigned int i = 0, y = 0;
488 gstcs_debug("padding will be inserted to buffer");
489 data = (unsigned char *) malloc(src_size);
491 gstcs_error("app_buffer is NULL\n");
492 return GSTCS_ERROR_INVALID_PARAMETER;
494 for (y = 0; y < (unsigned int)(input_format->height); y++) {
495 guint8 *pLine = (guint8 *) &(src[src_row * y]);
496 for (i = 0; i < src_row; i++)
497 data[y * stride_row + i] = pLine[i];
498 guint8 stride_row_color = pLine[i - 1];
499 for (i = src_row; i < stride_row; i++)
500 data[y * stride_row + i] = stride_row_color;
502 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
503 for (i = 0; i < stride_row; i++)
504 data[y * stride_row + i] = data[(y - 1) * stride_row + i];
506 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
508 if (gst_buf == NULL) {
509 gstcs_error("buffer is NULL\n");
511 return GSTCS_ERROR_INVALID_PARAMETER;
514 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
519 _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)
522 GstStateChangeReturn ret_state;
523 int ret = GSTCS_ERROR_NONE;
526 ret = _mm_create_pipeline(pGstreamer_s);
527 if (ret != GSTCS_ERROR_NONE)
528 gstcs_error("ERROR - mm_create_pipeline ");
530 /* 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. */
531 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
533 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
534 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
535 gst_object_unref(bus);
537 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
538 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
540 if ((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) {
541 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
542 ret = _mm_push_buffer_into_pipeline_new(input_format, src, pGstreamer_s);
544 gstcs_debug("Start mm_push_buffer_into_pipeline");
545 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
548 if (ret != GSTCS_ERROR_NONE) {
549 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
550 gst_object_unref(pGstreamer_s->pipeline);
553 gstcs_debug("End mm_push_buffer_into_pipeline");
556 gstcs_debug("Start mm_link_pipeline");
557 _mm_link_pipeline(pGstreamer_s, pImgp_info->angle);
558 gstcs_debug("End mm_link_pipeline");
560 /* Conecting to the new-sample signal emited by the appsink*/
561 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
562 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
563 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
565 /* GST_STATE_PLAYING*/
566 gstcs_debug("Start GST_STATE_PLAYING");
567 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
568 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
571 gstcs_debug("g_main_loop_run");
572 g_main_loop_run(pGstreamer_s->loop);
574 gstcs_debug("Sucess GST_STATE_CHANGE");
577 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
578 gstcs_debug("End GST_STATE_NULL");
580 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
582 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
584 if (ret_state == GST_STATE_CHANGE_SUCCESS)
585 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
586 else if (ret_state == GST_STATE_CHANGE_ASYNC)
587 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
589 gstcs_debug("Success gst_element_get_state\n");
591 if (ret_state == GST_STATE_CHANGE_FAILURE) {
592 gstcs_error("GST_STATE_CHANGE_FAILURE");
594 if (pGstreamer_s->output_buffer != NULL) {
595 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
596 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
597 int buffer_size = mapinfo.size;
598 int calc_buffer_size = 0;
600 calc_buffer_size = _mm_setup_image_size(pImgp_info->output_format_label, output_format->stride, output_format->elevation);
602 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
603 if (buffer_size != calc_buffer_size) {
604 gstcs_debug("Buffer size is different \n");
605 gstcs_debug("unref output buffer");
606 gst_buffer_unref(pGstreamer_s->output_buffer);
607 gst_object_unref(pGstreamer_s->pipeline);
608 pGstreamer_s->output_buffer = NULL;
609 return GSTCS_ERROR_INVALID_OPERATION;
611 gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);
612 *dst = calloc(1, buffer_size);
614 gstcs_error("ERROR - calloc ");
615 gst_buffer_unref(pGstreamer_s->output_buffer);
616 gst_object_unref(pGstreamer_s->pipeline);
617 pGstreamer_s->output_buffer = NULL;
618 return GSTCS_ERROR_INVALID_OPERATION;
621 memcpy(*dst, mapinfo.data, buffer_size);
622 pImgp_info->buffer_size = buffer_size;
623 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
625 gstcs_debug("pGstreamer_s->output_buffer is NULL");
628 gstcs_debug("unref output buffer");
629 gst_buffer_unref(pGstreamer_s->output_buffer);
630 gst_object_unref(pGstreamer_s->pipeline);
631 pGstreamer_s->output_buffer = NULL;
633 gstcs_debug("End gstreamer processing");
634 gstcs_debug("dst: %p", *dst);
638 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
640 if (gstreamer == NULL) {
641 gstcs_error("ERROR - gstreamer is null ");
642 return GSTCS_ERROR_INVALID_OPERATION;
645 gstreamer->context = g_main_context_new();
646 if (gstreamer->context == NULL) {
647 gstcs_error("ERROR - g_main_context_new ");
648 return GSTCS_ERROR_INVALID_OPERATION;
650 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
651 if (gstreamer->loop == NULL) {
652 gstcs_error("ERROR - g_main_loop_new ");
653 g_main_context_unref(gstreamer->context);
654 return GSTCS_ERROR_INVALID_OPERATION;
657 g_main_context_push_thread_default(gstreamer->context);
658 return GSTCS_ERROR_NONE;
661 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
663 if (gstreamer == NULL) {
664 gstcs_error("ERROR - gstreamer is null ");
665 return GSTCS_ERROR_INVALID_OPERATION;
668 if (gstreamer->loop != NULL)
669 g_main_loop_unref(gstreamer->loop);
671 if (gstreamer->context != NULL)
672 g_main_context_unref(gstreamer->context);
674 return GSTCS_ERROR_NONE;
677 static int _gstcs_init(gstreamer_s** gstreamer)
679 static const int max_argc = 50;
683 int ret = GSTCS_ERROR_NONE;
685 argv = malloc(sizeof(gchar*) * max_argc);
688 gstcs_error("argv is not allocated");
689 return GSTCS_ERROR_OUT_OF_MEMORY;
691 memset(argv, 0, sizeof(gchar*) * max_argc);
693 argv[argc] = g_strdup("mmutil_gstcs");
694 if (argv[argc] == NULL) {
695 gstcs_error("argv[%d] is not allocated", argc);
696 ret = GSTCS_ERROR_OUT_OF_MEMORY;
699 /* check disable registry scan */
700 argv[argc] = g_strdup("--gst-disable-registry-update");
701 if (argv[argc] == NULL) {
702 gstcs_error("argv[%d] is not allocated", argc);
703 ret = GSTCS_ERROR_OUT_OF_MEMORY;
706 if (ret != GSTCS_ERROR_NONE) {
707 for (i = 0; i < argc; i++)
714 gst_init(&argc, &argv);
716 *gstreamer = g_new0(gstreamer_s, 1);
717 if (*gstreamer == NULL) {
718 gstcs_error("gstreamer structure is not allocated");
719 ret = GSTCS_ERROR_OUT_OF_MEMORY;
722 for (i = 0; i < argc; i++)
729 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
731 image_format_s* input_format = NULL, *output_format = NULL;
732 gstreamer_s* pGstreamer_s;
733 int ret = GSTCS_ERROR_NONE;
735 /* Print debug message for inout structure */
736 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
737 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);
739 /* Initialize gstreamer */
740 ret = _gstcs_init(&pGstreamer_s);
741 if (ret != GSTCS_ERROR_NONE) {
742 gstcs_error("Error: _gstcs_new is failed");
746 /* Create input/output format for gstreamer processing */
747 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
748 if (input_format == NULL) {
749 gstcs_error("Error: memory allocation failed");
750 GSTCS_FREE(pGstreamer_s);
751 return GSTCS_ERROR_OUT_OF_MEMORY;
754 output_format = _mm_set_output_image_format_s_struct(pImgp_info);
755 if (output_format == NULL) {
756 gstcs_error("Error: memory allocation failed");
757 _gstcs_destroy_image_format(input_format);
758 GSTCS_FREE(pGstreamer_s);
759 return GSTCS_ERROR_OUT_OF_MEMORY;
762 /* Create default thread for async behavior */
763 ret = _gstcs_create_default_thread(pGstreamer_s);
764 if (ret != GSTCS_ERROR_NONE) {
765 gstcs_error("Error: _gstcs_create_default_thread is failed");
766 _gstcs_destroy_image_format(input_format);
767 _gstcs_destroy_image_format(output_format);
768 GSTCS_FREE(pGstreamer_s);
772 /* Do gstreamer processing */
773 gstcs_debug("Start _mm_imgp_gstcs_processing ");
774 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 */
776 if (ret == GSTCS_ERROR_NONE)
777 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst);
778 else if (ret != GSTCS_ERROR_NONE)
779 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
782 ret = _gstcs_destroy_default_thread(pGstreamer_s);
783 if (ret != GSTCS_ERROR_NONE)
784 gstcs_error("Error: _gstcs_create_default_thread is failed");
786 _gstcs_destroy_image_format(input_format);
787 _gstcs_destroy_image_format(output_format);
788 GSTCS_FREE(pGstreamer_s);
793 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
795 if (pImgp_info == NULL) {
796 gstcs_error("Error: input vaule is NULL");
797 return GSTCS_ERROR_INVALID_PARAMETER;
800 if (src == NULL || dst == NULL) {
801 gstcs_error("Error: src | dst is NULL");
802 return GSTCS_ERROR_INVALID_PARAMETER;
805 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
806 gstcs_error("Error: imgp_type is wrong");
807 return GSTCS_ERROR_INVALID_PARAMETER;
810 gstcs_debug("[src %p] [dst %p]", src, dst);
812 return _mm_imgp_gstcs(pImgp_info, src, dst);