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)
32 _mm_sink_sample(GstElement * appsink, gpointer user_data)
34 GstBuffer *_buf = NULL;
35 GstSample *_sample = NULL;
36 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
37 _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
39 _buf = gst_sample_get_buffer(_sample);
41 pGstreamer_s->output_buffer = _buf;
43 if (pGstreamer_s->output_buffer != NULL) {
44 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
45 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
46 gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
47 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
49 gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
53 gst_buffer_ref(pGstreamer_s->output_buffer); /* when you want to avoid flushing */
54 gst_sample_unref(_sample);
60 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
62 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
63 switch (GST_MESSAGE_TYPE(message)) {
64 case GST_MESSAGE_EOS: {
65 gstcs_debug("The source got dry");
66 gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
67 g_main_context_pop_thread_default(pGstreamer_s->context);
68 g_main_loop_quit(pGstreamer_s->loop);
71 case GST_MESSAGE_ERROR: {
73 gchar *dbg_info = NULL;
75 gst_message_parse_error(message, &err, &dbg_info);
76 gstcs_error("ERROR from element %s: %s\n", GST_OBJECT_NAME(message->src), err->message);
77 gstcs_error("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
80 g_main_context_pop_thread_default(pGstreamer_s->context);
81 g_main_loop_quit(pGstreamer_s->loop);
82 gstcs_debug("Quit GST_CS\n");
92 _mm_get_byte_per_pixcel(const char *__format_label)
94 int byte_per_pixcel = 1;
95 if (strcmp(__format_label, "RGB565") == 0) {
97 } else if (strcmp(__format_label, "RGB888") == 0 ||
98 strcmp(__format_label, "BGR888") == 0) {
100 } else if (strcmp(__format_label, "RGBA8888") == 0 ||
101 strcmp(__format_label, "ARGB8888") == 0 ||
102 strcmp(__format_label, "BGRA8888") == 0 ||
103 strcmp(__format_label, "ABGR8888") == 0 ||
104 strcmp(__format_label, "RGBX") == 0 ||
105 strcmp(__format_label, "XRGB") == 0 ||
106 strcmp(__format_label, "BGRX") == 0 ||
107 strcmp(__format_label, "XBGR") == 0) {
111 gstcs_debug("byte per pixcel : %d", byte_per_pixcel);
113 return byte_per_pixcel;
116 static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
118 int ret = GSTCS_ERROR_NONE;
119 pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
120 if (!pGstreamer_s->pipeline) {
121 gstcs_error("pipeline could not be created. Exiting.\n");
122 ret = GSTCS_ERROR_INVALID_PARAMETER;
124 pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
125 if (!pGstreamer_s->appsrc) {
126 gstcs_error("appsrc could not be created. Exiting.\n");
127 ret = GSTCS_ERROR_INVALID_PARAMETER;
129 pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
130 if (!pGstreamer_s->colorspace) {
131 gstcs_error("colorspace could not be created. Exiting.\n");
132 ret = GSTCS_ERROR_INVALID_PARAMETER;
134 pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
135 if (!pGstreamer_s->videoscale) {
136 gstcs_error("videoscale could not be created. Exiting.\n");
137 ret = GSTCS_ERROR_INVALID_PARAMETER;
139 pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
140 if (!pGstreamer_s->videoflip) {
141 gstcs_error("videoflip could not be created. Exiting.\n");
142 ret = GSTCS_ERROR_INVALID_PARAMETER;
144 pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
145 if (!pGstreamer_s->appsink) {
146 gstcs_error("appsink could not be created. Exiting.\n");
147 ret = GSTCS_ERROR_INVALID_PARAMETER;
152 static void _mm_destroy_notify(gpointer data)
154 unsigned char *_data = (unsigned char *)data;
162 _mm_check_caps_format(GstCaps* caps)
164 GstStructure *caps_structure = gst_caps_get_structure(caps, 0);
165 const gchar* formatInfo = gst_structure_get_string(caps_structure, "format");
166 gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
170 _mm_link_pipeline(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int value)
173 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);
174 if (!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL))
175 gstcs_error("Fail to link pipeline");
177 gstcs_debug("Success to link pipeline");
179 g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
180 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
181 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
183 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 */
185 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
186 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
190 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
192 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
193 GstVideoFormat videoFormat;
202 if (__format == NULL) {
203 gstcs_error("Image format is NULL\n");
207 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
208 memset(_format_name, 0, sizeof(_format_name));
210 if (strcmp(__format->colorspace, "YUV") == 0) {
211 if (strcmp(__format->format_label, "I420") == 0
212 || strcmp(__format->format_label, "Y42B") == 0
213 || strcmp(__format->format_label, "Y444") == 0
214 || strcmp(__format->format_label, "YV12") == 0
215 || strcmp(__format->format_label, "NV12") == 0
216 || strcmp(__format->format_label, "UYVY") == 0) {
217 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
218 } else if (strcmp(__format->format_label, "YUYV") == 0) {
219 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
221 gstcs_debug("Chosen video format: %s", _format_name);
222 __format->caps = gst_caps_new_simple("video/x-raw",
223 "format", G_TYPE_STRING, _format_name,
224 "framerate", GST_TYPE_FRACTION, 25, 1,
225 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
226 "width", G_TYPE_INT, __format->width,
227 "height", G_TYPE_INT, __format->height,
228 "framerate", GST_TYPE_FRACTION, 1, 1,
232 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
233 if (strcmp(__format->format_label, "RGB888") == 0) {
234 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
235 } else if (strcmp(__format->format_label, "BGR888") == 0) {
236 _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
237 } else if (strcmp(__format->format_label, "RGB565") == 0) {
238 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
239 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
240 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
243 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
244 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
246 __format->caps = gst_caps_new_simple("video/x-raw",
247 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
248 "width", G_TYPE_INT, __format->stride,
249 "height", G_TYPE_INT, __format->elevation,
250 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
253 else if (strcmp(__format->colorspace, "RGBA") == 0) {
254 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
255 gstcs_debug("ARGB8888");
256 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
257 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
258 gstcs_debug("BGRA8888");
259 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
260 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
261 gstcs_debug("RGBA8888");
262 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
263 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
264 gstcs_debug("ABGR8888");
265 _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
267 gstcs_error("***Wrong format cs type***\n");
270 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
271 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
273 __format->caps = gst_caps_new_simple("video/x-raw",
274 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
275 "width", G_TYPE_INT, __format->width,
276 "height", G_TYPE_INT, __format->elevation,
277 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
280 if (__format->caps) {
281 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
282 _mm_check_caps_format(__format->caps);
284 gstcs_error("__format->caps is NULL");
289 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
291 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
292 GstVideoFormat videoFormat;
293 int _bpp = 0; int _depth = 0; int _red_mask = 0; int _green_mask = 0; int _blue_mask = 0; int _alpha_mask = 0; int _endianness = 0;
295 if (__format == NULL) {
296 gstcs_error("Image format is NULL\n");
300 gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
301 memset(_format_name, 0, sizeof(_format_name));
303 if (strcmp(__format->colorspace, "YUV") == 0) {
304 if (strcmp(__format->format_label, "I420") == 0
305 || strcmp(__format->format_label, "Y42B") == 0
306 || strcmp(__format->format_label, "Y444") == 0
307 || strcmp(__format->format_label, "YV12") == 0
308 || strcmp(__format->format_label, "NV12") == 0
309 || strcmp(__format->format_label, "UYVY") == 0) {
310 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
311 } else if (strcmp(__format->format_label, "YUYV") == 0) {
312 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
314 gstcs_debug("Chosen video format: %s", _format_name);
315 __format->caps = gst_caps_new_simple("video/x-raw",
316 "format", G_TYPE_STRING, _format_name,
317 "width", G_TYPE_INT, __format->width,
318 "height", G_TYPE_INT, __format->height,
319 "framerate", GST_TYPE_FRACTION, 1, 1,
323 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
324 if (strcmp(__format->format_label, "RGB888") == 0) {
325 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
326 } else if (strcmp(__format->format_label, "BGR888") == 0) {
327 _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
328 } else if (strcmp(__format->format_label, "RGB565") == 0) {
329 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
330 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
331 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
334 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
335 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
337 __format->caps = gst_caps_new_simple("video/x-raw",
338 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
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 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
346 gstcs_debug("ARGB8888");
347 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
348 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
349 gstcs_debug("BGRA8888");
350 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
351 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
352 gstcs_debug("RGBA8888");
353 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
354 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
355 gstcs_debug("ABGR8888");
356 _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
358 gstcs_error("***Wrong format cs type***\n");
361 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
362 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
364 __format->caps = gst_caps_new_simple("video/x-raw",
365 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
366 "width", G_TYPE_INT, __format->width,
367 "height", G_TYPE_INT, __format->height,
368 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
371 if (__format->caps) {
372 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
373 _mm_check_caps_format(__format->caps);
375 gstcs_error("__format->caps is NULL");
380 _mm_set_image_colorspace(image_format_s* __format)
382 gstcs_debug("format_label: %s\n", __format->format_label);
384 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
385 if (__format->colorspace == NULL) {
386 gstcs_error("memory allocation failed");
389 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
390 if ((strcmp(__format->format_label, "I420") == 0) || (strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
391 || (strcmp(__format->format_label, "YV12") == 0) || (strcmp(__format->format_label, "NV12") == 0) || (strcmp(__format->format_label, "UYVY") == 0) || (strcmp(__format->format_label, "YUYV") == 0)) {
392 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
393 } else if ((strcmp(__format->format_label, "RGB888") == 0) || (strcmp(__format->format_label, "BGR888") == 0) || (strcmp(__format->format_label, "RGB565") == 0)) {
394 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
395 } 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)) {
396 strncpy(__format->colorspace, "RGBA", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
397 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
398 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
400 gstcs_error("Check your colorspace format label");
401 GSTCS_FREE(__format->colorspace);
405 static int _gstcs_create_image_format(image_format_s **format)
407 int ret = GSTCS_ERROR_NONE;
408 image_format_s *_format = NULL;
410 if (format == NULL) {
411 gstcs_error("format is wrong value");
412 return GSTCS_ERROR_INVALID_OPERATION;
415 _format = (image_format_s*)malloc(sizeof(image_format_s));
416 if (_format == NULL) {
417 gstcs_error("memory allocation failed");
418 return GSTCS_ERROR_OUT_OF_MEMORY;
420 memset(_format, 0, sizeof(image_format_s));
426 static void _gstcs_destroy_image_format(image_format_s *format)
428 if (format != NULL) {
429 GSTCS_FREE(format->format_label);
430 GSTCS_FREE(format->colorspace);
436 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
438 if (strcmp(pFormat->colorspace, "YUV") == 0) {
439 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
440 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
441 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
442 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
443 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
444 } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
445 pFormat->stride = pFormat->width;
446 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
448 gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
451 static image_format_s*
452 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
454 int ret = GSTCS_ERROR_NONE;
455 image_format_s* __format = NULL;
457 ret = _gstcs_create_image_format(&__format);
458 if (ret != GSTCS_ERROR_NONE) {
459 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
463 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
464 if (__format->format_label == NULL) {
465 gstcs_error("memory allocation failed");
466 _gstcs_destroy_image_format(__format);
469 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
470 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
471 gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
472 _mm_set_image_colorspace(__format);
474 __format->width = pImgp_info->src_width;
475 __format->height = pImgp_info->src_height;
476 _mm_round_up_input_image_widh_height(__format);
478 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
479 gstcs_debug("blocksize: %d\n", __format->blocksize);
480 _mm_set_image_input_format_s_capabilities(__format);
486 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
488 if (strcmp(pFormat->colorspace, "YUV") == 0) {
489 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
490 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
491 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
492 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
493 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
494 pFormat->width = pFormat->stride;
495 if (input_format->height != input_format->elevation)
496 pFormat->height = pFormat->elevation;
497 } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
498 pFormat->stride = pFormat->width;
499 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
500 if (input_format->height != input_format->elevation)
501 pFormat->height = pFormat->elevation;
503 gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
506 static image_format_s*
507 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
509 int ret = GSTCS_ERROR_NONE;
510 image_format_s* __format = NULL;
512 ret = _gstcs_create_image_format(&__format);
513 if (ret != GSTCS_ERROR_NONE) {
514 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
518 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
519 if (__format->format_label == NULL) {
520 gstcs_error("memory allocation failed");
521 _gstcs_destroy_image_format(__format);
524 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
525 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
526 _mm_set_image_colorspace(__format);
528 __format->width = pImgp_info->dst_width;
529 __format->height = pImgp_info->dst_height;
530 _mm_round_up_output_image_widh_height(__format, input_format);
532 pImgp_info->output_stride = __format->stride;
533 pImgp_info->output_elevation = __format->elevation;
535 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
536 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
537 _mm_set_image_output_format_s_capabilities(__format);
542 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
544 int ret = GSTCS_ERROR_NONE;
546 if (pGstreamer_s->pipeline == NULL) {
547 gstcs_error("pipeline is NULL\n");
548 return GSTCS_ERROR_INVALID_PARAMETER;
551 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
552 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
554 if (gst_buf == NULL) {
555 gstcs_error("buffer is NULL\n");
556 return GSTCS_ERROR_INVALID_PARAMETER;
559 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
564 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
566 int ret = GSTCS_ERROR_NONE;
567 GstBuffer *gst_buf = NULL;
568 unsigned int src_size = 0;
569 unsigned char *data = NULL;
570 unsigned int stride = input_format->stride;
571 unsigned int elevation = input_format->elevation;
573 if (pGstreamer_s->pipeline == NULL) {
574 gstcs_error("pipeline is NULL\n");
575 return GSTCS_ERROR_INVALID_PARAMETER;
578 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
579 src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
580 gstcs_debug("buffer size (src): %d", src_size);
582 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
583 unsigned int src_row = input_format->width * byte_per_pixcel;
584 unsigned int stride_row = stride * byte_per_pixcel;
585 unsigned int i = 0, y = 0;
586 gstcs_debug("padding will be inserted to buffer");
587 data = (unsigned char *) malloc(src_size);
589 gstcs_error("app_buffer is NULL\n");
590 return GSTCS_ERROR_INVALID_PARAMETER;
592 for (y = 0; y < (unsigned int)(input_format->height); y++) {
593 guint8 *pLine = (guint8 *) &(src[src_row * y]);
594 for (i = 0; i < src_row; i++)
595 data[y * stride_row + i] = pLine[i];
597 for (i = src_row; i < stride_row; i++)
598 data[y * stride_row + i] = 0x00;
600 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
601 for (i = 0; i < stride_row; i++)
602 data[y * stride_row + i] = 0x00;
604 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
606 if (gst_buf == NULL) {
607 gstcs_error("buffer is NULL\n");
608 return GSTCS_ERROR_INVALID_PARAMETER;
611 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
616 _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)
619 GstStateChangeReturn ret_state;
620 int ret = GSTCS_ERROR_NONE;
623 ret = _mm_create_pipeline(pGstreamer_s);
624 if (ret != GSTCS_ERROR_NONE)
625 gstcs_error("ERROR - mm_create_pipeline ");
627 /* 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. */
628 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
630 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
631 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
632 gst_object_unref(bus);
634 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
635 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
637 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
638 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
639 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
640 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
642 gstcs_debug("Start mm_push_buffer_into_pipeline");
643 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
645 if (ret != GSTCS_ERROR_NONE) {
646 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
647 gst_object_unref(pGstreamer_s->pipeline);
650 gstcs_debug("End mm_push_buffer_into_pipeline");
653 gstcs_debug("Start mm_link_pipeline");
654 _mm_link_pipeline(pGstreamer_s, input_format, output_format, pImgp_info->angle);
655 gstcs_debug("End mm_link_pipeline");
657 /* Conecting to the new-sample signal emited by the appsink*/
658 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
659 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
660 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
662 /* GST_STATE_PLAYING*/
663 gstcs_debug("Start GST_STATE_PLAYING");
664 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
665 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
668 gstcs_debug("g_main_loop_run");
669 g_main_loop_run(pGstreamer_s->loop);
671 gstcs_debug("Sucess GST_STATE_CHANGE");
674 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
675 gstcs_debug("End GST_STATE_NULL");
677 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
679 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
681 if (ret_state == GST_STATE_CHANGE_SUCCESS)
682 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
683 else if (ret_state == GST_STATE_CHANGE_ASYNC)
684 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
686 gstcs_debug("Success gst_element_get_state\n");
688 if (ret_state == GST_STATE_CHANGE_FAILURE) {
689 gstcs_error("GST_STATE_CHANGE_FAILURE");
691 if (pGstreamer_s->output_buffer != NULL) {
692 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
693 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
694 int buffer_size = mapinfo.size;
695 int calc_buffer_size = 0;
696 if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
697 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
698 gstcs_debug("calculate image size with stride & elevation");
699 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
701 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
703 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
704 if (buffer_size != calc_buffer_size) {
705 gstcs_debug("Buffer size is different \n");
706 gstcs_debug("unref output buffer");
707 gst_buffer_unref(pGstreamer_s->output_buffer);
708 gst_object_unref(pGstreamer_s->pipeline);
709 pGstreamer_s->output_buffer = NULL;
710 return GSTCS_ERROR_INVALID_OPERATION;
712 gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
713 memcpy(dst, mapinfo.data, buffer_size);
714 pImgp_info->buffer_size = buffer_size;
715 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
717 gstcs_debug("pGstreamer_s->output_buffer is NULL");
720 gstcs_debug("unref output buffer");
721 gst_buffer_unref(pGstreamer_s->output_buffer);
722 gst_object_unref(pGstreamer_s->pipeline);
723 pGstreamer_s->output_buffer = NULL;
725 gstcs_debug("End gstreamer processing");
726 gstcs_debug("dst: %p", dst);
732 mm_setup_image_size(const char* _format_label, int width, int height)
736 if (strcmp(_format_label, "I420") == 0) {
737 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; */
738 } else if (strcmp(_format_label, "Y42B") == 0) {
739 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
740 } else if (strcmp(_format_label, "YUV422") == 0) {
741 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
742 } else if (strcmp(_format_label, "Y444") == 0) {
743 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
744 } else if (strcmp(_format_label, "YV12") == 0) {
745 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; */
746 } else if (strcmp(_format_label, "NV12") == 0) {
747 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
748 } else if (strcmp(_format_label, "RGB565") == 0) {
749 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
750 } else if (strcmp(_format_label, "RGB888") == 0) {
751 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
752 } else if (strcmp(_format_label, "BGR888") == 0) {
753 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
754 } else if (strcmp(_format_label, "UYVY") == 0) {
755 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
756 } else if (strcmp(_format_label, "YUYV") == 0) {
757 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
758 } else if (strcmp(_format_label, "ARGB8888") == 0) {
759 size = width * height *4;
760 } else if (strcmp(_format_label, "BGRA8888") == 0) {
761 size = width * height *4;
762 } else if (strcmp(_format_label, "RGBA8888") == 0) {
763 size = width * height *4;
764 } else if (strcmp(_format_label, "ABGR8888") == 0) {
765 size = width * height *4;
766 } else if (strcmp(_format_label, "BGRX") == 0) {
767 size = width * height *4;
770 gstcs_debug("file_size: %d\n", size);
775 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
777 if (gstreamer == NULL) {
778 gstcs_error("ERROR - gstreamer is null ");
779 return GSTCS_ERROR_INVALID_OPERATION;
782 gstreamer->context = g_main_context_new();
783 if (gstreamer->context == NULL) {
784 gstcs_error("ERROR - g_main_context_new ");
785 return GSTCS_ERROR_INVALID_OPERATION;
787 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
788 if (gstreamer->loop == NULL) {
789 gstcs_error("ERROR - g_main_loop_new ");
790 g_main_context_unref(gstreamer->context);
791 return GSTCS_ERROR_INVALID_OPERATION;
794 g_main_context_push_thread_default(gstreamer->context);
795 return GSTCS_ERROR_NONE;
798 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
800 if (gstreamer == NULL) {
801 gstcs_error("ERROR - gstreamer is null ");
802 return GSTCS_ERROR_INVALID_OPERATION;
805 if (gstreamer->loop != NULL)
806 g_main_loop_unref(gstreamer->loop);
808 if (gstreamer->context != NULL)
809 g_main_context_unref(gstreamer->context);
811 return GSTCS_ERROR_NONE;
814 static int _gstcs_init(gstreamer_s** gstreamer)
816 static const int max_argc = 50;
820 int ret = GSTCS_ERROR_NONE;
822 argv = malloc(sizeof(gchar*) * max_argc);
825 gstcs_error("argv is not allocated");
826 return GSTCS_ERROR_OUT_OF_MEMORY;
828 memset(argv, 0, sizeof(gchar*) * max_argc);
830 argv[argc] = g_strdup("mmutil_gstcs");
831 if (argv[argc] == NULL) {
832 gstcs_error("argv[%d] is not allocated", argc);
833 ret = GSTCS_ERROR_OUT_OF_MEMORY;
836 /* check disable registry scan */
837 argv[argc] = g_strdup("--gst-disable-registry-update");
838 if (argv[argc] == NULL) {
839 gstcs_error("argv[%d] is not allocated", argc);
840 ret = GSTCS_ERROR_OUT_OF_MEMORY;
843 if (ret != GSTCS_ERROR_NONE) {
844 for (i = 0; i < argc; i++)
851 gst_init(&argc, &argv);
853 *gstreamer = g_new0(gstreamer_s, 1);
854 if (*gstreamer == NULL) {
855 gstcs_error("gstreamer structure is not allocated");
856 ret = GSTCS_ERROR_OUT_OF_MEMORY;
859 for (i = 0; i < argc; i++)
866 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
868 image_format_s* input_format = NULL, *output_format = NULL;
869 gstreamer_s* pGstreamer_s;
870 int ret = GSTCS_ERROR_NONE;
872 /* Print debug message for inout structure */
873 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
874 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);
876 /* Initialize gstreamer */
877 ret = _gstcs_init(&pGstreamer_s);
878 if (ret != GSTCS_ERROR_NONE) {
879 gstcs_error("Error: _gstcs_new is failed");
883 /* Create input/output format for gstreamer processing */
884 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
885 if (input_format == NULL) {
886 gstcs_error("Error: memory allocation failed");
887 GSTCS_FREE(pGstreamer_s);
888 return GSTCS_ERROR_OUT_OF_MEMORY;
891 output_format = _mm_set_output_image_format_s_struct(pImgp_info, input_format);
892 if (output_format == NULL) {
893 gstcs_error("Error: memory allocation failed");
894 _gstcs_destroy_image_format(input_format);
895 GSTCS_FREE(pGstreamer_s);
896 return GSTCS_ERROR_OUT_OF_MEMORY;
899 /* Create default thread for async behavior */
900 ret = _gstcs_create_default_thread(pGstreamer_s);
901 if (ret != GSTCS_ERROR_NONE) {
902 gstcs_error("Error: _gstcs_create_default_thread is failed");
903 _gstcs_destroy_image_format(input_format);
904 _gstcs_destroy_image_format(output_format);
905 GSTCS_FREE(pGstreamer_s);
909 /* Do gstreamer processing */
910 gstcs_debug("Start _mm_imgp_gstcs_processing ");
911 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 */
913 if (ret == GSTCS_ERROR_NONE)
914 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
915 else if (ret != GSTCS_ERROR_NONE)
916 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
919 ret = _gstcs_destroy_default_thread(pGstreamer_s);
920 if (ret != GSTCS_ERROR_NONE)
921 gstcs_error("Error: _gstcs_create_default_thread is failed");
923 _gstcs_destroy_image_format(input_format);
924 _gstcs_destroy_image_format(output_format);
925 GSTCS_FREE(pGstreamer_s);
930 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
932 if (pImgp_info == NULL) {
933 gstcs_error("Error: input vaule is NULL");
934 return GSTCS_ERROR_INVALID_PARAMETER;
937 if (src == NULL || dst == NULL) {
938 gstcs_error("Error: src | dst is NULL");
939 return GSTCS_ERROR_INVALID_PARAMETER;
942 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
943 gstcs_error("Error: imgp_type is wrong");
944 return GSTCS_ERROR_INVALID_PARAMETER;
947 gstcs_debug("[src %p] [dst %p]", src, dst);
949 return _mm_imgp_gstcs(pImgp_info, src, dst);