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 gst_caps_unref(format->caps);
430 GSTCS_FREE(format->format_label);
431 GSTCS_FREE(format->colorspace);
437 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
439 if (strcmp(pFormat->colorspace, "YUV") == 0) {
440 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
441 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
442 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
443 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
444 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
445 } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
446 pFormat->stride = pFormat->width;
447 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
449 gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
452 static image_format_s*
453 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
455 int ret = GSTCS_ERROR_NONE;
456 image_format_s* __format = NULL;
458 ret = _gstcs_create_image_format(&__format);
459 if (ret != GSTCS_ERROR_NONE) {
460 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
464 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
465 if (__format->format_label == NULL) {
466 gstcs_error("memory allocation failed");
467 _gstcs_destroy_image_format(__format);
470 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
471 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
472 gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
473 _mm_set_image_colorspace(__format);
475 __format->width = pImgp_info->src_width;
476 __format->height = pImgp_info->src_height;
477 _mm_round_up_input_image_widh_height(__format);
479 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
480 gstcs_debug("blocksize: %d\n", __format->blocksize);
481 _mm_set_image_input_format_s_capabilities(__format);
487 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
489 if (strcmp(pFormat->colorspace, "YUV") == 0) {
490 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
491 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
492 } else if (strcmp(pFormat->colorspace, "RGB") == 0) {
493 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
494 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
495 pFormat->width = pFormat->stride;
496 if (input_format->height != input_format->elevation)
497 pFormat->height = pFormat->elevation;
498 } else if (strcmp(pFormat->colorspace, "RGBA") == 0) {
499 pFormat->stride = pFormat->width;
500 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
501 if (input_format->height != input_format->elevation)
502 pFormat->height = pFormat->elevation;
504 gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
507 static image_format_s*
508 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
510 int ret = GSTCS_ERROR_NONE;
511 image_format_s* __format = NULL;
513 ret = _gstcs_create_image_format(&__format);
514 if (ret != GSTCS_ERROR_NONE) {
515 gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret);
519 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
520 if (__format->format_label == NULL) {
521 gstcs_error("memory allocation failed");
522 _gstcs_destroy_image_format(__format);
525 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
526 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
527 _mm_set_image_colorspace(__format);
529 __format->width = pImgp_info->dst_width;
530 __format->height = pImgp_info->dst_height;
531 _mm_round_up_output_image_widh_height(__format, input_format);
533 pImgp_info->output_stride = __format->stride;
534 pImgp_info->output_elevation = __format->elevation;
536 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
537 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
538 _mm_set_image_output_format_s_capabilities(__format);
543 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
545 int ret = GSTCS_ERROR_NONE;
547 if (pGstreamer_s->pipeline == NULL) {
548 gstcs_error("pipeline is NULL\n");
549 return GSTCS_ERROR_INVALID_PARAMETER;
552 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
553 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
555 if (gst_buf == NULL) {
556 gstcs_error("buffer is NULL\n");
557 return GSTCS_ERROR_INVALID_PARAMETER;
560 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
565 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
567 int ret = GSTCS_ERROR_NONE;
568 GstBuffer *gst_buf = NULL;
569 unsigned int src_size = 0;
570 unsigned char *data = NULL;
571 unsigned int stride = input_format->stride;
572 unsigned int elevation = input_format->elevation;
574 if (pGstreamer_s->pipeline == NULL) {
575 gstcs_error("pipeline is NULL\n");
576 return GSTCS_ERROR_INVALID_PARAMETER;
579 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
580 src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
581 gstcs_debug("buffer size (src): %d", src_size);
583 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
584 unsigned int src_row = input_format->width * byte_per_pixcel;
585 unsigned int stride_row = stride * byte_per_pixcel;
586 unsigned int i = 0, y = 0;
587 gstcs_debug("padding will be inserted to buffer");
588 data = (unsigned char *) malloc(src_size);
590 gstcs_error("app_buffer is NULL\n");
591 return GSTCS_ERROR_INVALID_PARAMETER;
593 for (y = 0; y < (unsigned int)(input_format->height); y++) {
594 guint8 *pLine = (guint8 *) &(src[src_row * y]);
595 for (i = 0; i < src_row; i++)
596 data[y * stride_row + i] = pLine[i];
597 guint8 stride_row_color = pLine[i - 1];
598 for (i = src_row; i < stride_row; i++)
599 data[y * stride_row + i] = stride_row_color;
601 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
602 for (i = 0; i < stride_row; i++)
603 data[y * stride_row + i] = data[(y - 1) * stride_row + i];
605 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
607 if (gst_buf == NULL) {
608 gstcs_error("buffer is NULL\n");
609 return GSTCS_ERROR_INVALID_PARAMETER;
612 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
617 _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)
620 GstStateChangeReturn ret_state;
621 int ret = GSTCS_ERROR_NONE;
624 ret = _mm_create_pipeline(pGstreamer_s);
625 if (ret != GSTCS_ERROR_NONE)
626 gstcs_error("ERROR - mm_create_pipeline ");
628 /* 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. */
629 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
631 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
632 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
633 gst_object_unref(bus);
635 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
636 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
638 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
639 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
640 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
641 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
643 gstcs_debug("Start mm_push_buffer_into_pipeline");
644 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
646 if (ret != GSTCS_ERROR_NONE) {
647 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
648 gst_object_unref(pGstreamer_s->pipeline);
651 gstcs_debug("End mm_push_buffer_into_pipeline");
654 gstcs_debug("Start mm_link_pipeline");
655 _mm_link_pipeline(pGstreamer_s, input_format, output_format, pImgp_info->angle);
656 gstcs_debug("End mm_link_pipeline");
658 /* Conecting to the new-sample signal emited by the appsink*/
659 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
660 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
661 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
663 /* GST_STATE_PLAYING*/
664 gstcs_debug("Start GST_STATE_PLAYING");
665 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
666 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
669 gstcs_debug("g_main_loop_run");
670 g_main_loop_run(pGstreamer_s->loop);
672 gstcs_debug("Sucess GST_STATE_CHANGE");
675 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
676 gstcs_debug("End GST_STATE_NULL");
678 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
680 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
682 if (ret_state == GST_STATE_CHANGE_SUCCESS)
683 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
684 else if (ret_state == GST_STATE_CHANGE_ASYNC)
685 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
687 gstcs_debug("Success gst_element_get_state\n");
689 if (ret_state == GST_STATE_CHANGE_FAILURE) {
690 gstcs_error("GST_STATE_CHANGE_FAILURE");
692 if (pGstreamer_s->output_buffer != NULL) {
693 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
694 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
695 int buffer_size = mapinfo.size;
696 int calc_buffer_size = 0;
697 if (((pImgp_info->dst_width != (unsigned int)(output_format->width)) || (pImgp_info->dst_height != (unsigned int)(output_format->height))) &&
698 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
699 gstcs_debug("calculate image size with stride & elevation");
700 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
702 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
704 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
705 if (buffer_size != calc_buffer_size) {
706 gstcs_debug("Buffer size is different \n");
707 gstcs_debug("unref output buffer");
708 gst_buffer_unref(pGstreamer_s->output_buffer);
709 gst_object_unref(pGstreamer_s->pipeline);
710 pGstreamer_s->output_buffer = NULL;
711 return GSTCS_ERROR_INVALID_OPERATION;
713 gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
714 memcpy(dst, mapinfo.data, buffer_size);
715 pImgp_info->buffer_size = buffer_size;
716 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
718 gstcs_debug("pGstreamer_s->output_buffer is NULL");
721 gstcs_debug("unref output buffer");
722 gst_buffer_unref(pGstreamer_s->output_buffer);
723 gst_object_unref(pGstreamer_s->pipeline);
724 pGstreamer_s->output_buffer = NULL;
726 gstcs_debug("End gstreamer processing");
727 gstcs_debug("dst: %p", dst);
733 mm_setup_image_size(const char* _format_label, int width, int height)
737 if (strcmp(_format_label, "I420") == 0) {
738 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; */
739 } else if (strcmp(_format_label, "Y42B") == 0) {
740 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
741 } else if (strcmp(_format_label, "YUV422") == 0) {
742 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
743 } else if (strcmp(_format_label, "Y444") == 0) {
744 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
745 } else if (strcmp(_format_label, "YV12") == 0) {
746 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; */
747 } else if (strcmp(_format_label, "NV12") == 0) {
748 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
749 } else if (strcmp(_format_label, "RGB565") == 0) {
750 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
751 } else if (strcmp(_format_label, "RGB888") == 0) {
752 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
753 } else if (strcmp(_format_label, "BGR888") == 0) {
754 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
755 } else if (strcmp(_format_label, "UYVY") == 0) {
756 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
757 } else if (strcmp(_format_label, "YUYV") == 0) {
758 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
759 } else if (strcmp(_format_label, "ARGB8888") == 0) {
760 size = width * height *4;
761 } else if (strcmp(_format_label, "BGRA8888") == 0) {
762 size = width * height *4;
763 } else if (strcmp(_format_label, "RGBA8888") == 0) {
764 size = width * height *4;
765 } else if (strcmp(_format_label, "ABGR8888") == 0) {
766 size = width * height *4;
767 } else if (strcmp(_format_label, "BGRX") == 0) {
768 size = width * height *4;
771 gstcs_debug("file_size: %d\n", size);
776 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
778 if (gstreamer == NULL) {
779 gstcs_error("ERROR - gstreamer is null ");
780 return GSTCS_ERROR_INVALID_OPERATION;
783 gstreamer->context = g_main_context_new();
784 if (gstreamer->context == NULL) {
785 gstcs_error("ERROR - g_main_context_new ");
786 return GSTCS_ERROR_INVALID_OPERATION;
788 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
789 if (gstreamer->loop == NULL) {
790 gstcs_error("ERROR - g_main_loop_new ");
791 g_main_context_unref(gstreamer->context);
792 return GSTCS_ERROR_INVALID_OPERATION;
795 g_main_context_push_thread_default(gstreamer->context);
796 return GSTCS_ERROR_NONE;
799 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
801 if (gstreamer == NULL) {
802 gstcs_error("ERROR - gstreamer is null ");
803 return GSTCS_ERROR_INVALID_OPERATION;
806 if (gstreamer->loop != NULL)
807 g_main_loop_unref(gstreamer->loop);
809 if (gstreamer->context != NULL)
810 g_main_context_unref(gstreamer->context);
812 return GSTCS_ERROR_NONE;
815 static int _gstcs_init(gstreamer_s** gstreamer)
817 static const int max_argc = 50;
821 int ret = GSTCS_ERROR_NONE;
823 argv = malloc(sizeof(gchar*) * max_argc);
826 gstcs_error("argv is not allocated");
827 return GSTCS_ERROR_OUT_OF_MEMORY;
829 memset(argv, 0, sizeof(gchar*) * max_argc);
831 argv[argc] = g_strdup("mmutil_gstcs");
832 if (argv[argc] == NULL) {
833 gstcs_error("argv[%d] is not allocated", argc);
834 ret = GSTCS_ERROR_OUT_OF_MEMORY;
837 /* check disable registry scan */
838 argv[argc] = g_strdup("--gst-disable-registry-update");
839 if (argv[argc] == NULL) {
840 gstcs_error("argv[%d] is not allocated", argc);
841 ret = GSTCS_ERROR_OUT_OF_MEMORY;
844 if (ret != GSTCS_ERROR_NONE) {
845 for (i = 0; i < argc; i++)
852 gst_init(&argc, &argv);
854 *gstreamer = g_new0(gstreamer_s, 1);
855 if (*gstreamer == NULL) {
856 gstcs_error("gstreamer structure is not allocated");
857 ret = GSTCS_ERROR_OUT_OF_MEMORY;
860 for (i = 0; i < argc; i++)
867 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
869 image_format_s* input_format = NULL, *output_format = NULL;
870 gstreamer_s* pGstreamer_s;
871 int ret = GSTCS_ERROR_NONE;
873 /* Print debug message for inout structure */
874 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
875 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);
877 /* Initialize gstreamer */
878 ret = _gstcs_init(&pGstreamer_s);
879 if (ret != GSTCS_ERROR_NONE) {
880 gstcs_error("Error: _gstcs_new is failed");
884 /* Create input/output format for gstreamer processing */
885 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
886 if (input_format == NULL) {
887 gstcs_error("Error: memory allocation failed");
888 GSTCS_FREE(pGstreamer_s);
889 return GSTCS_ERROR_OUT_OF_MEMORY;
892 output_format = _mm_set_output_image_format_s_struct(pImgp_info, input_format);
893 if (output_format == NULL) {
894 gstcs_error("Error: memory allocation failed");
895 _gstcs_destroy_image_format(input_format);
896 GSTCS_FREE(pGstreamer_s);
897 return GSTCS_ERROR_OUT_OF_MEMORY;
900 /* Create default thread for async behavior */
901 ret = _gstcs_create_default_thread(pGstreamer_s);
902 if (ret != GSTCS_ERROR_NONE) {
903 gstcs_error("Error: _gstcs_create_default_thread is failed");
904 _gstcs_destroy_image_format(input_format);
905 _gstcs_destroy_image_format(output_format);
906 GSTCS_FREE(pGstreamer_s);
910 /* Do gstreamer processing */
911 gstcs_debug("Start _mm_imgp_gstcs_processing ");
912 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 */
914 if (ret == GSTCS_ERROR_NONE)
915 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
916 else if (ret != GSTCS_ERROR_NONE)
917 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
920 ret = _gstcs_destroy_default_thread(pGstreamer_s);
921 if (ret != GSTCS_ERROR_NONE)
922 gstcs_error("Error: _gstcs_create_default_thread is failed");
924 _gstcs_destroy_image_format(input_format);
925 _gstcs_destroy_image_format(output_format);
926 GSTCS_FREE(pGstreamer_s);
931 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
933 if (pImgp_info == NULL) {
934 gstcs_error("Error: input vaule is NULL");
935 return GSTCS_ERROR_INVALID_PARAMETER;
938 if (src == NULL || dst == NULL) {
939 gstcs_error("Error: src | dst is NULL");
940 return GSTCS_ERROR_INVALID_PARAMETER;
943 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
944 gstcs_error("Error: imgp_type is wrong");
945 return GSTCS_ERROR_INVALID_PARAMETER;
948 gstcs_debug("[src %p] [dst %p]", src, dst);
950 return _mm_imgp_gstcs(pImgp_info, src, dst);