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)
30 /*########################################################################################*/
31 #define setup_image_size_I420(width, height) { \
33 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); \
37 #define setup_image_size_Y42B(width, height) { \
39 size = (MM_UTIL_ROUND_UP_4 (width) * height + MM_UTIL_ROUND_UP_8 (width) * height); \
43 #define setup_image_size_Y444(width, height) { \
45 size = (MM_UTIL_ROUND_UP_4 (width) * height * 3); \
49 #define setup_image_size_UYVY(width, height) { \
51 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
55 #define setup_image_size_YUYV(width, height) { \
57 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
61 #define setup_image_size_YV12(width, height) { \
63 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); \
67 #define setup_image_size_NV12(width, height) { \
69 size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5); \
73 #define setup_image_size_RGB565(width, height) { \
75 size = (MM_UTIL_ROUND_UP_4 (width * 2) *height); \
79 #define setup_image_size_RGB888(width, height) { \
81 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
85 #define setup_image_size_BGR888(width, height) { \
87 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
90 /*########################################################################################*/
93 _mm_sink_buffer (GstElement * appsink, gpointer user_data)
96 GstSample *_sample = NULL;
97 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
98 _sample = gst_app_sink_pull_sample((GstAppSink*)appsink);
99 _buf = gst_sample_get_buffer(_sample);
101 pGstreamer_s->output_buffer = _buf;
103 if(pGstreamer_s->output_buffer != NULL) {
104 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
105 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
106 debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n", mapinfo.data, mapinfo.size);
107 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
109 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
111 gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushing */
112 gst_sample_unref(_sample);
116 _mm_sink_preroll (GstElement * appsink, gpointer user_data)
118 GstSample *_sample = NULL;
119 GstBuffer *_buf=NULL;
120 gstreamer_s * pGstreamer_s = (gstreamer_s*) user_data;
122 _sample = gst_app_sink_pull_preroll((GstAppSink*)appsink);
123 _buf = gst_sample_get_buffer(_sample);
125 pGstreamer_s->output_buffer = _buf;
126 /* pGstreamer_s->output_image_format_s->caps = GST_BUFFER_CAPS(_buf); */
127 if(pGstreamer_s->output_buffer != NULL) {
128 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
129 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
130 debug_log("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n",
131 mapinfo.data, mapinfo.size);
132 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
134 debug_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
136 gst_buffer_ref (pGstreamer_s->output_buffer); /* when you want to avoid flushings */
137 gst_sample_unref(_sample);
141 _mm_on_sink_message (GstBus * bus, GstMessage * message, gstreamer_s * pGstreamer_s)
143 switch (GST_MESSAGE_TYPE (message)) {
144 case GST_MESSAGE_EOS:
145 debug_log("Finished playback\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
147 case GST_MESSAGE_ERROR:
148 debug_error("Received error\n"); /* g_main_loop_quit (pGstreamer_s->loop); */
150 case GST_MESSAGE_STATE_CHANGED:
151 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
153 case GST_MESSAGE_STREAM_STATUS:
154 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
157 debug_log("[%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
165 _mm_create_pipeline( gstreamer_s* pGstreamer_s)
167 int ret = MM_ERROR_NONE;
168 pGstreamer_s->pipeline= gst_pipeline_new ("videoconvert");
169 pGstreamer_s->appsrc= gst_element_factory_make("appsrc","appsrc");
170 pGstreamer_s->colorspace=gst_element_factory_make("videoconvert","colorconverter");
172 pGstreamer_s->videoscale=gst_element_factory_make("videoscale", "scale");
173 pGstreamer_s->videoflip=gst_element_factory_make( "videoflip", "flip" );
175 pGstreamer_s->appsink=gst_element_factory_make("appsink","appsink");
177 if (!pGstreamer_s->pipeline || !pGstreamer_s->appsrc||!pGstreamer_s->colorspace || !pGstreamer_s->appsink) {
178 debug_error("One element could not be created. Exiting.\n");
179 ret = MM_ERROR_IMAGE_INVALID_VALUE;
185 _mm_check_resize_format(int src_width, int src_height, int dst_width, int dst_height)
187 gboolean _bool=FALSE;
189 if( (src_width != dst_width) || (src_height != dst_height) ) {
197 _mm_check_rotate_format(int GstVideoFlipMethod)
199 gboolean _bool=FALSE;
201 if((GstVideoFlipMethod >= 1) && ( GstVideoFlipMethod<= 7)) {
209 _mm_check_resize_format_label(char* __format_label)
211 gboolean _bool = FALSE;
213 if(strcmp(__format_label, "AYUV") == 0
214 || strcmp(__format_label, "UYVY") == 0 ||strcmp(__format_label, "Y800") == 0 || strcmp(__format_label, "I420") == 0 || strcmp(__format_label, "YV12") == 0
215 || strcmp(__format_label, "RGB888") == 0 || strcmp(__format_label, "RGB565") == 0 || strcmp(__format_label, "BGR888") == 0 || strcmp(__format_label, "RGBA8888") == 0
216 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ||strcmp(__format_label, "RGBX") == 0
217 ||strcmp(__format_label, "XRGB") == 0 ||strcmp(__format_label, "BGRX") == 0 ||strcmp(__format_label, "XBGR") == 0 ||strcmp(__format_label, "Y444") == 0
218 ||strcmp(__format_label, "Y42B") == 0 ||strcmp(__format_label, "YUY2") == 0 ||strcmp(__format_label, "YUYV") == 0 ||strcmp(__format_label, "UYVY") == 0
219 ||strcmp(__format_label, "Y41B") == 0 ||strcmp(__format_label, "Y16") == 0 ||strcmp(__format_label, "Y800") == 0 ||strcmp(__format_label, "Y8") == 0
220 ||strcmp(__format_label, "GREY") == 0 ||strcmp(__format_label, "AY64") == 0 || strcmp(__format_label, "YUV422") == 0) {
229 _mm_check_rotate_format_label(const char* __format_label)
231 gboolean _bool = FALSE;
233 if(strcmp(__format_label, "I420") == 0 ||strcmp(__format_label, "YV12") == 0 || strcmp(__format_label, "IYUV")
234 || strcmp(__format_label, "RGB888") ||strcmp(__format_label, "BGR888") == 0 ||strcmp(__format_label, "RGBA8888") == 0
235 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ) {
243 _mm_link_pipeline_order_csc_rsz(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
245 if(_mm_check_resize_format(input_format->width,input_format->height, output_format->width, output_format->height)) {
246 debug_log("check_for_resize");
247 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
248 debug_log("gst_bin_add_many");
249 if(_mm_check_resize_format_label( input_format->format_label)) {
250 debug_log(" input_format->format_label: %s", input_format->format_label);
251 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
252 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
254 }else if(_mm_check_resize_format_label(output_format->format_label)) {
255 debug_log("output_format->format_label: %s", output_format->format_label);
256 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->appsink, NULL)) {
257 debug_error("Fail to link b/w ffmpeg and appsink except rot\n");
261 debug_log("check_for_convert");
262 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
263 debug_log("gst_bin_add_many");
264 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
265 debug_error("Fail to link b/w ffmpeg and appsink except rsz & rot\n");
271 _mm_link_pipeline_order_csc_rsz_rot(gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format)
273 if(_mm_check_rotate_format_label(input_format->format_label)) {
274 gst_bin_add_many(GST_BIN(pGstreamer_s->pipeline), pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL);
275 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->colorspace, pGstreamer_s->appsink, NULL)) {
276 debug_error("Fail to link b/w appsrc and ffmpeg in rotate\n");
278 }else if(_mm_check_rotate_format_label(output_format->format_label)) {
279 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);
280 if(!gst_element_link_many(pGstreamer_s->appsrc, pGstreamer_s->colorspace, pGstreamer_s->videoscale, pGstreamer_s->videoflip, pGstreamer_s->appsink, NULL)) {
281 debug_error("Fail to link b/w ffmpeg and appsink in rotate\n");
288 _mm_link_pipeline( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, int _valuepGstreamer_sVideoFlipMethod)
291 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); you can use appsrc'cap property */
292 g_object_set(pGstreamer_s->appsrc, "num-buffers", 1, NULL);
293 g_object_set(pGstreamer_s->appsrc, "is-live", TRUE, NULL); /* add because of gstreamer_s time issue */
294 g_object_set (pGstreamer_s->appsrc, "format", GST_FORMAT_TIME, NULL);
295 g_object_set(pGstreamer_s->appsrc, "stream-type", 0 /*stream*/, NULL);
297 g_object_set(pGstreamer_s->videoflip, "method", _valuepGstreamer_sVideoFlipMethod, 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 */
299 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); */
300 g_object_set(pGstreamer_s->appsink, "drop", TRUE, NULL);
301 g_object_set(pGstreamer_s->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
303 if(_mm_check_rotate_format(_valuepGstreamer_sVideoFlipMethod)) { /* when you want to rotate image */
304 /* because IYUV, I420, YV12 format can use vidoeflip*/
305 debug_log("set_link_pipeline_order_csc_rsz_rot");
306 _mm_link_pipeline_order_csc_rsz_rot(pGstreamer_s, input_format, output_format);
308 debug_log("set_link_pipeline_order_csc_rsz");
309 _mm_link_pipeline_order_csc_rsz(pGstreamer_s, input_format, output_format);
311 /* Conecting to the new-sample signal emited by the appsink*/
312 debug_log("Start G_CALLBACK (mm_sink_buffer)");
313 g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
314 debug_log("End G_CALLBACK (mm_sink_buffer)");
320 _mm_set_image_format_s_capabilities(image_format_s* __format) /*_format_label: I420 _colorsapace: YUV */
322 char _format_name[sizeof(GST_VIDEO_FORMATS_ALL)] = {'\0'};
323 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;
325 if(__format == NULL) {
326 debug_error("Image format is NULL\n");
330 debug_log("colorspace: %s(%d)\n", __format->colorspace, strlen(__format->colorspace));
332 if(strcmp(__format->colorspace,"YUV") == 0) {
333 if(strcmp(__format->format_label,"I420") == 0
334 || strcmp(__format->format_label,"Y42B") == 0
335 || strcmp(__format->format_label,"Y444") == 0
336 || strcmp(__format->format_label,"YV12") == 0
337 || strcmp(__format->format_label,"NV12") == 0
338 || strcmp(__format->format_label,"UYVY") == 0) {
339 strcpy(_format_name, __format->format_label);
340 }else if(strcmp(__format->format_label,"YUYV") == 0) {
341 strcpy(_format_name, "YVYU");
344 __format->caps = gst_caps_new_simple ("video/x-raw",
345 "format", G_TYPE_STRING, _format_name,
346 "framerate", GST_TYPE_FRACTION, 25, 1,
347 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
348 "width", G_TYPE_INT, __format->width,
349 "height", G_TYPE_INT, __format->height,
350 "framerate", GST_TYPE_FRACTION, 1, 1,
354 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0) {
355 if(strcmp(__format->format_label,"RGB888") == 0) {
356 _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321;
357 }else if(strcmp(__format->format_label,"BGR888") == 0) {
358 _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321;
359 }else if(strcmp(__format->format_label,"RGB565") == 0) {
360 _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234;
361 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
362 _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321;
364 __format->caps = gst_caps_new_simple ("video/x-raw",
365 "format", G_TYPE_STRING, gst_video_format_to_string(
366 gst_video_format_from_masks(_depth, _bpp, _endianness,
367 _red_mask, _green_mask, _blue_mask, 0)),
368 "width", G_TYPE_INT, __format->width,
369 "height", G_TYPE_INT, __format->height,
370 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
373 else if(strcmp(__format->colorspace,"RGBA") ==0) {
374 if(strcmp(__format->format_label,"ARGB8888") == 0) { /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
375 _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321;
376 }else if(strcmp(__format->format_label,"BGRA8888") == 0) { /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
377 _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321;
378 }else if(strcmp(__format->format_label,"RGBA8888") == 0) { /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
379 _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321;
380 }else if(strcmp(__format->format_label,"ABGR8888") == 0) { /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
381 _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321;
383 debug_error("***Wrong format cs type***\n");
386 __format->caps = gst_caps_new_simple ("video/x-raw",
387 "format", G_TYPE_STRING, gst_video_format_to_string(
388 gst_video_format_from_masks(_depth, _bpp, _endianness,
389 _red_mask, _green_mask, _blue_mask, _alpha_mask)),
390 "width", G_TYPE_INT, __format->width,
391 "height", G_TYPE_INT, __format->height,
392 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
395 debug_log("###__format->caps is not NULL###, %p", __format->caps);
397 debug_error("__format->caps is NULL");
402 _mm_set_image_colorspace( image_format_s* __format)
404 debug_log("format_label: %s\n", __format->format_label);
406 __format->colorspace = (char*)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
407 memset(__format->colorspace, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
408 if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
409 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0)) {
410 strncpy(__format->colorspace, "YUV", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
411 }else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0)) {
412 strncpy(__format->colorspace, "RGB", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
413 }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)) {
414 strncpy(__format->colorspace, "RGBA",IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
415 }else if( (strcmp(__format->format_label, "BGRX") == 0)) {
416 strncpy(__format->colorspace, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE-1);
418 debug_error("Check your colorspace format label");
419 GSTCS_FREE(__format->colorspace);
423 static image_format_s*
424 _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) /* char* __format_label, int __width, int __height) */
426 image_format_s* __format = NULL;
428 __format=(image_format_s*)malloc(sizeof(image_format_s));
430 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
431 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
432 strncpy(__format->format_label, pImgp_info->input_format_label, strlen(pImgp_info->input_format_label));
433 debug_log("input_format_label: %s\n", pImgp_info->input_format_label);
434 _mm_set_image_colorspace(__format);
436 __format->width=pImgp_info->src_width;
437 __format->height=pImgp_info->src_height;
439 __format->blocksize = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
440 debug_log("blocksize: %d\n", __format->blocksize);
441 _mm_set_image_format_s_capabilities(__format);
447 _mm_round_up_output_image_widh_height(image_format_s* pFormat)
449 if(strcmp(pFormat->colorspace,"YUV") ==0) {
450 pFormat->stride=MM_UTIL_ROUND_UP_8(pFormat->width);
451 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
452 } else if(strcmp(pFormat->colorspace, "RGB") ==0) {
453 pFormat->stride=MM_UTIL_ROUND_UP_4(pFormat->width);
454 pFormat->elevation=MM_UTIL_ROUND_UP_2(pFormat->height);
458 static image_format_s*
459 _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info)
461 image_format_s* __format = NULL;
463 __format=(image_format_s*)malloc(sizeof(image_format_s));
465 __format->format_label = (char *)malloc(sizeof(char) * IMAGE_FORMAT_LABEL_BUFFER_SIZE);
466 memset(__format->format_label, 0, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
467 strncpy(__format->format_label, pImgp_info->output_format_label, strlen(pImgp_info->output_format_label));
468 _mm_set_image_colorspace(__format);
470 __format->width=pImgp_info->dst_width;
471 __format->height=pImgp_info->dst_height;
472 _mm_round_up_output_image_widh_height(__format);
474 __format->blocksize = mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->dst_width, pImgp_info->dst_height);
475 debug_log("output_format_label: %s", pImgp_info->output_format_label);
476 _mm_set_image_format_s_capabilities(__format);
477 debug_log("pImgp_info->dst: %p", pImgp_info->dst);
482 __mm_check_resize_format( char* _in_format_label, int _in_w, int _in_h, char* _out_format_label, int _out_w, int _out_h)
485 debug_log("input image format: %s input image width: %d input image height: %d output image format: %s output image width: %d output image height: %d\n",
486 _in_format_label, _in_w,_in_h, _out_format_label, _out_w, _out_h);
487 if(_mm_check_resize_format(_in_w, _in_h, _out_w, _out_h)) {
488 if( !( _mm_check_resize_format_label(_in_format_label) ||_mm_check_resize_format_label(_out_format_label) ) ) {
489 debug_error("ERROR-%s | %s Resize Only [AYUV] [YUY2] [YVYU] [UYVY] [Y800] [I420][YV12] format can use vidoeflip ERROR- Resize!!!#####\n", _in_format_label, _out_format_label);
497 __mm_check_rotate_format(int angle, const char* input_format_label, const char* output_format_label)
500 debug_log("rotate value: %d input_format_label: %s output_format_label: %s\n",
501 angle, input_format_label, output_format_label);
502 if(_mm_check_rotate_format(angle)) {
503 if(!(_mm_check_rotate_format_label(input_format_label) || _mm_check_rotate_format_label(output_format_label))) {
504 debug_error("ERROR- %s | %s Rotate Only IYUV, I420, YV12 format can use vidoeflip ERROR -Rotate", input_format_label, output_format_label);
512 _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, gstreamer_s * pGstreamer_s)
514 int ret = MM_ERROR_NONE;
516 if(pGstreamer_s->pipeline == NULL) {
517 debug_error("pipeline is NULL\n");
518 return MM_ERROR_IMAGE_INVALID_VALUE;
521 gsize data_size = mm_setup_image_size(pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height);
522 GstBuffer* gst_buf = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, pImgp_info->src, data_size, 0, data_size, NULL, NULL);
525 debug_error("buffer is NULL\n");
526 return MM_ERROR_IMAGE_INVALID_VALUE;
529 gst_app_src_push_buffer (GST_APP_SRC (pGstreamer_s->appsrc), gst_buf); /* push buffer to pipeline */
535 _mm_imgp_gstcs_processing( gstreamer_s* pGstreamer_s, image_format_s* input_format, image_format_s* output_format, imgp_info_s* pImgp_info)
538 GstStateChangeReturn ret_state;
539 int ret = MM_ERROR_NONE;
541 ret = _mm_create_pipeline(pGstreamer_s);
542 if(ret != MM_ERROR_NONE) {
543 debug_error("ERROR - mm_create_pipeline ");
547 /* 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. */
548 gst_app_sink_set_emit_signals ((GstAppSink*)pGstreamer_s->appsink, TRUE);
550 bus = gst_pipeline_get_bus (GST_PIPELINE (pGstreamer_s->pipeline)); /* GST_PIPELINE (pipeline)); */
551 /* gst_bus_add_watch (bus, (GstBusFunc) _mm_on_sink_message, pGstreamer_s); thow to appplicaton */
552 gst_object_unref(bus);
554 debug_log("Start mm_push_buffer_into_pipeline");
555 ret = _mm_push_buffer_into_pipeline(pImgp_info, pGstreamer_s);
556 if(ret != MM_ERROR_NONE) {
557 debug_error("ERROR - mm_push_buffer_into_pipeline ");
559 debug_log("End mm_push_buffer_into_pipeline");
562 debug_log("Start mm_link_pipeline");
563 _mm_link_pipeline( pGstreamer_s, input_format, output_format, pImgp_info->angle);
564 debug_log("End mm_link_pipeline");
566 /* Conecting to the new-sample signal emited by the appsink*/
567 debug_log("Start G_CALLBACK (_mm_sink_preroll)");
568 g_signal_connect (pGstreamer_s->appsink, "new-preroll", G_CALLBACK (_mm_sink_preroll), pGstreamer_s);
569 debug_log("End G_CALLBACK (mm_sink_preroll)");
571 /* GST_STATE_PLAYING*/
572 debug_log("Start GST_STATE_PLAYING");
573 ret_state = gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_PLAYING);
574 debug_log("End GST_STATE_PLAYING ret_state: %d", ret_state);
575 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
576 debug_log("Success Get GST_STATE_PLAYING ret_state: %d", ret_state);
578 /* Conecting to the new-sample signal emited by the appsink*/
579 debug_log("Start G_CALLBACK (mm_sink_buffer)");
580 g_signal_connect (pGstreamer_s->appsink, "new-sample", G_CALLBACK (_mm_sink_buffer), pGstreamer_s);
581 debug_log("End G_CALLBACK (mm_sink_buffer)");
584 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_SUCCESS)", ret_state);
585 }else if( ret_state == 2) {
586 debug_log("GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_ASYNC)", ret_state);
589 debug_log("Sucess GST_STATE_CHANGE");
592 if (ret_state == 0) {
593 debug_error("GST_STATE_CHANGE_FAILURE");
594 return MM_ERROR_IMAGE_INVALID_VALUE;
597 g_main_loop_run (pGstreamer_s->loop);
599 debug_log("Set GST_STATE_NULL");
602 gst_element_set_state (pGstreamer_s->pipeline, GST_STATE_NULL);
603 debug_log("End GST_STATE_NULL");
605 debug_log("###pGstreamer_s->output_buffer### : %p", pGstreamer_s->output_buffer);
607 ret_state = gst_element_get_state (pGstreamer_s->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
610 debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", ret_state);
611 }else if( ret_state == 2) {
612 debug_log("GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", ret_state);
615 debug_log("Success gst_element_get_state\n");
617 if (ret_state == 0) {
618 debug_error("GST_STATE_CHANGE_FAILURE");
620 if(pGstreamer_s->output_buffer != NULL) {
621 GstMapInfo mapinfo = GST_MAP_INFO_INIT;
622 gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
623 int buffer_size = mapinfo.size;
624 debug_log("buffer size: %d\n", buffer_size);
625 if( buffer_size != mm_setup_image_size(pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation)) {
626 debug_log("Buffer size is different stride:%d elevation: %d\n", pImgp_info->output_stride, pImgp_info->output_elevation);
628 debug_log("pGstreamer_s->output_buffer: 0x%2x\n", pGstreamer_s->output_buffer);
629 memcpy( pImgp_info->dst, mapinfo.data, buffer_size);
630 gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
632 debug_log("pGstreamer_s->output_buffer is NULL");
635 gst_object_unref (pGstreamer_s->pipeline);
636 pGstreamer_s->output_buffer = NULL;
637 g_free (pGstreamer_s);
639 debug_log("End mm_convert_colorspace \n");
641 debug_log("pImgp_info->dst: %p", pImgp_info->dst);
647 mm_setup_image_size(const char* _format_label, int width, int height)
651 if(strcmp(_format_label, "I420") == 0) {
652 setup_image_size_I420(width, height); /*width * height *1.5; */
653 }else if(strcmp(_format_label, "Y42B") == 0) {
654 setup_image_size_Y42B(width, height); /*width * height *2; */
655 }else if(strcmp(_format_label, "YUV422") == 0) {
656 setup_image_size_Y42B(width, height); /*width * height *2; */
657 }else if(strcmp(_format_label, "Y444") == 0) {
658 setup_image_size_Y444(width, height); /* width * height *3; */
659 }else if(strcmp(_format_label, "YV12") == 0) {
660 setup_image_size_YV12(width, height); /* width * height *1; */
661 }else if(strcmp(_format_label, "NV12") == 0) {
662 setup_image_size_NV12(width, height); /* width * height *1.5; */
663 }else if(strcmp(_format_label, "RGB565") == 0) {
664 setup_image_size_RGB565(width, height); /* width * height *2; */
665 }else if(strcmp(_format_label, "RGB888") == 0) {
666 setup_image_size_RGB888(width, height); /* width * height *3; */
667 }else if(strcmp(_format_label, "BGR888") == 0) {
668 setup_image_size_BGR888(width, height); /* width * height *3; */
669 }else if(strcmp(_format_label, "UYVY") == 0) {
670 setup_image_size_UYVY(width, height); /* width * height *2; */
671 }else if(strcmp(_format_label, "YUYV") == 0) {
672 setup_image_size_YUYV(width, height); /* width * height *2; */
673 }else if(strcmp(_format_label, "ARGB8888") == 0) {
674 size = width * height *4; debug_log("file_size: %d\n", size);
675 }else if(strcmp(_format_label, "BGRA8888") == 0) {
676 size = width * height *4; debug_log("file_size: %d\n", size);
677 }else if(strcmp(_format_label, "RGBA8888") == 0) {
678 size = width * height *4; debug_log("file_size: %d\n", size);
679 }else if(strcmp(_format_label, "ABGR8888") == 0) {
680 size = width * height *4; debug_log("file_size: %d\n", size);
681 }else if(strcmp(_format_label, "BGRX") == 0) {
682 size = width * height *4; debug_log("file_size: %d\n", size);
689 _mm_imgp_gstcs(imgp_info_s* pImgp_info)
691 image_format_s* input_format=NULL, *output_format=NULL;
692 gstreamer_s* pGstreamer_s;
693 int ret = MM_ERROR_NONE;
694 static const int max_argc = 50;
699 if(pImgp_info == NULL) {
700 debug_error("imgp_info_s is NULL");
703 debug_log("[input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d dst: 0x%2x",
704 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, pImgp_info->dst);
706 if(pImgp_info->dst == NULL) {
707 debug_error("imgp_info_s->dst is NULL");
709 input_format= _mm_set_input_image_format_s_struct(pImgp_info);
710 output_format= _mm_set_output_image_format_s_struct(pImgp_info);
712 pImgp_info->output_stride = output_format->stride;
713 pImgp_info->output_elevation = output_format->elevation;
715 debug_log("mm_check_resize_format&&mm_check_rotate_format ");
717 if(__mm_check_resize_format(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)
718 && __mm_check_rotate_format(pImgp_info->angle, pImgp_info->input_format_label, pImgp_info->output_format_label) ) {
719 #if 0 /* def GST_EXT_TIME_ANALYSIS */
722 argc = malloc(sizeof(int));
723 argv = malloc(sizeof(gchar*) * max_argc);
725 if (!argc || !argv) {
726 debug_error("argc ||argv is NULL");
727 return MM_ERROR_IMAGE_INVALID_VALUE;
729 memset(argv, 0, sizeof(gchar*) * max_argc);
730 debug_log("memset argv");
734 argv[0] = g_strdup( "mmutil_gstcs" );
735 /* check disable registry scan */
736 argv[*argc] = g_strdup("--gst-disable-registry-update");
738 debug_log("--gst-disable-registry-update");
740 gst_init(argc, &argv);
742 pGstreamer_s = g_new0 (gstreamer_s, 1);
744 #if 0 /* def GST_EXT_TIME_ANALYSIS */
745 MMTA_ACUM_ITEM_BEGIN("videoconvert", 0);
747 /* _format_label : I420, RGB888 etc*/
748 debug_log("Start mm_convert_colorspace ");
749 ret =_mm_imgp_gstcs_processing(pGstreamer_s, 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 */
751 if(ret == MM_ERROR_NONE) {
752 debug_log("End mm_convert_colorspace [pImgp_info->dst: %p]", pImgp_info->dst);
753 }else if (ret != MM_ERROR_NONE) {
754 debug_error("ERROR - mm_convert_colorspace");
756 #if 0 /* def GST_EXT_TIME_ANALYSIS */
757 MMTA_ACUM_ITEM_END("videoconvert", 0);
758 MMTA_ACUM_ITEM_SHOW_RESULT();
759 MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
763 debug_error("Error - Check your input / ouput image input_format_label: %s src_width: %d src_height: %d output_format_label: %s output_stride: %d output_elevation: %d angle: %d ",
764 pImgp_info->input_format_label, pImgp_info->src_width, pImgp_info->src_height, pImgp_info->output_format_label, pImgp_info->output_stride, pImgp_info->output_elevation, pImgp_info->angle);
765 ret = MM_ERROR_IMAGE_INVALID_VALUE;
771 GSTCS_FREE(input_format->format_label);
772 GSTCS_FREE(input_format->colorspace);
773 GSTCS_FREE(input_format);
775 GSTCS_FREE(output_format->format_label);
776 GSTCS_FREE(output_format->colorspace);
777 GSTCS_FREE(output_format);
783 mm_imgp(imgp_info_s* pImgp_info, imgp_type_e _imgp_type)
785 if (pImgp_info == NULL) {
786 debug_error("input vaule is error");
788 return _mm_imgp_gstcs(pImgp_info);