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)
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;
117 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
119 int ret = GSTCS_ERROR_NONE;
120 pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
121 if (!pGstreamer_s->pipeline) {
122 gstcs_error("pipeline could not be created. Exiting.\n");
123 ret = GSTCS_ERROR_INVALID_PARAMETER;
125 pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
126 if (!pGstreamer_s->appsrc) {
127 gstcs_error("appsrc could not be created. Exiting.\n");
128 ret = GSTCS_ERROR_INVALID_PARAMETER;
130 pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorspace");
131 if (!pGstreamer_s->colorspace) {
132 gstcs_error("colorspace could not be created. Exiting.\n");
133 ret = GSTCS_ERROR_INVALID_PARAMETER;
135 pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
136 if (!pGstreamer_s->videoscale) {
137 gstcs_error("videoscale could not be created. Exiting.\n");
138 ret = GSTCS_ERROR_INVALID_PARAMETER;
140 pGstreamer_s->videoflip=gst_element_factory_make("videoflip", "flip");
141 if (!pGstreamer_s->videoflip) {
142 gstcs_error("videoflip could not be created. Exiting.\n");
143 ret = GSTCS_ERROR_INVALID_PARAMETER;
145 pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
146 if (!pGstreamer_s->appsink) {
147 gstcs_error("appsink could not be created. Exiting.\n");
148 ret = GSTCS_ERROR_INVALID_PARAMETER;
153 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;
195 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;
197 if(__format == NULL) {
198 gstcs_error("Image format is NULL\n");
202 gstcs_debug("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
203 memset(_format_name, 0, sizeof(_format_name));
205 if(strcmp(__format->colorspace,"YUV") == 0) {
206 if(strcmp(__format->format_label,"I420") == 0
207 || strcmp(__format->format_label,"Y42B") == 0
208 || strcmp(__format->format_label,"Y444") == 0
209 || strcmp(__format->format_label,"YV12") == 0
210 || strcmp(__format->format_label,"NV12") == 0
211 || strcmp(__format->format_label,"UYVY") == 0) {
212 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
213 }else if(strcmp(__format->format_label,"YUYV") == 0) {
214 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
216 gstcs_debug("Chosen video format: %s", _format_name);
217 __format->caps = gst_caps_new_simple ("video/x-raw",
218 "format", G_TYPE_STRING, _format_name,
219 "framerate", GST_TYPE_FRACTION, 25, 1,
220 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
221 "width", G_TYPE_INT, __format->width,
222 "height", G_TYPE_INT, __format->height,
223 "framerate", GST_TYPE_FRACTION, 1, 1,
227 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
228 if(strcmp(__format->format_label,"RGB888") == 0) {
229 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
230 }else if(strcmp(__format->format_label,"BGR888") == 0) {
231 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
232 }else if(strcmp(__format->format_label,"RGB565") == 0) {
233 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
234 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
235 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
238 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
239 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
241 __format->caps = gst_caps_new_simple ("video/x-raw",
242 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
243 "width", G_TYPE_INT, __format->stride,
244 "height", G_TYPE_INT, __format->elevation,
245 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
248 else if(strcmp(__format->colorspace,"RGBA") ==0) {
249 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
250 gstcs_debug("ARGB8888");
251 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
252 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
253 gstcs_debug("BGRA8888");
254 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
255 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
256 gstcs_debug("RGBA8888");
257 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
258 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
259 gstcs_debug("ABGR8888");
260 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
262 gstcs_error("***Wrong format cs type***\n");
265 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
266 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
268 __format->caps = gst_caps_new_simple ("video/x-raw",
269 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
270 "width", G_TYPE_INT, __format->width,
271 "height", G_TYPE_INT, __format->elevation,
272 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
276 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
277 _mm_check_caps_format(__format->caps);
279 gstcs_error("__format->caps is NULL");
284 _mm_set_image_output_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
286 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
287 GstVideoFormat videoFormat;
288 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;
290 if(__format == NULL) {
291 gstcs_error("Image format is NULL\n");
295 gstcs_debug("colorspace: %s(%d), w: %d, h: %d\n", __format->colorspace, strlen(__format->colorspace), __format->width, __format->height);
296 memset(_format_name, 0, sizeof(_format_name));
298 if(strcmp(__format->colorspace,"YUV") == 0) {
299 if(strcmp(__format->format_label,"I420") == 0
300 || strcmp(__format->format_label,"Y42B") == 0
301 || strcmp(__format->format_label,"Y444") == 0
302 || strcmp(__format->format_label,"YV12") == 0
303 || strcmp(__format->format_label,"NV12") == 0
304 || strcmp(__format->format_label,"UYVY") == 0) {
305 strncpy(_format_name, __format->format_label, sizeof(GST_VIDEO_FORMATS_ALL)-1);
306 }else if(strcmp(__format->format_label,"YUYV") == 0) {
307 strncpy(_format_name, "YVYU", sizeof(GST_VIDEO_FORMATS_ALL)-1);
309 gstcs_debug("Chosen video format: %s", _format_name);
310 __format->caps = gst_caps_new_simple ("video/x-raw",
311 "format", G_TYPE_STRING, _format_name,
312 "width", G_TYPE_INT, __format->width,
313 "height", G_TYPE_INT, __format->height,
314 "framerate", GST_TYPE_FRACTION, 1, 1,
318 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
319 if(strcmp(__format->format_label,"RGB888") == 0) {
320 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
321 }else if(strcmp(__format->format_label,"BGR888") == 0) {
322 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
323 }else if(strcmp(__format->format_label,"RGB565") == 0) {
324 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
325 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
326 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
329 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, 0);
330 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
332 __format->caps = gst_caps_new_simple ("video/x-raw",
333 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
334 "width", G_TYPE_INT, __format->width,
335 "height", G_TYPE_INT, __format->height,
336 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
339 else if(strcmp(__format->colorspace,"RGBA") ==0) {
340 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
341 gstcs_debug("ARGB8888");
342 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
343 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
344 gstcs_debug("BGRA8888");
345 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
346 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
347 gstcs_debug("RGBA8888");
348 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
349 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
350 gstcs_debug("ABGR8888");
351 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
353 gstcs_error("***Wrong format cs type***\n");
356 videoFormat = gst_video_format_from_masks(_depth, _bpp, _endianness, _red_mask, _green_mask, _blue_mask, _alpha_mask);
357 gstcs_debug("Chosen video format: %s", gst_video_format_to_string(videoFormat));
359 __format->caps = gst_caps_new_simple ("video/x-raw",
360 "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
361 "width", G_TYPE_INT, __format->width,
362 "height", G_TYPE_INT, __format->height,
363 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
367 gstcs_debug("###__format->caps is not NULL###, %p", __format->caps);
368 _mm_check_caps_format(__format->caps);
370 gstcs_error("__format->caps is NULL");
375 _mm_set_image_colorspace( image_format_s* __format)
377 gstcs_debug("format_label: %s\n", __format->format_label);
379 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
380 if (__format->colorspace == NULL) {
381 gstcs_error("memory allocation failed");
384 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
385 if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
386 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
387 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
388 }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
389 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
390 }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)) {
391 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
392 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
393 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
395 gstcs_error("Check your colorspace format label");
396 GSTCS_FREE(__format->colorspace);
401 _mm_round_up_input_image_widh_height(image_format_s* pFormat)
403 if(strcmp(pFormat->colorspace,"YUV") == 0) {
404 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
405 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
406 } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
407 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
408 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
409 } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
410 pFormat->stride = pFormat->width;
411 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
413 gstcs_debug("input_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
416 static image_format_s*
417 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
419 image_format_s* __format = NULL;
421 __format=(image_format_s*)malloc(sizeof(image_format_s));
422 if (__format == NULL) {
423 gstcs_error("memory allocation failed");
426 memset(__format, 0, sizeof(image_format_s));
428 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
429 if (__format->format_label == NULL) {
430 gstcs_error("memory allocation failed");
431 GSTCS_FREE(__format);
434 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
435 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
436 gstcs_debug("input_format_label: %s\n", pImgp_info->input_format_label);
437 _mm_set_image_colorspace(__format);
439 __format->width=pImgp_info->src_width;
440 __format->height=pImgp_info->src_height;
441 _mm_round_up_input_image_widh_height(__format);
443 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
444 gstcs_debug("blocksize: %d\n", __format->blocksize);
445 _mm_set_image_input_format_s_capabilities(__format);
451 _mm_round_up_output_image_widh_height(image_format_s* pFormat, const image_format_s *input_format)
453 if(strcmp(pFormat->colorspace,"YUV") == 0) {
454 pFormat->stride = MM_UTIL_ROUND_UP_8(pFormat->width);
455 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
456 } else if(strcmp(pFormat->colorspace, "RGB") == 0) {
457 pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width);
458 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
459 pFormat->width = pFormat->stride;
460 if (input_format->height != input_format->elevation)
461 pFormat->height = pFormat->elevation;
462 } else if (strcmp(pFormat->colorspace,"RGBA") == 0){
463 pFormat->stride = pFormat->width;
464 pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height);
465 if (input_format->height != input_format->elevation)
466 pFormat->height = pFormat->elevation;
468 gstcs_debug("output_format stride: %d, elevation: %d", pFormat->stride, pFormat->elevation);
471 static image_format_s*
472 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info, const image_format_s *input_format)
474 image_format_s* __format = NULL;
476 __format=(image_format_s*)malloc(sizeof(image_format_s));
477 if (__format == NULL) {
478 gstcs_error("memory allocation failed");
481 memset(__format, 0, sizeof(image_format_s));
483 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
484 if (__format->format_label == NULL) {
485 gstcs_error("memory allocation failed");
486 GSTCS_FREE(__format);
489 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
490 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
491 _mm_set_image_colorspace(__format);
493 __format->width=pImgp_info->dst_width;
494 __format->height=pImgp_info->dst_height;
495 _mm_round_up_output_image_widh_height(__format, input_format);
497 pImgp_info->output_stride = __format->stride;
498 pImgp_info->output_elevation = __format->elevation;
500 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
501 gstcs_debug("output_format_label: %s", pImgp_info->output_format_label);
502 _mm_set_image_output_format_s_capabilities(__format);
507 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
509 int ret = GSTCS_ERROR_NONE;
511 if(pGstreamer_s->pipeline == NULL) {
512 gstcs_error("pipeline is NULL\n");
513 return GSTCS_ERROR_INVALID_PARAMETER;
516 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
517 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, src, data_size, 0, data_size, NULL, NULL);
520 gstcs_error("buffer is NULL\n");
521 return GSTCS_ERROR_INVALID_PARAMETER;
524 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
529 _mm_push_buffer_into_pipeline_new(image_format_s *input_format, image_format_s *output_format, unsigned char *src, gstreamer_s * pGstreamer_s)
531 int ret = GSTCS_ERROR_NONE;
532 GstBuffer *gst_buf = NULL;
533 unsigned int src_size = 0;
534 unsigned char *data = NULL;
535 unsigned int stride = input_format->stride;
536 unsigned int elevation = input_format->elevation;
538 if(pGstreamer_s->pipeline == NULL) {
539 gstcs_error("pipeline is NULL\n");
540 return GSTCS_ERROR_INVALID_PARAMETER;
543 gstcs_debug("stride: %d, elevation: %d", stride, elevation);
544 src_size = mm_setup_image_size(input_format->format_label, stride, elevation);
545 gstcs_debug("buffer size (src): %d", src_size);
547 int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->format_label);
548 unsigned int src_row = input_format->width * byte_per_pixcel;
549 unsigned int stride_row = stride * byte_per_pixcel;
550 unsigned int i = 0, y = 0;
551 gstcs_debug("padding will be inserted to buffer");
552 data =(unsigned char *) g_malloc (src_size);
554 gstcs_error("app_buffer is NULL\n");
555 return GSTCS_ERROR_INVALID_PARAMETER;
557 for (y = 0; y < (unsigned int)(input_format->height); y++) {
558 guint8 *pLine = (guint8 *) &(src[src_row * y]);
559 for(i = 0; i < src_row; i++) {
560 data[y * stride_row + i] = pLine[i];
562 for(i = src_row; i < stride_row; i++) {
563 data[y * stride_row + i] = 0x00;
566 for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) {
567 for(i = 0; i < stride_row; i++) {
568 data[y * stride_row + i] = 0x00;
571 gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, src_size, 0, src_size, data, _mm_destroy_notify);
574 gstcs_error("buffer is NULL\n");
575 return GSTCS_ERROR_INVALID_PARAMETER;
578 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
583 _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)
586 GstStateChangeReturn ret_state;
587 int ret = GSTCS_ERROR_NONE;
589 if(src== NULL || dst == NULL) {
590 gstcs_error("src || dst is NULL");
591 return GSTCS_ERROR_INVALID_PARAMETER;
595 ret = _mm_create_pipeline(pGstreamer_s);
596 if(ret != GSTCS_ERROR_NONE) {
597 gstcs_error("ERROR - mm_create_pipeline ");
600 pGstreamer_s->context = g_main_context_new();
601 if (pGstreamer_s->context == NULL) {
602 gstcs_error("ERROR - g_main_context_new ");
603 gst_object_unref (pGstreamer_s->pipeline);
604 g_free (pGstreamer_s);
605 return GSTCS_ERROR_INVALID_OPERATION;
607 pGstreamer_s->loop = g_main_loop_new (pGstreamer_s->context, FALSE);
608 if (pGstreamer_s->loop == NULL) {
609 gstcs_error("ERROR - g_main_loop_new ");
610 gst_object_unref (pGstreamer_s->pipeline);
611 g_main_context_unref(pGstreamer_s->context);
612 return GSTCS_ERROR_INVALID_OPERATION;
615 g_main_context_push_thread_default(pGstreamer_s->context);
617 /* 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. */
618 gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
620 bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline));
621 gst_bus_add_watch (bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s);
622 gst_object_unref(bus);
624 gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps);
625 gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps);
627 if (((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) &&
628 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
629 gstcs_debug("Start _mm_push_buffer_into_pipeline_new");
630 ret = _mm_push_buffer_into_pipeline_new(input_format, output_format, src, pGstreamer_s);
632 gstcs_debug("Start mm_push_buffer_into_pipeline");
633 ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s);
635 if(ret != GSTCS_ERROR_NONE) {
636 gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
637 gst_object_unref (pGstreamer_s->pipeline);
638 g_main_context_unref(pGstreamer_s->context);
639 g_main_loop_unref(pGstreamer_s->loop);
642 gstcs_debug("End mm_push_buffer_into_pipeline");
645 gstcs_debug("Start mm_link_pipeline");
646 _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
647 gstcs_debug("End mm_link_pipeline");
649 /* Conecting to the new-sample signal emited by the appsink*/
650 gstcs_debug("Start G_CALLBACK (_mm_sink_sample)");
651 g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_sample), pGstreamer_s);
652 gstcs_debug("End G_CALLBACK (_mm_sink_sample)");
654 /* GST_STATE_PLAYING*/
655 gstcs_debug("Start GST_STATE_PLAYING");
656 ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
657 gstcs_debug("End GST_STATE_PLAYING ret_state: %d", ret_state);
660 gstcs_debug("g_main_loop_run");
661 g_main_loop_run (pGstreamer_s->loop);
663 gstcs_debug("Sucess GST_STATE_CHANGE");
666 gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
667 gstcs_debug("End GST_STATE_NULL");
669 gstcs_debug("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
671 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, 1*GST_SECOND);
673 if(ret_state == GST_STATE_CHANGE_SUCCESS) {
674 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
675 }else if( ret_state == GST_STATE_CHANGE_ASYNC) {
676 gstcs_debug("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
679 gstcs_debug("Success gst_element_get_state\n");
681 if (ret_state == GST_STATE_CHANGE_FAILURE) {
682 gstcs_error("GST_STATE_CHANGE_FAILURE");
684 if(pGstreamer_s->output_buffer != NULL) {
685 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
686 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
687 int buffer_size = mapinfo.size;
688 int calc_buffer_size = 0;
689 if (((pImgp_info->dst_width != output_format->width) || (pImgp_info->dst_height != output_format->height)) &&
690 ((strcmp(input_format->colorspace, "RGB") == 0) || (strcmp(input_format->colorspace, "RGBA") == 0))) {
691 gstcs_debug("calculate image size with stride & elevation");
692 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, output_format->width, output_format->height);
694 calc_buffer_size = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
696 gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
697 if( buffer_size != calc_buffer_size) {
698 gstcs_debug("Buffer size is different \n");
699 gstcs_debug("unref output buffer");
700 gst_buffer_unref (pGstreamer_s->output_buffer);
701 gst_object_unref (pGstreamer_s->pipeline);
702 pGstreamer_s->output_buffer = NULL;
703 g_main_context_unref(pGstreamer_s->context);
704 g_main_loop_unref(pGstreamer_s->loop);
705 g_free (pGstreamer_s);
706 return GSTCS_ERROR_INVALID_OPERATION;
708 gstcs_debug("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
709 memcpy(dst, mapinfo.data, buffer_size);
710 pImgp_info->buffer_size = buffer_size;
711 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
713 gstcs_debug("pGstreamer_s->output_buffer is NULL");
716 gstcs_debug("unref output buffer");
717 gst_buffer_unref (pGstreamer_s->output_buffer);
718 gst_object_unref (pGstreamer_s->pipeline);
719 pGstreamer_s->output_buffer = NULL;
720 g_main_context_unref(pGstreamer_s->context);
721 g_main_loop_unref(pGstreamer_s->loop);
723 gstcs_debug("End gstreamer processing");
724 gstcs_debug("dst: %p", dst);
730 mm_setup_image_size(const char* _format_label, int width, int height)
734 if(strcmp(_format_label, "I420") == 0) {
735 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; */
736 }else if(strcmp(_format_label, "Y42B") == 0) {
737 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
738 }else if(strcmp(_format_label, "YUV422") == 0) {
739 size = (MM_UTIL_ROUND_UP_4(width) * height + MM_UTIL_ROUND_UP_8(width) * height); /*width * height *2; */
740 }else if(strcmp(_format_label, "Y444") == 0) {
741 size = (MM_UTIL_ROUND_UP_4(width) * height * 3); /* width * height *3; */
742 }else if(strcmp(_format_label, "YV12") == 0) {
743 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; */
744 }else if(strcmp(_format_label, "NV12") == 0) {
745 size = (MM_UTIL_ROUND_UP_4(width) * MM_UTIL_ROUND_UP_2(height) * 1.5); /* width * height *1.5; */
746 }else if(strcmp(_format_label, "RGB565") == 0) {
747 size = (MM_UTIL_ROUND_UP_4(width) * 2 * height); /* width * height *2; */
748 }else if(strcmp(_format_label, "RGB888") == 0) {
749 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
750 }else if(strcmp(_format_label, "BGR888") == 0) {
751 size = (MM_UTIL_ROUND_UP_4(width) * 3 * height); /* width * height *3; */
752 }else if(strcmp(_format_label, "UYVY") == 0) {
753 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
754 }else if(strcmp(_format_label, "YUYV") == 0) {
755 size = (MM_UTIL_ROUND_UP_2(width) * 2 * height); /* width * height *2; */
756 }else if(strcmp(_format_label, "ARGB8888") == 0) {
757 size = width * height *4;
758 }else if(strcmp(_format_label, "BGRA8888") == 0) {
759 size = width * height *4;
760 }else if(strcmp(_format_label, "RGBA8888") == 0) {
761 size = width * height *4;
762 }else if(strcmp(_format_label, "ABGR8888") == 0) {
763 size = width * height *4;
764 }else if(strcmp(_format_label, "BGRX") == 0) {
765 size = width * height *4;
768 gstcs_debug("file_size: %d\n", size);
774 _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst)
776 image_format_s* input_format=NULL, *output_format=NULL;
777 gstreamer_s* pGstreamer_s;
778 int ret = GSTCS_ERROR_NONE;
779 static const int max_argc = 50;
784 if(pImgp_info == NULL) {
785 gstcs_error("imgp_info_s is NULL");
786 return GSTCS_ERROR_INVALID_PARAMETER;
789 if(src== NULL || dst == NULL) {
790 gstcs_error("src || dst is NULL");
791 return GSTCS_ERROR_INVALID_PARAMETER;
793 gstcs_debug("[src %p] [dst %p]", src, dst);
795 argc = malloc(sizeof(int));
796 argv = malloc(sizeof(gchar*) * max_argc);
798 if (!argc || !argv) {
799 gstcs_error("argc ||argv is NULL");
800 GSTCS_FREE(input_format);
801 GSTCS_FREE(output_format);
808 return GSTCS_ERROR_INVALID_PARAMETER;
810 memset(argv, 0, sizeof(gchar*) * max_argc);
811 gstcs_debug("memset argv");
815 argv[*argc] = (gchar *)g_strdup( "mmutil_gstcs" );
817 /* check disable registry scan */
818 argv[*argc] = g_strdup("--gst-disable-registry-update");
820 gstcs_debug("--gst-disable-registry-update");
822 gst_init(argc, &argv);
824 pGstreamer_s = g_new0 (gstreamer_s, 1);
826 for (i = 0; i < (*argc); i++) {
832 gstcs_debug("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d",
833 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);
835 input_format= _mm_set_input_image_format_s_struct(pImgp_info);
836 if (input_format == NULL) {
837 gstcs_error("memory allocation failed");
838 g_free (pGstreamer_s);
839 return GSTCS_ERROR_OUT_OF_MEMORY;
841 output_format= _mm_set_output_image_format_s_struct(pImgp_info, input_format);
842 if (output_format == NULL) {
843 gstcs_error("memory allocation failed");
844 GSTCS_FREE(input_format->format_label);
845 GSTCS_FREE(input_format->colorspace);
846 GSTCS_FREE(input_format);
847 g_free (pGstreamer_s);
848 return GSTCS_ERROR_OUT_OF_MEMORY;
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 == GSTCS_ERROR_NONE) {
856 gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", dst);
857 }else if (ret != GSTCS_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);
868 g_free (pGstreamer_s);
874 mm_imgp(imgp_info_s* pImgp_info, unsigned char *src, unsigned char *dst, imgp_type_e _imgp_type)
876 if (pImgp_info == NULL) {
877 gstcs_error("input vaule is error");
879 return _mm_imgp_gstcs(pImgp_info, src, dst);