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"
23 #include <gst/check/gstcheck.h>
24 #include <gst/video/video-format.h>
26 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
27 #define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
28 #define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)
29 #define MM_UTIL_ROUND_UP_16(num) (((num)+15)&~15)
33 _mm_sink_sample(GstElement * appsink, gpointer user_data)
36 GstSample *_sample = NULL;
37 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
38 _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
40 _buf = gst_sample_get_buffer(_sample);
42 pGstreamer_s->output_buffer = _buf;
44 if(pGstreamer_s->output_buffer != NULL) {
45 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
46 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
47 gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
48 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
50 gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
54 gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
55 gst_sample_unref(_sample);
61 _mm_on_src_message(GstBus * bus, GstMessage * message, gpointer user_data)
63 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
64 switch (GST_MESSAGE_TYPE (message)) {
65 case GST_MESSAGE_EOS: {
66 gstcs_debug("The source got dry");
67 gst_app_src_end_of_stream(GST_APP_SRC(pGstreamer_s->appsrc));
68 g_main_context_pop_thread_default(pGstreamer_s->context);
69 g_main_loop_quit (pGstreamer_s->loop);
72 case GST_MESSAGE_ERROR: {
74 gchar *dbg_info = NULL;
76 gst_message_parse_error (message, &err, &dbg_info);
77 gstcs_error ("ERROR from element %s: %s\n", GST_OBJECT_NAME (message->src), err->message);
78 gstcs_error ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
81 g_main_context_pop_thread_default(pGstreamer_s->context);
82 g_main_loop_quit (pGstreamer_s->loop);
83 gstcs_debug("Quit GST_CS\n");
93 _mm_get_byte_per_pixcel(const char *__format_label)
95 int byte_per_pixcel = 1;
96 if (strcmp(__format_label, "RGB565") == 0) {
98 } else if (strcmp(__format_label, "RGB888") == 0 ||
99 strcmp(__format_label, "BGR888") == 0) {
101 } else if (strcmp(__format_label, "RGBA8888") == 0 ||
102 strcmp(__format_label, "ARGB8888") == 0 ||
103 strcmp(__format_label, "BGRA8888") == 0 ||
104 strcmp(__format_label, "ABGR8888") == 0 ||
105 strcmp(__format_label, "RGBX") == 0 ||
106 strcmp(__format_label, "XRGB") == 0 ||
107 strcmp(__format_label, "BGRX") == 0 ||
108 strcmp(__format_label, "XBGR") == 0) {
112 gstcs_debug("byte per pixcel : %d", byte_per_pixcel);
114 return byte_per_pixcel;
118 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
120 int ret = MM_ERROR_NONE;
121 pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
122 if (!pGstreamer_s->pipeline) {
123 gstcs_error("pipeline could not be created. Exiting.\n");
124 ret = MM_ERROR_IMAGE_INVALID_VALUE;
126 pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
127 if (!pGstreamer_s->appsrc) {
128 gstcs_error("appsrc could not be created. Exiting.\n");
129 ret = MM_ERROR_IMAGE_INVALID_VALUE;
131 pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorspace");
132 if (!pGstreamer_s->colorspace) {
133 gstcs_error("colorspace could not be created. Exiting.\n");
134 ret = MM_ERROR_IMAGE_INVALID_VALUE;
136 pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
137 if (!pGstreamer_s->videoscale) {
138 gstcs_error("videoscale could not be created. Exiting.\n");
139 ret = MM_ERROR_IMAGE_INVALID_VALUE;
141 pGstreamer_s->videoflip=gst_element_factory_make("videoflip", "flip");
142 if (!pGstreamer_s->videoflip) {
143 gstcs_error("videoflip could not be created. Exiting.\n");
144 ret = MM_ERROR_IMAGE_INVALID_VALUE;
146 pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
147 if (!pGstreamer_s->appsink) {
148 gstcs_error("appsink could not be created. Exiting.\n");
149 ret = MM_ERROR_IMAGE_INVALID_VALUE;
154 static void _mm_destroy_notify(gpointer data) {
155 unsigned char *_data = (unsigned char *)data;
163 _mm_check_caps_format(GstCaps* caps)
165 GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
166 const gchar* formatInfo = gst_structure_get_string (caps_structure, "format");
167 gstcs_debug("[%d] caps: %s", GST_IS_CAPS(caps), formatInfo);
171 _mm_link_pipeline(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int value)
174 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps); /*g_object_set(pGstreamer_s->appsrc, "caps", input_format->caps, NULL);*/
175 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps); /*g_object_set(pGstreamer_s->appsink, "caps", output_format->caps, NULL); */
176 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);
177 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
178 gstcs_error("Fail to link pipeline");
180 gstcs_debug("Success to link pipeline");
183 g_object_set (G_OBJECT (pGstreamer_s->appsrc), "stream-type", 0, "format", GST_FORMAT_TIME, NULL);
184 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
185 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
187 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 */
189 /*g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);*/
190 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
194 _mm_set_image_input_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
196 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
197 GstVideoFormat videoFormat;
198 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;
200 if(__format == NULL) {
201 gstcs_error("Image format is NULL\n");
205 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
206 memset(_format_name, 0, sizeof(_format_name));
208 if(strcmp(__format->colorspace,"YUV") == 0) {
209 if(strcmp(__format->format_label,"I420") == 0
210 || strcmp(__format->format_label,"Y42B") == 0
211 || strcmp(__format->format_label,"Y444") == 0
212 || strcmp(__format->format_label,"YV12") == 0
213 || strcmp(__format->format_label,"NV12") == 0
214 || strcmp(__format->format_label,"UYVY") == 0) {
215 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
216 }else if(strcmp(__format->format_label,"YUYV") == 0) {
217 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
219 gstcs_debug("Chosen video format: %s", _format_name);
220 __format->caps = gst_caps_new_simple ("video/x-raw",
221 "format", G_TYPE_STRING, _format_name,
222 "framerate", GST_TYPE_FRACTION, 25, 1,
223 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
224 "width", G_TYPE_INT, __format->width,
225 "height", G_TYPE_INT, __format->height,
226 "framerate", GST_TYPE_FRACTION, 1, 1,
230 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
231 if(strcmp(__format->format_label,"RGB888") == 0) {
232 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
233 }else if(strcmp(__format->format_label,"BGR888") == 0) {
234 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
235 }else if(strcmp(__format->format_label,"RGB565") == 0) {
236 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
237 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
238 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
241 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
242 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
244 __format->caps = gst_caps_new_simple ("video/x-raw",
245 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
246 "width", G_TYPE_INT, __format->stride,
247 "height", G_TYPE_INT, __format->elevation,
248 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
251 else if(strcmp(__format->colorspace,"RGBA") ==0) {
252 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
253 gstcs_debug("ARGB8888");
254 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
255 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
256 gstcs_debug("BGRA8888");
257 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
258 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
259 gstcs_debug("RGBA8888");
260 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
261 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
262 gstcs_debug("ABGR8888");
263 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
265 gstcs_error("***Wrong format cs type***\n");
268 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
269 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
271 __format->caps = gst_caps_new_simple ("video/x-raw",
272 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
273 "width", G_TYPE_INT, __format->width,
274 "height", G_TYPE_INT, __format->elevation,
275 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
279 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
280 _mm_check_caps_format(__format->caps);
282 gstcs_error("__format->caps is NULL");
287 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
289 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
290 GstVideoFormat videoFormat;
291 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;
293 if(__format == NULL) {
294 gstcs_error("Image format is NULL\n");
298 gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
299 memset(_format_name, 0, sizeof(_format_name));
301 if(strcmp(__format->colorspace,"YUV") == 0) {
302 if(strcmp(__format->format_label,"I420") == 0
303 || strcmp(__format->format_label,"Y42B") == 0
304 || strcmp(__format->format_label,"Y444") == 0
305 || strcmp(__format->format_label,"YV12") == 0
306 || strcmp(__format->format_label,"NV12") == 0
307 || strcmp(__format->format_label,"UYVY") == 0) {
308 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
309 }else if(strcmp(__format->format_label,"YUYV") == 0) {
310 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
312 gstcs_debug("Chosen video format: %s", _format_name);
313 __format->caps = gst_caps_new_simple ("video/x-raw",
314 "format", G_TYPE_STRING, _format_name,
315 "width", G_TYPE_INT, __format->width,
316 "height", G_TYPE_INT, __format->height,
317 "framerate", GST_TYPE_FRACTION, 1, 1,
321 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
322 if(strcmp(__format->format_label,"RGB888") == 0) {
323 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
324 }else if(strcmp(__format->format_label,"BGR888") == 0) {
325 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
326 }else if(strcmp(__format->format_label,"RGB565") == 0) {
327 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
328 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
329 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
332 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
333 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
335 __format->caps = gst_caps_new_simple ("video/x-raw",
336 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
337 "width", G_TYPE_INT, __format->width,
338 "height", G_TYPE_INT, __format->height,
339 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
342 else if(strcmp(__format->colorspace,"RGBA") ==0) {
343 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
344 gstcs_debug("ARGB8888");
345 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
346 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
347 gstcs_debug("BGRA8888");
348 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
349 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
350 gstcs_debug("RGBA8888");
351 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
352 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
353 gstcs_debug("ABGR8888");
354 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
356 gstcs_error("***Wrong format cs type***\n");
359 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
360 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
362 __format->caps = gst_caps_new_simple ("video/x-raw",
363 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
364 "width", G_TYPE_INT, __format->width,
365 "height", G_TYPE_INT, __format->height,
366 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
370 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
371 _mm_check_caps_format(__format->caps);
373 gstcs_error("__format->caps is NULL");
378 _mm_set_image_colorspace( image_format_s* __format)
380 gstcs_debug("format_label: %s\n", __format->format_label);
382 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
383 if (__format->colorspace == NULL) {
384 gstcs_error("memory allocation failed");
387 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
388 if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
389 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
390 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
391 }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
392 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
393 }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)) {
394 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
395 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
396 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
398 gstcs_error("Check your colorspace format label");
399 GSTCS_FREE(__format->colorspace);
404 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
406 if(strcmp(pFormat->colorspace,"YUV") == 0) {
407 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
408 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
409 } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
410 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
411 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
412 } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
413 pFormat->stride = pFormat->width;
414 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
416 gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
419 static image_format_s*
420 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
422 image_format_s* __format = NULL;
424 __format=(image_format_s*)malloc(sizeof(image_format_s));
425 if (__format == NULL) {
426 gstcs_error("memory allocation failed");
429 memset(__format, 0, sizeof(image_format_s));
431 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
432 if (__format->format_label == NULL) {
433 gstcs_error("memory allocation failed");
434 GSTCS_FREE(__format);
437 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
438 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
439 gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
440 _mm_set_image_colorspace(__format);
442 __format->width=pImgp_info->src_width;
443 __format->height=pImgp_info->src_height;
444 _mm_round_up_input_image_widh_height(__format);
446 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
447 gstcs_debug("blocksize: %d\n", __format->blocksize);
448 _mm_set_image_input_format_s_capabilities(__format);
454 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
456 if(strcmp(pFormat->colorspace,"YUV") == 0) {
457 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
458 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
459 } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
460 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
461 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
462 pFormat->width = pFormat->stride;
463 if (input_format->height != input_format->elevation)
464 pFormat->height = pFormat->elevation;
465 } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
466 pFormat->stride = pFormat->width;
467 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
468 if (input_format->height != input_format->elevation)
469 pFormat->height = pFormat->elevation;
471 gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
474 static image_format_s*
475 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
477 image_format_s* __format = NULL;
479 __format=(image_format_s*)malloc(sizeof(image_format_s));
480 if (__format == NULL) {
481 gstcs_error("memory allocation failed");
484 memset(__format, 0, sizeof(image_format_s));
486 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
487 if (__format->format_label == NULL) {
488 gstcs_error("memory allocation failed");
489 GSTCS_FREE(__format);
492 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
493 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
494 _mm_set_image_colorspace(__format);
496 __format->width=pImgp_info->dst_width;
497 __format->height=pImgp_info->dst_height;
498 _mm_round_up_output_image_widh_height(__format, input_format);
500 pImgp_info->output_stride = __format->stride;
501 pImgp_info->output_elevation = __format->elevation;
503 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
504 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
505 _mm_set_image_output_format_s_capabilities(__format);
510 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
512 int ret = MM_ERROR_NONE;
514 if(pGstreamer_s->pipeline == NULL) {
515 gstcs_error("pipeline is NULL\n");
516 return MM_ERROR_IMAGE_INVALID_VALUE;
519 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
520 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
523 gstcs_error("buffer is NULL\n");
524 return MM_ERROR_IMAGE_INVALID_VALUE;
527 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
532 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
534 int ret = MM_ERROR_NONE;
535 GstBuffer *gst_buf = NULL;
536 unsigned int src_size = 0;
537 unsigned char *data = NULL;
538 unsigned int stride = input_format->stride;
539 unsigned int elevation = input_format->elevation;
541 if(pGstreamer_s->pipeline == NULL) {
542 gstcs_error("pipeline is NULL\n");
543 return MM_ERROR_IMAGE_INVALID_VALUE;
546 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
547 src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
548 gstcs_debug("buffer size (src): %d", src_size);
550 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
551 unsigned int src_row = input_format->width * byte_per_pixcel;
552 unsigned int stride_row = stride * byte_per_pixcel;
553 unsigned int i = 0, y = 0;
554 gstcs_debug("padding will be inserted to buffer");
555 data =(unsigned char *) g_malloc (src_size);
557 gstcs_error("app_buffer is NULL\n");
558 return MM_ERROR_IMAGE_INVALID_VALUE;
560 for (y = 0; y < (unsigned int)(input_format->height); y++) {
561 guint8 *pLine = (guint8 *) &(src[src_row * y]);
562 for(i = 0; i < src_row; i++) {
563 data[y * stride_row + i] = pLine[i];
565 for(i = src_row; i < stride_row; i++) {
566 data[y * stride_row + i] = 0x00;
569 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
570 for(i = 0; i < stride_row; i++) {
571 data[y * stride_row + i] = 0x00;
574 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
577 gstcs_error("buffer is NULL\n");
578 return MM_ERROR_IMAGE_INVALID_VALUE;
581 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
586 _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)
589 GstStateChangeReturn ret_state;
590 int ret = MM_ERROR_NONE;
592 if(src== NULL || dst == NULL) {
593 gstcs_error("src || dst is NULL");
594 return MM_ERROR_IMAGE_INVALID_VALUE;
598 ret = _mm_create_pipeline(pGstreamer_s);
599 if(ret != MM_ERROR_NONE) {
600 gstcs_error("ERROR - mm_create_pipeline ");
603 pGstreamer_s->context = g_main_context_new();
604 if (pGstreamer_s->context == NULL) {
605 gstcs_error("ERROR - g_main_context_new ");
606 gst_object_unref (pGstreamer_s->pipeline);
607 g_free (pGstreamer_s);
608 return MM_ERROR_IMAGE_INTERNAL;
610 pGstreamer_s->loop = g_main_loop_new (pGstreamer_s->context, FALSE);
611 if (pGstreamer_s->loop == NULL) {
612 gstcs_error("ERROR - g_main_loop_new ");
613 gst_object_unref (pGstreamer_s->pipeline);
614 g_main_context_unref(pGstreamer_s->context);
615 g_free (pGstreamer_s);
616 return MM_ERROR_IMAGE_INTERNAL;
619 g_main_context_push_thread_default(pGstreamer_s->context);
621 /* 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. */
622 gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
624 bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline));
625 gst_bus_add_watch (bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
626 gst_object_unref(bus);
628 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
629 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
630 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
631 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
633 gstcs_debug("Start mm_push_buffer_into_pipeline");
634 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
636 if(ret != MM_ERROR_NONE) {
637 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
638 gst_object_unref (pGstreamer_s->pipeline);
639 g_main_context_unref(pGstreamer_s->context);
640 g_main_loop_unref(pGstreamer_s->loop);
641 g_free (pGstreamer_s);
644 gstcs_debug("End mm_push_buffer_into_pipeline");
647 gstcs_debug("Start mm_link_pipeline");
648 _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
649 gstcs_debug("End mm_link_pipeline");
651 /* Conecting to the new-sample signal emited by the appsink*/
652 gstcs_debug("Start G_CALLBACK (_mm_sink_sample)");
653 g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_sample), pGstreamer_s);
654 gstcs_debug("End G_CALLBACK (_mm_sink_sample)");
656 /* GST_STATE_PLAYING*/
657 gstcs_debug("Start GST_STATE_PLAYING");
658 ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
659 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
662 gstcs_debug("g_main_loop_run");
663 g_main_loop_run (pGstreamer_s->loop);
665 gstcs_debug("Sucess GST_STATE_CHANGE");
668 gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
669 gstcs_debug("End GST_STATE_NULL");
671 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
673 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
675 if(ret_state == GST_STATE_CHANGE_SUCCESS) {
676 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
677 }else if( ret_state == GST_STATE_CHANGE_ASYNC) {
678 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
681 gstcs_debug("Success gst_element_get_state\n");
683 if (ret_state == GST_STATE_CHANGE_FAILURE) {
684 gstcs_error("GST_STATE_CHANGE_FAILURE");
686 if(pGstreamer_s->output_buffer != NULL) {
687 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
688 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
689 int buffer_size = mapinfo.size;
690 int calc_buffer_size = 0;
691 if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
692 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
693 gstcs_debug("calculate image size with stride & elevation");
694 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
696 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
698 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
699 if( buffer_size != calc_buffer_size) {
700 gstcs_debug("Buffer size is different \n");
701 gstcs_debug("unref output buffer");
702 gst_buffer_unref (pGstreamer_s->output_buffer);
703 gst_object_unref (pGstreamer_s->pipeline);
704 pGstreamer_s->output_buffer = NULL;
705 g_main_context_unref(pGstreamer_s->context);
706 g_main_loop_unref(pGstreamer_s->loop);
707 g_free (pGstreamer_s);
708 return MM_ERROR_IMAGE_INTERNAL;
710 gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
711 memcpy(dst, mapinfo.data, buffer_size);
712 pImgp_info->buffer_size = buffer_size;
713 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
715 gstcs_debug("pGstreamer_s->output_buffer is NULL");
718 gstcs_debug("unref output buffer");
719 gst_buffer_unref (pGstreamer_s->output_buffer);
720 gst_object_unref (pGstreamer_s->pipeline);
721 pGstreamer_s->output_buffer = NULL;
722 g_main_context_unref(pGstreamer_s->context);
723 g_main_loop_unref(pGstreamer_s->loop);
724 g_free (pGstreamer_s);
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);
777 _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
779 image_format_s* input_format=NULL, *output_format=NULL;
780 gstreamer_s* pGstreamer_s;
781 int ret = MM_ERROR_NONE;
782 static const int max_argc = 50;
787 if(pImgp_info == NULL) {
788 gstcs_error("imgp_info_s is NULL");
789 return MM_ERROR_IMAGE_INVALID_VALUE;
792 if(src== NULL || dst == NULL) {
793 gstcs_error("src || dst is NULL");
794 return MM_ERROR_IMAGE_INVALID_VALUE;
796 gstcs_debug("[src %p] [dst %p]", src, dst);
798 argc = malloc(sizeof(int));
799 argv = malloc(sizeof(gchar*) * max_argc);
801 if (!argc || !argv) {
802 gstcs_error("argc ||argv is NULL");
803 GSTCS_FREE(input_format);
804 GSTCS_FREE(output_format);
811 return MM_ERROR_IMAGE_INVALID_VALUE;
813 memset(argv, 0, sizeof(gchar*) * max_argc);
814 gstcs_debug("memset argv");
818 argv[0] = g_strdup( "mmutil_gstcs" );
819 /* check disable registry scan */
820 argv[*argc] = g_strdup("--gst-disable-registry-update");
822 gstcs_debug("--gst-disable-registry-update");
824 gst_init(argc, &argv);
826 pGstreamer_s = g_new0 (gstreamer_s, 1);
828 for (i = 0; i < (*argc); i++) {
834 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
835 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);
837 input_format= _mm_set_input_image_format_s_struct(pImgp_info);
838 if (input_format == NULL) {
839 gstcs_error("memory allocation failed");
840 return MM_ERROR_IMAGE_NO_FREE_SPACE;
842 output_format= _mm_set_output_image_format_s_struct(pImgp_info, input_format);
843 if (output_format == NULL) {
844 gstcs_error("memory allocation failed");
845 GSTCS_FREE(input_format->format_label);
846 GSTCS_FREE(input_format->colorspace);
847 GSTCS_FREE(input_format);
848 return MM_ERROR_IMAGE_NO_FREE_SPACE;
851 /* _format_label : I420, RGB888 etc*/
852 gstcs_debug("Start _mm_imgp_gstcs_processing ");
853 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 */
855 if(ret == MM_ERROR_NONE) {
856 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
857 }else if (ret != MM_ERROR_NONE) {
858 gstcs_error("ERROR - _mm_imgp_gstcs_processing");
861 GSTCS_FREE(input_format->format_label);
862 GSTCS_FREE(input_format->colorspace);
863 GSTCS_FREE(input_format);
865 GSTCS_FREE(output_format->format_label);
866 GSTCS_FREE(output_format->colorspace);
867 GSTCS_FREE(output_format);
873 mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
875 if (pImgp_info == NULL) {
876 gstcs_error("input vaule is error");
878 return _mm_imgp_gstcs(pImgp_info, src, dst);