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");
180 g_object_set(G_OBJECT(pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
181 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
182 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
184 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 */
186 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
187 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
191 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
193 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
194 GstVideoFormat videoFormat;
203 if (__format == NULL) {
204 gstcs_error("Image format is NULL\n");
208 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
209 memset(_format_name, 0, sizeof(_format_name));
211 if (strcmp(__format->colorspace, "YUV") == 0) {
212 if (strcmp(__format->format_label, "I420") == 0
213 || strcmp(__format->format_label, "Y42B") == 0
214 || strcmp(__format->format_label, "Y444") == 0
215 || strcmp(__format->format_label, "YV12") == 0
216 || strcmp(__format->format_label, "NV12") == 0
217 || strcmp(__format->format_label, "UYVY") == 0) {
218 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
219 } else if (strcmp(__format->format_label, "YUYV") == 0) {
220 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
222 gstcs_debug("Chosen video format: %s", _format_name);
223 __format->caps = gst_caps_new_simple("video/x-raw",
224 "format", G_TYPE_STRING, _format_name,
225 "framerate", GST_TYPE_FRACTION, 25, 1,
226 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
227 "width", G_TYPE_INT, __format->width,
228 "height", G_TYPE_INT, __format->height,
229 "framerate", GST_TYPE_FRACTION, 1, 1,
233 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
234 if (strcmp(__format->format_label, "RGB888") == 0) {
235 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
236 } else if (strcmp(__format->format_label, "BGR888") == 0) {
237 _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
238 } else if (strcmp(__format->format_label, "RGB565") == 0) {
239 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
240 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
241 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
244 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
245 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
247 __format->caps = gst_caps_new_simple("video/x-raw",
248 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
249 "width", G_TYPE_INT, __format->stride,
250 "height", G_TYPE_INT, __format->elevation,
251 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
254 else if (strcmp(__format->colorspace, "RGBA") == 0) {
255 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
256 gstcs_debug("ARGB8888");
257 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
258 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
259 gstcs_debug("BGRA8888");
260 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
261 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
262 gstcs_debug("RGBA8888");
263 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
264 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
265 gstcs_debug("ABGR8888");
266 _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
268 gstcs_error("***Wrong format cs type***\n");
271 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
272 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
274 __format->caps = gst_caps_new_simple("video/x-raw",
275 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
276 "width", G_TYPE_INT, __format->width,
277 "height", G_TYPE_INT, __format->elevation,
278 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
281 if (__format->caps) {
282 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
283 _mm_check_caps_format(__format->caps);
285 gstcs_error("__format->caps is NULL");
290 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
292 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
293 GstVideoFormat videoFormat;
294 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;
296 if (__format == NULL) {
297 gstcs_error("Image format is NULL\n");
301 gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
302 memset(_format_name, 0, sizeof(_format_name));
304 if (strcmp(__format->colorspace, "YUV") == 0) {
305 if (strcmp(__format->format_label, "I420") == 0
306 || strcmp(__format->format_label, "Y42B") == 0
307 || strcmp(__format->format_label, "Y444") == 0
308 || strcmp(__format->format_label, "YV12") == 0
309 || strcmp(__format->format_label, "NV12") == 0
310 || strcmp(__format->format_label, "UYVY") == 0) {
311 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
312 } else if (strcmp(__format->format_label, "YUYV") == 0) {
313 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
315 gstcs_debug("Chosen video format: %s", _format_name);
316 __format->caps = gst_caps_new_simple("video/x-raw",
317 "format", G_TYPE_STRING, _format_name,
318 "width", G_TYPE_INT, __format->width,
319 "height", G_TYPE_INT, __format->height,
320 "framerate", GST_TYPE_FRACTION, 1, 1,
324 else if (strcmp(__format->colorspace, "RGB") == 0 || strcmp(__format->colorspace, "BGRX") == 0) {
325 if (strcmp(__format->format_label, "RGB888") == 0) {
326 _bpp = 24; _depth = 24; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _endianness = 4321;
327 } else if (strcmp(__format->format_label, "BGR888") == 0) {
328 _bpp = 24; _depth = 24; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _endianness = 4321;
329 } else if (strcmp(__format->format_label, "RGB565") == 0) {
330 _bpp = 16; _depth = 16; _red_mask = 63488; _green_mask = 2016; _blue_mask = 31; _endianness = 1234;
331 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
332 _bpp = 32; _depth = 24; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _endianness = 4321;
335 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
336 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
338 __format->caps = gst_caps_new_simple("video/x-raw",
339 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
340 "width", G_TYPE_INT, __format->width,
341 "height", G_TYPE_INT, __format->height,
342 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
345 else if (strcmp(__format->colorspace, "RGBA") == 0) {
346 if (strcmp(__format->format_label, "ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
347 gstcs_debug("ARGB8888");
348 _bpp = 32; _depth = 32; _red_mask = 16711680; _green_mask = 65280; _blue_mask = 255; _alpha_mask = -16777216; _endianness = 4321;
349 } else if (strcmp(__format->format_label, "BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
350 gstcs_debug("BGRA8888");
351 _bpp = 32; _depth = 32; _red_mask = 65280; _green_mask = 16711680; _blue_mask = -16777216; _alpha_mask = 255; _endianness = 4321;
352 } else if (strcmp(__format->format_label, "RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
353 gstcs_debug("RGBA8888");
354 _bpp = 32; _depth = 32; _red_mask = -16777216; _green_mask = 16711680; _blue_mask = 65280; _alpha_mask = 255; _endianness = 4321;
355 } else if (strcmp(__format->format_label, "ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
356 gstcs_debug("ABGR8888");
357 _bpp = 32; _depth = 32; _red_mask = 255; _green_mask = 65280; _blue_mask = 16711680; _alpha_mask = -16777216; _endianness = 4321;
359 gstcs_error("***Wrong format cs type***\n");
362 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
363 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
365 __format->caps = gst_caps_new_simple("video/x-raw",
366 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
367 "width", G_TYPE_INT, __format->width,
368 "height", G_TYPE_INT, __format->height,
369 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
372 if (__format->caps) {
373 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
374 _mm_check_caps_format(__format->caps);
376 gstcs_error("__format->caps is NULL");
381 _mm_set_image_colorspace(image_format_s* __format)
383 gstcs_debug("format_label: %s\n", __format->format_label);
385 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
386 if (__format->colorspace == NULL) {
387 gstcs_error("memory allocation failed");
390 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
391 if ((strcmp(__format->format_label, "I420") == 0) || (strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
392 || (strcmp(__format->format_label, "YV12") == 0) || (strcmp(__format->format_label, "NV12") == 0) || (strcmp(__format->format_label, "UYVY") == 0) || (strcmp(__format->format_label, "YUYV") == 0)) {
393 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
394 } else if ((strcmp(__format->format_label, "RGB888") == 0) || (strcmp(__format->format_label, "BGR888") == 0) || (strcmp(__format->format_label, "RGB565") == 0)) {
395 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
396 } 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)) {
397 strncpy(__format->colorspace, "RGBA", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
398 } else if ((strcmp(__format->format_label, "BGRX") == 0)) {
399 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
401 gstcs_error("Check your colorspace format label");
402 GSTCS_FREE(__format->colorspace);
406 static int _gstcs_create_image_format(image_format_s **format)
408 int ret = GSTCS_ERROR_NONE;
409 image_format_s *_format = NULL;
411 if (format == NULL) {
412 gstcs_error("format is wrong value");
413 return GSTCS_ERROR_INVALID_OPERATION;
416 _format = (image_format_s*)malloc(sizeof(image_format_s));
417 if (_format == NULL) {
418 gstcs_error("memory allocation failed");
419 return GSTCS_ERROR_OUT_OF_MEMORY;
421 memset(_format, 0, sizeof(image_format_s));
427 static void _gstcs_destroy_image_format(image_format_s *format)
429 if (format != NULL) {
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 *) g_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];
598 for (i = src_row; i < stride_row; i++) {
599 data[y * stride_row + i] = 0x00;
602 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
603 for (i = 0; i < stride_row; i++) {
604 data[y * stride_row + i] = 0x00;
607 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
609 if (gst_buf == NULL) {
610 gstcs_error("buffer is NULL\n");
611 return GSTCS_ERROR_INVALID_PARAMETER;
614 gst_app_src_push_buffer(GST_APP_SRC(pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
619 _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)
622 GstStateChangeReturn ret_state;
623 int ret = GSTCS_ERROR_NONE;
626 ret = _mm_create_pipeline(pGstreamer_s);
627 if (ret != GSTCS_ERROR_NONE) {
628 gstcs_error("ERROR - mm_create_pipeline ");
631 /* 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. */
632 gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
634 bus = gst_pipeline_get_bus(GST_PIPELINE(pGstreamer_s->pipeline));
635 gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
636 gst_object_unref(bus);
638 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
639 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
641 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
642 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
643 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
644 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
646 gstcs_debug("Start mm_push_buffer_into_pipeline");
647 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
649 if (ret != GSTCS_ERROR_NONE) {
650 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
651 gst_object_unref(pGstreamer_s->pipeline);
654 gstcs_debug("End mm_push_buffer_into_pipeline");
657 gstcs_debug("Start mm_link_pipeline");
658 _mm_link_pipeline(pGstreamer_s, input_format, output_format, pImgp_info->angle);
659 gstcs_debug("End mm_link_pipeline");
661 /* Conecting to the new-sample signal emited by the appsink*/
662 gstcs_debug("Start G_CALLBACK(_mm_sink_sample)");
663 g_signal_connect(pGstreamer_s->appsink, "new-sample", G_CALLBACK(_mm_sink_sample), pGstreamer_s);
664 gstcs_debug("End G_CALLBACK(_mm_sink_sample)");
666 /* GST_STATE_PLAYING*/
667 gstcs_debug("Start GST_STATE_PLAYING");
668 ret_state = gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_PLAYING);
669 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
672 gstcs_debug("g_main_loop_run");
673 g_main_loop_run(pGstreamer_s->loop);
675 gstcs_debug("Sucess GST_STATE_CHANGE");
678 gst_element_set_state(pGstreamer_s->pipeline, GST_STATE_NULL);
679 gstcs_debug("End GST_STATE_NULL");
681 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
683 ret_state = gst_element_get_state(pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
685 if (ret_state == GST_STATE_CHANGE_SUCCESS) {
686 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
687 } else if (ret_state == GST_STATE_CHANGE_ASYNC) {
688 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
691 gstcs_debug("Success gst_element_get_state\n");
693 if (ret_state == GST_STATE_CHANGE_FAILURE) {
694 gstcs_error("GST_STATE_CHANGE_FAILURE");
696 if (pGstreamer_s->output_buffer != NULL) {
697 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
698 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
699 int buffer_size = mapinfo.size;
700 int calc_buffer_size = 0;
701 if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
702 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
703 gstcs_debug("calculate image size with stride & elevation");
704 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
706 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
708 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
709 if (buffer_size != calc_buffer_size) {
710 gstcs_debug("Buffer size is different \n");
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;
715 return GSTCS_ERROR_INVALID_OPERATION;
717 gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
718 memcpy(dst, mapinfo.data, buffer_size);
719 pImgp_info->buffer_size = buffer_size;
720 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
722 gstcs_debug("pGstreamer_s->output_buffer is NULL");
725 gstcs_debug("unref output buffer");
726 gst_buffer_unref(pGstreamer_s->output_buffer);
727 gst_object_unref(pGstreamer_s->pipeline);
728 pGstreamer_s->output_buffer = NULL;
730 gstcs_debug("End gstreamer processing");
731 gstcs_debug("dst: %p", dst);
737 mm_setup_image_size(const char* _format_label, int width, int height)
741 if (strcmp(_format_label, "I420") == 0) {
742 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; */
743 } else if (strcmp(_format_label, "Y42B") == 0) {
744 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
745 } else if (strcmp(_format_label, "YUV422") == 0) {
746 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
747 } else if (strcmp(_format_label, "Y444") == 0) {
748 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
749 } else if (strcmp(_format_label, "YV12") == 0) {
750 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; */
751 } else if (strcmp(_format_label, "NV12") == 0) {
752 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
753 } else if (strcmp(_format_label, "RGB565") == 0) {
754 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
755 } else if (strcmp(_format_label, "RGB888") == 0) {
756 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
757 } else if (strcmp(_format_label, "BGR888") == 0) {
758 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
759 } else if (strcmp(_format_label, "UYVY") == 0) {
760 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
761 } else if (strcmp(_format_label, "YUYV") == 0) {
762 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
763 } else if (strcmp(_format_label, "ARGB8888") == 0) {
764 size = width * height *4;
765 } else if (strcmp(_format_label, "BGRA8888") == 0) {
766 size = width * height *4;
767 } else if (strcmp(_format_label, "RGBA8888") == 0) {
768 size = width * height *4;
769 } else if (strcmp(_format_label, "ABGR8888") == 0) {
770 size = width * height *4;
771 } else if (strcmp(_format_label, "BGRX") == 0) {
772 size = width * height *4;
775 gstcs_debug("file_size: %d\n", size);
780 static int _gstcs_create_default_thread(gstreamer_s *gstreamer)
782 if (gstreamer == NULL) {
783 gstcs_error("ERROR - gstreamer is null ");
784 return GSTCS_ERROR_INVALID_OPERATION;
787 gstreamer->context = g_main_context_new();
788 if (gstreamer->context == NULL) {
789 gstcs_error("ERROR - g_main_context_new ");
790 return GSTCS_ERROR_INVALID_OPERATION;
792 gstreamer->loop = g_main_loop_new(gstreamer->context, FALSE);
793 if (gstreamer->loop == NULL) {
794 gstcs_error("ERROR - g_main_loop_new ");
795 g_main_context_unref(gstreamer->context);
796 return GSTCS_ERROR_INVALID_OPERATION;
799 g_main_context_push_thread_default(gstreamer->context);
800 return GSTCS_ERROR_NONE;
803 static int _gstcs_destroy_default_thread(gstreamer_s *gstreamer)
805 if (gstreamer == NULL) {
806 gstcs_error("ERROR - gstreamer is null ");
807 return GSTCS_ERROR_INVALID_OPERATION;
809 if (gstreamer->loop != NULL) {
810 g_main_loop_unref(gstreamer->loop);
812 if (gstreamer->context != NULL) {
813 g_main_context_unref(gstreamer->context);
815 return GSTCS_ERROR_NONE;
818 static int _gstcs_init(gstreamer_s** gstreamer)
820 static const int max_argc = 50;
824 int ret = GSTCS_ERROR_NONE;
826 argv = malloc(sizeof(gchar*) * max_argc);
829 gstcs_error("argv is not allocated");
830 return GSTCS_ERROR_OUT_OF_MEMORY;
832 memset(argv, 0, sizeof(gchar*) * max_argc);
834 argv[argc] = g_strdup("mmutil_gstcs");
835 if (argv[argc] == NULL) {
836 gstcs_error("argv[%d] is not allocated", argc);
837 ret = GSTCS_ERROR_OUT_OF_MEMORY;
840 /* check disable registry scan */
841 argv[argc] = g_strdup("--gst-disable-registry-update");
842 if (argv[argc] == NULL) {
843 gstcs_error("argv[%d] is not allocated", argc);
844 ret = GSTCS_ERROR_OUT_OF_MEMORY;
847 if (ret != GSTCS_ERROR_NONE) {
848 for (i = 0; i < argc; i++) {
855 gst_init(&argc, &argv);
857 *gstreamer = g_new0(gstreamer_s, 1);
858 if (*gstreamer == NULL) {
859 gstcs_error("gstreamer structure is not allocated");
860 ret = GSTCS_ERROR_OUT_OF_MEMORY;
863 for (i = 0; i < argc; i++) {
870 static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
872 image_format_s* input_format = NULL, *output_format = NULL;
873 gstreamer_s* pGstreamer_s;
874 int ret = GSTCS_ERROR_NONE;
876 /* Print debug message for inout structure */
877 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
878 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);
880 /* Initialize gstreamer */
881 ret = _gstcs_init(&pGstreamer_s);
882 if (ret != GSTCS_ERROR_NONE) {
883 gstcs_error("Error: _gstcs_new is failed");
887 /* Create input/output format for gstreamer processing */
888 input_format = _mm_set_input_image_format_s_struct(pImgp_info);
889 if (input_format == NULL) {
890 gstcs_error("Error: memory allocation failed");
891 GSTCS_FREE(pGstreamer_s);
892 return GSTCS_ERROR_OUT_OF_MEMORY;
895 output_format = _mm_set_output_image_format_s_struct(pImgp_info, input_format);
896 if (output_format == NULL) {
897 gstcs_error("Error: memory allocation failed");
898 _gstcs_destroy_image_format(input_format);
899 GSTCS_FREE(pGstreamer_s);
900 return GSTCS_ERROR_OUT_OF_MEMORY;
903 /* Create default thread for async behavior */
904 ret = _gstcs_create_default_thread(pGstreamer_s);
905 if (ret != GSTCS_ERROR_NONE) {
906 gstcs_error("Error: _gstcs_create_default_thread is failed");
907 _gstcs_destroy_image_format(input_format);
908 _gstcs_destroy_image_format(output_format);
909 GSTCS_FREE(pGstreamer_s);
913 /* Do gstreamer processing */
914 gstcs_debug("Start _mm_imgp_gstcs_processing ");
915 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 */
917 if (ret == GSTCS_ERROR_NONE) {
918 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
919 } else if (ret != GSTCS_ERROR_NONE) {
920 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
924 ret = _gstcs_destroy_default_thread(pGstreamer_s);
925 if (ret != GSTCS_ERROR_NONE) {
926 gstcs_error("Error: _gstcs_create_default_thread is failed");
929 _gstcs_destroy_image_format(input_format);
930 _gstcs_destroy_image_format(output_format);
931 GSTCS_FREE(pGstreamer_s);
936 int mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
938 if (pImgp_info == NULL) {
939 gstcs_error("Error: input vaule is NULL");
940 return GSTCS_ERROR_INVALID_PARAMETER;
943 if (src == NULL || dst == NULL) {
944 gstcs_error("Error: src | dst is NULL");
945 return GSTCS_ERROR_INVALID_PARAMETER;
948 if (_imgp_type < 0 || _imgp_type > IMGP_MAX) {
949 gstcs_error("Error: imgp_type is wrong");
950 return GSTCS_ERROR_INVALID_PARAMETER;
953 gstcs_debug("[src %p] [dst %p]", src, dst);
955 return _mm_imgp_gstcs(pImgp_info, src, dst);