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));
257 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
259 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
261 if (__format == NULL) {
262 gstcs_error("Image format is NULL\n");
266 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
268 videoFormat = _mm_get_video_format(__format->format_label);
270 if (strcmp(__format->colorspace, "YUV") == 0) {
271 __format->caps = gst_caps_new_simple("video/x-raw",
272 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
273 "framerate", GST_TYPE_FRACTION, 25, 1,
274 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
275 "width", G_TYPE_INT, __format->width,
276 "height", G_TYPE_INT, __format->height,
277 "framerate", GST_TYPE_FRACTION, 1, 1,
281 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
282 __format->caps = gst_caps_new_simple("video/x-raw",
283 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
284 "framerate", GST_TYPE_FRACTION, 25, 1,
285 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
286 "width", G_TYPE_INT, __format->stride,
287 "height", G_TYPE_INT, __format->elevation,
288 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
291 else if (strcmp(__format->colorspace, "RGBA") == 0) {
292 __format->caps = gst_caps_new_simple("video/x-raw",
293 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
294 "framerate", GST_TYPE_FRACTION, 25, 1,
295 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
296 "width", G_TYPE_INT, __format->width,
297 "height", G_TYPE_INT, __format->elevation,
298 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
301 if (__format->caps) {
302 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
303 _mm_check_caps_format(__format->caps);
305 gstcs_error("__format->caps is NULL");
310 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
312 GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
314 if (__format == NULL) {
315 gstcs_error("Image format is NULL\n");
319 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
321 videoFormat = _mm_get_video_format(__format->format_label);
323 if (strcmp(__format->colorspace, "YUV") == 0) {
324 __format->caps = gst_caps_new_simple("video/x-raw",
325 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
326 "framerate", GST_TYPE_FRACTION, 25, 1,
327 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
328 "width", G_TYPE_INT, __format->width,
329 "height", G_TYPE_INT, __format->height,
330 "framerate", GST_TYPE_FRACTION, 1, 1,
334 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
335 __format->caps = gst_caps_new_simple("video/x-raw",
336 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
337 "framerate", GST_TYPE_FRACTION, 25, 1,
338 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
339 "width", G_TYPE_INT, __format->width,
340 "height", G_TYPE_INT, __format->height,
341 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
344 else if (strcmp(__format->colorspace, "RGBA") == 0) {
345 __format->caps = gst_caps_new_simple("video/x-raw",
346 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
347 "framerate", GST_TYPE_FRACTION, 25, 1,
348 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
349 "width", G_TYPE_INT, __format->width,
350 "height", G_TYPE_INT, __format->height,
351 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
354 if (__format->caps) {
355 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
356 _mm_check_caps_format(__format->caps);
358 gstcs_error("__format->caps is NULL");
363 _mm_set_image_colorspace(image_format_s* __format)
365 gstcs_debug("format_label: %s\n", __format->format_label);
367 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
368 if (__format->colorspace == NULL) {
369 gstcs_error("memory allocation failed");
372 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
373 if ((strcmp(__format->format_label, "I420") == 0) || (strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
374 || (strcmp(__format->format_label, "YV12") == 0) || (strcmp(__format->format_label, "NV12") == 0) || (strcmp(__format->format_label, "UYVY") == 0) || (strcmp(__format->format_label, "YUYV") == 0)) {
375 SAFE_STRCPY(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
376 } else if ((strcmp(__format->format_label, "RGB888") == 0) || (strcmp(__format->format_label, "BGR888") == 0) || (strcmp(__format->format_label, "RGB565") == 0)) {
377 SAFE_STRCPY(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
378 } else if ((strcmp(__format->format_label, "ARGB8888") == 0) || (strcmp(__format->format_label, "BGRA8888") == 0) || (strcmp(__format->format_label, "RGBA8888") == 0) || (strcmp(__format->format_label, "ABGR8888") == 0)) {
379 SAFE_STRCPY(__format->colorspace, "RGBA", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
380 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
381 SAFE_STRCPY(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
383 gstcs_error("Check your colorspace format label");
384 GSTCS_FREE(__format->colorspace);
388 static int _gstcs_create_image_format(image_format_s **format)
390 int ret = GSTCS_ERROR_NONE;
391 image_format_s *_format = NULL;
393 if (format == NULL) {
394 gstcs_error("format is wrong value");
395 return GSTCS_ERROR_INVALID_OPERATION;
398 _format = (image_format_s*)malloc(sizeof(image_format_s));
399 if (_format == NULL) {
400 gstcs_error("memory allocation failed");
401 return GSTCS_ERROR_OUT_OF_MEMORY;
403 memset(_format, 0, sizeof(image_format_s));
409 static void _gstcs_destroy_image_format(image_format_s *format)
411 if (format != NULL) {
412 gst_caps_unref(format->caps);
413 GSTCS_FREE(format->format_label);
414 GSTCS_FREE(format->colorspace);
420 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
422 if (strcmp(pFormat->colorspace, "YUV") == 0) {
423 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
424 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
425 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
426 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
427 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
428 } else if ((strcmp(pFormat->colorspace, "RGBA") == 0) || (strcmp(pFormat->colorspace, "BGRX") == 0)) {
429 pFormat->stride = pFormat->width;
430 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
432 gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
435 static image_format_s*
436 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
438 int ret = GSTCS_ERROR_NONE;
439 image_format_s* __format = NULL;
441 ret = _gstcs_create_image_format(&__format);
442 if (ret != GSTCS_ERROR_NONE) {
443 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
447 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
448 if (__format->format_label == NULL) {
449 gstcs_error("memory allocation failed");
450 _gstcs_destroy_image_format(__format);
453 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
454 SAFE_STRCPY(__format->format_label, pImgp_info->input_format_label, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
455 gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
456 _mm_set_image_colorspace(__format);
458 __format->width = pImgp_info->src_width;
459 __format->height = pImgp_info->src_height;
460 _mm_round_up_input_image_widh_height(__format);
462 _mm_set_image_input_format_s_capabilities(__format);
468 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
470 if (strcmp(pFormat->colorspace, "YUV") == 0) {
471 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
472 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
473 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
474 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
475 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
476 pFormat->width = pFormat->stride;
477 if (input_format->height != input_format->elevation)
478 pFormat->height = pFormat->elevation;
479 } else if ((strcmp(pFormat->colorspace, "RGBA") == 0) || (strcmp(pFormat->colorspace, "BGRX") == 0)) {
480 pFormat->stride = pFormat->width;
481 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
482 if (input_format->height != input_format->elevation)
483 pFormat->height = pFormat->elevation;
485 gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
488 static image_format_s*
489 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
491 int ret = GSTCS_ERROR_NONE;
492 image_format_s* __format = NULL;
494 ret = _gstcs_create_image_format(&__format);
495 if (ret != GSTCS_ERROR_NONE) {
496 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
500 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
501 if (__format->format_label == NULL) {
502 gstcs_error("memory allocation failed");
503 _gstcs_destroy_image_format(__format);
506 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
507 SAFE_STRCPY(__format->format_label, pImgp_info->output_format_label, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
508 _mm_set_image_colorspace(__format);
510 __format->width = pImgp_info->dst_width;
511 __format->height = pImgp_info->dst_height;
512 _mm_round_up_output_image_widh_height(__format, input_format);
514 pImgp_info->output_stride = __format->stride;
515 pImgp_info->output_elevation = __format->elevation;
517 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
518 _mm_set_image_output_format_s_capabilities(__format);
523 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
525 int ret = GSTCS_ERROR_NONE;
527 if (pGstreamer_s->pipeline == NULL) {
528 gstcs_error("pipeline is NULL\n");
529 return GSTCS_ERROR_INVALID_PARAMETER;
532 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
533 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
535 if (gst_buf == NULL) {
536 gstcs_error("buffer is NULL\n");
537 return GSTCS_ERROR_INVALID_PARAMETER;
540 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
545 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsigned char *src, gstreamer_s * pGstreamer_s)
547 int ret = GSTCS_ERROR_NONE;
548 GstBuffer *gst_buf = NULL;
549 unsigned int src_size = 0;
550 unsigned char *data = NULL;
551 unsigned int stride = input_format->stride;
552 unsigned int elevation = input_format->elevation;
554 if (pGstreamer_s->pipeline == NULL) {
555 gstcs_error("pipeline is NULL\n");
556 return GSTCS_ERROR_INVALID_PARAMETER;
559 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
560 src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
561 gstcs_debug("buffer size (src): %d", src_size);
563 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
564 unsigned int src_row = input_format->width * byte_per_pixcel;
565 unsigned int stride_row = stride * byte_per_pixcel;
566 unsigned int i = 0, y = 0;
567 gstcs_debug("padding will be inserted to buffer");
568 data = (unsigned char *) malloc(src_size);
570 gstcs_error("app_buffer is NULL\n");
571 return GSTCS_ERROR_INVALID_PARAMETER;
573 for (y = 0; y < (unsigned int)(input_format->height); y++) {
574 guint8 *pLine = (guint8 *) &(src[src_row * y]);
575 for (i = 0; i < src_row; i++)
576 data[y * stride_row + i] = pLine[i];
577 guint8 stride_row_color = pLine[i - 1];
578 for (i = src_row; i < stride_row; i++)
579 data[y * stride_row + i] = stride_row_color;
581 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
582 for (i = 0; i < stride_row; i++)
583 data[y * stride_row + i] = data[(y - 1) * stride_row + i];
585 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
587 if (gst_buf == NULL) {
588 gstcs_error("buffer is NULL\n");
590 return GSTCS_ERROR_INVALID_PARAMETER;
593 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
598 _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)
601 GstStateChangeReturn ret_state;
602 int ret = GSTCS_ERROR_NONE;
605 ret = _mm_create_pipeline(pGstreamer_s);
606 if (ret != GSTCS_ERROR_NONE)
607 gstcs_error("ERROR - mm_create_pipeline ");
609 /* 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. */
610 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
612 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
613 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
614 gst_object_unref(bus);
616 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
617 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
619 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
620 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
621 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
622 ret = _mm_push_buffer_into_pipeline_new(input_format, src, pGstreamer_s);
624 gstcs_debug("Start mm_push_buffer_into_pipeline");
625 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
627 if (ret != GSTCS_ERROR_NONE) {
628 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
629 gst_object_unref(pGstreamer_s->pipeline);
632 gstcs_debug("End mm_push_buffer_into_pipeline");
635 gstcs_debug("Start mm_link_pipeline");
636 _mm_link_pipeline(pGstreamer_s, pImgp_info->angle);
637 gstcs_debug("End mm_link_pipeline");
639 /* Conecting to the new-sample signal emited by the appsink*/
640 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
641 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
642 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
644 /* GST_STATE_PLAYING*/
645 gstcs_debug("Start GST_STATE_PLAYING");
646 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
647 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
650 gstcs_debug("g_main_loop_run");
651 g_main_loop_run(pGstreamer_s->loop);
653 gstcs_debug("Sucess GST_STATE_CHANGE");
656 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
657 gstcs_debug("End GST_STATE_NULL");
659 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
661 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
663 if (ret_state == GST_STATE_CHANGE_SUCCESS)
664 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
665 else if (ret_state == GST_STATE_CHANGE_ASYNC)
666 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
668 gstcs_debug("Success gst_element_get_state\n");
670 if (ret_state == GST_STATE_CHANGE_FAILURE) {
671 gstcs_error("GST_STATE_CHANGE_FAILURE");
673 if (pGstreamer_s->output_buffer != NULL) {
674 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
675 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
676 int buffer_size = mapinfo.size;
677 int calc_buffer_size = 0;
678 if (((pImgp_info->dst_width != (unsigned int)(output_format->width)) || (pImgp_info->dst_height != (unsigned int)(output_format->height))) &&
679 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
680 gstcs_debug("calculate image size with stride & elevation");
681 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
683 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
685 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
686 if (buffer_size != calc_buffer_size) {
687 gstcs_debug("Buffer size is different \n");
688 gstcs_debug("unref output buffer");
689 gst_buffer_unref(pGstreamer_s->output_buffer);
690 gst_object_unref(pGstreamer_s->pipeline);
691 pGstreamer_s->output_buffer = NULL;
692 return GSTCS_ERROR_INVALID_OPERATION;
694 gstcs_debug("pGstreamer_s->output_buffer: %p\n", pGstreamer_s->output_buffer);
695 *dst = calloc(1, buffer_size);
697 gstcs_error("ERROR - calloc ");
698 gst_buffer_unref(pGstreamer_s->output_buffer);
699 gst_object_unref(pGstreamer_s->pipeline);
700 pGstreamer_s->output_buffer = NULL;
701 return GSTCS_ERROR_INVALID_OPERATION;
704 memcpy(*dst, mapinfo.data, buffer_size);
705 pImgp_info->buffer_size = buffer_size;
706 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
708 gstcs_debug("pGstreamer_s->output_buffer is NULL");
711 gstcs_debug("unref output buffer");
712 gst_buffer_unref(pGstreamer_s->output_buffer);
713 gst_object_unref(pGstreamer_s->pipeline);
714 pGstreamer_s->output_buffer = NULL;
716 gstcs_debug("End gstreamer processing");
717 gstcs_debug("dst: %p", *dst);
723 mm_setup_image_size(const char* _format_label, int width, int height)
727 if (strcmp(_format_label, "I420") == 0)
728 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; */
729 else if (strcmp(_format_label, "Y42B") == 0)
730 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
731 else if (strcmp(_format_label, "YUV422") == 0)
732 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
733 else if (strcmp(_format_label, "Y444") == 0)
734 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
735 else if (strcmp(_format_label, "YV12") == 0)
736 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; */
737 else if (strcmp(_format_label, "NV12") == 0)
738 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
739 else if (strcmp(_format_label, "RGB565") == 0)
740 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
741 else if (strcmp(_format_label, "RGB888") == 0)
742 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
743 else if (strcmp(_format_label, "BGR888") == 0)
744 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
745 else if (strcmp(_format_label, "UYVY") == 0)
746 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
747 else if (strcmp(_format_label, "YUYV") == 0)
748 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
749 else if (strcmp(_format_label, "ARGB8888") == 0)
750 size = width * height *4;
751 else if (strcmp(_format_label, "BGRA8888") == 0)
752 size = width * height *4;
753 else if (strcmp(_format_label, "RGBA8888") == 0)
754 size = width * height *4;
755 else if (strcmp(_format_label, "ABGR8888") == 0)
756 size = width * height *4;
757 else if (strcmp(_format_label, "BGRX") == 0)
758 size = width * height *4;
760 gstcs_debug("file_size: %d\n", size);
765 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
767 if (gstreamer == NULL) {
768 gstcs_error("ERROR - gstreamer is null ");
769 return GSTCS_ERROR_INVALID_OPERATION;
772 gstreamer->context = g_main_context_new();
773 if (gstreamer->context == NULL) {
774 gstcs_error("ERROR - g_main_context_new ");
775 return GSTCS_ERROR_INVALID_OPERATION;
777 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
778 if (gstreamer->loop == NULL) {
779 gstcs_error("ERROR - g_main_loop_new ");
780 g_main_context_unref(gstreamer->context);
781 return GSTCS_ERROR_INVALID_OPERATION;
784 g_main_context_push_thread_default(gstreamer->context);
785 return GSTCS_ERROR_NONE;
788 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
790 if (gstreamer == NULL) {
791 gstcs_error("ERROR - gstreamer is null ");
792 return GSTCS_ERROR_INVALID_OPERATION;
795 if (gstreamer->loop != NULL)
796 g_main_loop_unref(gstreamer->loop);
798 if (gstreamer->context != NULL)
799 g_main_context_unref(gstreamer->context);
801 return GSTCS_ERROR_NONE;
804 static int _gstcs_init(gstreamer_s** gstreamer)
806 static const int max_argc = 50;
810 int ret = GSTCS_ERROR_NONE;
812 argv = malloc(sizeof(gchar*) * max_argc);
815 gstcs_error("argv is not allocated");
816 return GSTCS_ERROR_OUT_OF_MEMORY;
818 memset(argv, 0, sizeof(gchar*) * max_argc);
820 argv[argc] = g_strdup("mmutil_gstcs");
821 if (argv[argc] == NULL) {
822 gstcs_error("argv[%d] is not allocated", argc);
823 ret = GSTCS_ERROR_OUT_OF_MEMORY;
826 /* check disable registry scan */
827 argv[argc] = g_strdup("--gst-disable-registry-update");
828 if (argv[argc] == NULL) {
829 gstcs_error("argv[%d] is not allocated", argc);
830 ret = GSTCS_ERROR_OUT_OF_MEMORY;
833 if (ret != GSTCS_ERROR_NONE) {
834 for (i = 0; i < argc; i++)
841 gst_init(&argc, &argv);
843 *gstreamer = g_new0(gstreamer_s, 1);
844 if (*gstreamer == NULL) {
845 gstcs_error("gstreamer structure is not allocated");
846 ret = GSTCS_ERROR_OUT_OF_MEMORY;
849 for (i = 0; i < argc; i++)
856 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst)
858 image_format_s* input_format = NULL, *output_format = NULL;
859 gstreamer_s* pGstreamer_s;
860 int ret = GSTCS_ERROR_NONE;
862 /* Print debug message for inout structure */
863 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
864 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);
866 /* Initialize gstreamer */
867 ret = _gstcs_init(&pGstreamer_s);
868 if (ret != GSTCS_ERROR_NONE) {
869 gstcs_error("Error: _gstcs_new is failed");
873 /* Create input/output format for gstreamer processing */
874 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
875 if (input_format == NULL) {
876 gstcs_error("Error: memory allocation failed");
877 GSTCS_FREE(pGstreamer_s);
878 return GSTCS_ERROR_OUT_OF_MEMORY;
881 output_format = _mm_set_output_image_format_s_struct(pImgp_info, input_format);
882 if (output_format == NULL) {
883 gstcs_error("Error: memory allocation failed");
884 _gstcs_destroy_image_format(input_format);
885 GSTCS_FREE(pGstreamer_s);
886 return GSTCS_ERROR_OUT_OF_MEMORY;
889 /* Create default thread for async behavior */
890 ret = _gstcs_create_default_thread(pGstreamer_s);
891 if (ret != GSTCS_ERROR_NONE) {
892 gstcs_error("Error: _gstcs_create_default_thread is failed");
893 _gstcs_destroy_image_format(input_format);
894 _gstcs_destroy_image_format(output_format);
895 GSTCS_FREE(pGstreamer_s);
899 /* Do gstreamer processing */
900 gstcs_debug("Start _mm_imgp_gstcs_processing ");
901 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 */
903 if (ret == GSTCS_ERROR_NONE)
904 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst);
905 else if (ret != GSTCS_ERROR_NONE)
906 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
909 ret = _gstcs_destroy_default_thread(pGstreamer_s);
910 if (ret != GSTCS_ERROR_NONE)
911 gstcs_error("Error: _gstcs_create_default_thread is failed");
913 _gstcs_destroy_image_format(input_format);
914 _gstcs_destroy_image_format(output_format);
915 GSTCS_FREE(pGstreamer_s);
920 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst, imgp_type_e _imgp_type)
922 if (pImgp_info == NULL) {
923 gstcs_error("Error: input vaule is NULL");
924 return GSTCS_ERROR_INVALID_PARAMETER;
927 if (src == NULL || dst == NULL) {
928 gstcs_error("Error: src | dst is NULL");
929 return GSTCS_ERROR_INVALID_PARAMETER;
932 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
933 gstcs_error("Error: imgp_type is wrong");
934 return GSTCS_ERROR_INVALID_PARAMETER;
937 gstcs_debug("[src %p] [dst %p]", src, dst);
939 return _mm_imgp_gstcs(pImgp_info, src, dst);