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>
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)
29 /*########################################################################################*/
30 #define setup_image_size_I420(width, height) { \
32 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); \
36 #define setup_image_size_Y42B(width, height) { \
38 size = (MM_UTIL_ROUND_UP_4 (width) * height + MM_UTIL_ROUND_UP_8 (width) * height); \
42 #define setup_image_size_Y444(width, height) { \
44 size = (MM_UTIL_ROUND_UP_4 (width) * height * 3); \
48 #define setup_image_size_UYVY(width, height) { \
50 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
54 #define setup_image_size_YUYV(width, height) { \
56 size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
60 #define setup_image_size_YV12(width, height) { \
62 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); \
66 #define setup_image_size_NV12(width, height) { \
68 size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5); \
72 #define setup_image_size_RGB565(width, height) { \
74 size = (MM_UTIL_ROUND_UP_4 (width * 2) *height); \
78 #define setup_image_size_RGB888(width, height) { \
80 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
84 #define setup_image_size_BGR888(width, height) { \
86 size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
89 /*########################################################################################*/
92 _mm_sink_buffer (GstElement * appsink, gpointer user_data)
95 gstreamer * gstreamer_h = (gstreamer*) user_data;
96 _buf = gst_app_sink_pull_buffer((GstAppSink*)appsink);
98 gstreamer_h->output_buffer = _buf;
100 if(gstreamer_h->output_buffer != NULL)
102 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n", __func__, __LINE__, GST_BUFFER_DATA(gstreamer_h->output_buffer), GST_BUFFER_SIZE (gstreamer_h->output_buffer));
106 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] ERROR -Input Prepare Buffer! Check createoutput buffer function", __func__, __LINE__);
108 gst_buffer_unref (_buf); //we don't need the appsink buffer anymore
109 gst_buffer_ref (gstreamer_h->output_buffer); //when you want to avoid flushing
113 _mm_sink_preroll (GstElement * appsink, gpointer user_data)
115 GstBuffer *_buf=NULL;
116 gstreamer * gstreamer_h = (gstreamer*) user_data;
117 _buf = gst_app_sink_pull_preroll((GstAppSink*)appsink);
119 gstreamer_h->output_buffer = _buf;
120 //gstreamer_h->output_image_format->caps = GST_BUFFER_CAPS(_buf);
121 if(gstreamer_h->output_buffer != NULL)
123 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d\n", __func__, __LINE__,
124 GST_BUFFER_DATA(gstreamer_h->output_buffer), GST_BUFFER_SIZE (gstreamer_h->output_buffer));
128 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] ERROR -Input Prepare Buffer! Check createoutput buffer function", __func__, __LINE__);
131 gst_buffer_unref (_buf); // we don't need the appsink buffer anymore
132 gst_buffer_ref (gstreamer_h->output_buffer); //when you want to avoid flushing
136 _mm_on_sink_message (GstBus * bus, GstMessage * message, gstreamer * gstreamer_h)
139 switch (GST_MESSAGE_TYPE (message))
141 case GST_MESSAGE_EOS:
142 mmf_debug(MMF_DEBUG_LOG,"Finished playback\n"); //g_main_loop_quit (gstreamer_h->loop);
144 case GST_MESSAGE_ERROR:
145 mmf_debug(MMF_DEBUG_ERROR,"Received error\n"); //g_main_loop_quit (gstreamer_h->loop);
147 case GST_MESSAGE_STATE_CHANGED:
148 mmf_debug(MMF_DEBUG_LOG, " [%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
150 case GST_MESSAGE_STREAM_STATUS:
151 mmf_debug(MMF_DEBUG_LOG, " [%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
154 mmf_debug(MMF_DEBUG_LOG, " [%s] %s(%d) \n", GST_MESSAGE_SRC_NAME(message), GST_MESSAGE_TYPE_NAME(message), GST_MESSAGE_TYPE(message));
162 _mm_create_pipeline( gstreamer* gstreamer_h)
164 int ret = MM_ERROR_NONE;
165 gstreamer_h->pipeline= gst_pipeline_new ("ffmpegcolorsapce");
166 gstreamer_h->appsrc= gst_element_factory_make("appsrc","appsrc");
167 gstreamer_h->colorspace=gst_element_factory_make("ffmpegcolorspace","colorconverter");
169 gstreamer_h->videoscale=gst_element_factory_make("videoscale", "scale");
170 gstreamer_h->videoflip=gst_element_factory_make( "videoflip", "flip" );
172 gstreamer_h->appsink=gst_element_factory_make("appsink","appsink");
174 if (!gstreamer_h->pipeline || !gstreamer_h->appsrc||!gstreamer_h->colorspace || !gstreamer_h->appsink)
176 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] One element could not be created. Exiting.\n", __func__, __LINE__);
177 ret = MM_ERROR_IMAGE_INVALID_VALUE;
183 _mm_check_resize_format(int src_width, int src_height, int dst_width, int dst_height)
185 gboolean _bool=FALSE;
187 if( (src_width != dst_width) || (src_height != dst_height) )
196 _mm_check_rotate_format(int GstVideoFlipMethod)
198 gboolean _bool=FALSE;
200 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") == 0)
242 _mm_link_pipeline_order_csc_rsz(gstreamer* gstreamer_h, image_format* input_format, image_format* output_format)
245 if(_mm_check_resize_format(input_format->width,input_format->height, output_format->width, output_format->height))
247 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] check_for_resize", __func__, __LINE__);
248 gst_bin_add_many(GST_BIN(gstreamer_h->pipeline), gstreamer_h->appsrc, gstreamer_h->videoscale, gstreamer_h->colorspace, gstreamer_h->appsink, NULL);
249 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] gst_bin_add_many", __func__, __LINE__);
250 if(_mm_check_resize_format_label( input_format->format_label))
252 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] input_format->format_label: %s", __func__, __LINE__, input_format->format_label);
253 if(!gst_element_link_many(gstreamer_h->appsrc, gstreamer_h->videoscale, gstreamer_h->colorspace, gstreamer_h->appsink, NULL)) mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Fail to link b/w ffmpeg and appsink except rot\n", __func__, __LINE__);
255 else if(_mm_check_resize_format_label(output_format->format_label))
257 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] output_format->format_label: %s", __func__, __LINE__, output_format->format_label);
258 if(!gst_element_link_many(gstreamer_h->appsrc, gstreamer_h->colorspace, gstreamer_h->videoscale, gstreamer_h->appsink, NULL)) mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Fail to link b/w ffmpeg and appsink except rot\n", __func__, __LINE__);
264 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] check_for_convert", __func__, __LINE__);
265 gst_bin_add_many(GST_BIN(gstreamer_h->pipeline), gstreamer_h->appsrc, gstreamer_h->colorspace, gstreamer_h->appsink, NULL);
266 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] gst_bin_add_many", __func__, __LINE__);
267 if(!gst_element_link_many(gstreamer_h->appsrc, gstreamer_h->colorspace, gstreamer_h->appsink, NULL)) mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Fail to link b/w ffmpeg and appsink except rsz & rot\n", __func__, __LINE__);
273 _mm_link_pipeline_order_csc_rsz_rot(gstreamer* gstreamer_h, image_format* input_format, image_format* output_format)
276 if(_mm_check_rotate_format_label(input_format->format_label))
278 gst_bin_add_many(GST_BIN(gstreamer_h->pipeline), gstreamer_h->appsrc, gstreamer_h->videoscale, gstreamer_h->videoflip, gstreamer_h->colorspace, gstreamer_h->appsink, NULL);
280 if(!gst_element_link_many(gstreamer_h->appsrc, gstreamer_h->videoscale, gstreamer_h->videoflip, gstreamer_h->colorspace, gstreamer_h->appsink, NULL)) mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Fail to link b/w appsrc and ffmpeg in rotate\n", __func__, __LINE__);
283 else if(_mm_check_rotate_format_label(output_format->format_label))
285 gst_bin_add_many(GST_BIN(gstreamer_h->pipeline), gstreamer_h->appsrc, gstreamer_h->colorspace, gstreamer_h->videoscale, gstreamer_h->videoflip,gstreamer_h->appsink, NULL);
286 if(!gst_element_link_many(gstreamer_h->appsrc, gstreamer_h->colorspace, gstreamer_h->videoscale, gstreamer_h->videoflip, gstreamer_h->appsink, NULL)) mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d]] Fail to link b/w ffmpeg and appsink in rotate\n", __func__, __LINE__);
292 _mm_link_pipeline( gstreamer* gstreamer_h, image_format* input_format, image_format* output_format, int _valuegstreamer_hVideoFlipMethod)
295 gst_app_src_set_caps(GST_APP_SRC(gstreamer_h->appsrc), input_format->caps); //g_object_set(gstreamer_h->appsrc, "caps", input_format->caps, NULL); // you can use appsrc'cap property
296 g_object_set(gstreamer_h->appsrc, "num-buffers", 1, NULL);
297 g_object_set(gstreamer_h->appsrc, "is-live", TRUE, NULL); // add because of gstreamer time issue
298 g_object_set (gstreamer_h->appsrc, "format", GST_FORMAT_TIME, NULL);
299 g_object_set(gstreamer_h->appsrc, "stream-type", 0 /*stream*/, NULL);
301 g_object_set(gstreamer_h->videoflip, "method", _valuegstreamer_hVideoFlipMethod, 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
303 gst_app_sink_set_caps(GST_APP_SINK(gstreamer_h->appsink), output_format->caps); //g_object_set(gstreamer_h->appsink, "caps", output_format->caps, NULL);
304 g_object_set(gstreamer_h->appsink, "drop", TRUE, NULL);
305 g_object_set(gstreamer_h->appsink, "emit-signals", TRUE, "sync", FALSE, NULL);
307 if(_mm_check_rotate_format(_valuegstreamer_hVideoFlipMethod)) // when you want to rotate image
309 /* because IYUV, I420, YV12 format can use vidoeflip*/
310 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] set_link_pipeline_order_csc_rsz_rot", __func__, __LINE__);
311 _mm_link_pipeline_order_csc_rsz_rot(gstreamer_h, input_format, output_format);
316 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] set_link_pipeline_order_csc_rsz", __func__, __LINE__);
317 _mm_link_pipeline_order_csc_rsz(gstreamer_h, input_format, output_format);
319 /* Conecting to the new-buffer signal emited by the appsink*/
320 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start G_CALLBACK (mm_sink_buffer)", __func__, __LINE__);
321 g_signal_connect (gstreamer_h->appsink, "new-buffer", G_CALLBACK (_mm_sink_buffer), gstreamer_h);
322 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End G_CALLBACK (mm_sink_buffer)", __func__, __LINE__)
328 _mm_set_image_format_capabilities(image_format* __format)//_format_label: I420 _colorsapace: YUV
330 char _a='A', _b='A', _c='A', _d='A';
331 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;
335 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Image format is NULL\n", __func__, __LINE__);
337 __format->caps = NULL;
340 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] colorspace: %s\n", __func__, __LINE__, __format->colorspace);
342 if(strcmp(__format->colorspace,"YUV") == 0)
344 if(strcmp(__format->format_label,"I420") == 0) {_a='I'; _b='4', _c='2', _d='0'; }
345 else if(strcmp(__format->format_label,"Y42B") == 0) {_a='Y'; _b='4', _c='2', _d='B'; }
346 else if(strcmp(__format->format_label,"Y444") == 0) {_a='Y'; _b='4', _c='4', _d='4'; }
347 else if(strcmp(__format->format_label,"YV12") == 0) {_a='Y'; _b='V', _c='1', _d='2'; } //GStreamer-CRITICAL **: gst_mini_object_ref: assertion `mini_object != NULL' failed
348 else if(strcmp(__format->format_label,"NV12") == 0) {_a='N'; _b='V', _c='1', _d='2'; }
349 else if(strcmp(__format->format_label,"UYVY") == 0) {_a='U'; _b='Y', _c='V', _d='Y'; }
350 else if(strcmp(__format->format_label,"YUYV") == 0) {_a='Y'; _b='U', _c='Y', _d='2'; }
352 __format->caps = gst_caps_new_simple ("video/x-raw-yuv",
353 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC (_a, _b, _c, _d), //'I', '4', '2', '0'),
354 "framerate", GST_TYPE_FRACTION, 25, 1,
355 "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
356 "width", G_TYPE_INT, __format->width,//320,
357 "height", G_TYPE_INT, __format->height, //240,
358 "framerate", GST_TYPE_FRACTION, 1, 1,
363 else if(strcmp(__format->colorspace,"RGB") ==0 || strcmp(__format->colorspace,"BGRX") ==0)
365 if(strcmp(__format->format_label,"RGB888") == 0) { _bpp=24; _depth=24; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _endianness=4321; }
366 else if(strcmp(__format->format_label,"BGR888") == 0) { _bpp=24; _depth=24; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _endianness=4321; }
367 else if(strcmp(__format->format_label,"RGB565") == 0) { _bpp=16; _depth=16; _red_mask=63488; _green_mask=2016; _blue_mask=31; _endianness=1234; }
368 else if( (strcmp(__format->format_label, "BGRX") == 0)) { _bpp=32; _depth=24; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _endianness=4321; }
369 __format->caps = gst_caps_new_simple ("video/x-raw-rgb",
370 "bpp", G_TYPE_INT, _bpp,
371 "depth", G_TYPE_INT, _depth,
372 "red_mask", G_TYPE_INT, _red_mask,
373 "green_mask", G_TYPE_INT, _green_mask,
374 "blue_mask", G_TYPE_INT, _blue_mask,
375 "width", G_TYPE_INT, __format->width,
376 "height", G_TYPE_INT, __format->height,
377 "endianness", G_TYPE_INT, _endianness,
378 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
381 else if(strcmp(__format->colorspace,"RGBA") ==0)
383 /*[Low Arrary Address] ARGBARGB... [High Array Address]*/
384 if(strcmp(__format->format_label,"ARGB8888") == 0) { _bpp=32; _depth=32; _red_mask=16711680; _green_mask=65280; _blue_mask=255; _alpha_mask=-16777216; _endianness=4321; }
386 /*[Low Arrary Address] BGRABGRA...[High Array Address]*/
387 else if(strcmp(__format->format_label,"BGRA8888") == 0) { _bpp=32; _depth=32; _red_mask=65280; _green_mask=16711680; _blue_mask=-16777216; _alpha_mask=255; _endianness=4321; }
389 /*[Low Arrary Address] RGBARGBA...[High Array Address]*/
390 else if(strcmp(__format->format_label,"RGBA8888") == 0) { _bpp=32; _depth=32; _red_mask=-16777216; _green_mask=16711680; _blue_mask=65280; _alpha_mask=255; _endianness=4321; }
392 /*[Low Arrary Address] ABGRABGR...[High Array Address]*/
393 else if(strcmp(__format->format_label,"ABGR8888") == 0) { _bpp=32; _depth=32; _red_mask=255; _green_mask=65280; _blue_mask=16711680; _alpha_mask=-16777216; _endianness=4321; }
394 else mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] ***Wrong format cs type***\n", __func__, __LINE__);
396 __format->caps = gst_caps_new_simple ("video/x-raw-rgb",
397 "bpp", G_TYPE_INT, _bpp,
398 "depth", G_TYPE_INT, _depth,
399 "red_mask", G_TYPE_INT, _red_mask,
400 "green_mask", G_TYPE_INT, _green_mask,
401 "blue_mask", G_TYPE_INT, _blue_mask,
402 "width", G_TYPE_INT, __format->width,
403 "height", G_TYPE_INT, __format->height,
404 "alpha_mask", G_TYPE_INT,_alpha_mask,
405 "endianness", G_TYPE_INT, _endianness,
406 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
410 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] ###__format->caps is not NULL###, %p", __func__, __LINE__, __format->caps);
414 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] __format->caps is NULL", __func__, __LINE__);
419 _mm_set_image_colorspace( image_format* __format)
421 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] format_label: %s\n", __func__, __LINE__, __format->format_label);
422 if( (strcmp(__format->format_label, "I420") == 0) ||(strcmp(__format->format_label, "Y42B") == 0) || (strcmp(__format->format_label, "Y444") == 0)
423 || (strcmp(__format->format_label, "YV12") == 0) ||(strcmp(__format->format_label, "NV12") == 0) ||(strcmp(__format->format_label, "UYVY") == 0) ||(strcmp(__format->format_label, "YUYV") == 0))
425 strncpy(__format->colorspace, "YUV", sizeof("__format->colorspace"));
427 else if( (strcmp(__format->format_label, "RGB888") == 0) ||(strcmp(__format->format_label, "BGR888") == 0) ||(strcmp(__format->format_label, "RGB565") == 0))
429 strncpy(__format->colorspace, "RGB", sizeof("__format->colorspace"));
431 else if( (strcmp(__format->format_label, "ARGB8888") == 0) || (strcmp(__format->format_label, "BGRA8888") == 0)
432 ||(strcmp(__format->format_label, "RGBA8888") == 0) || (strcmp(__format->format_label, "ABGR8888") == 0))
434 strncpy(__format->colorspace, "RGBA", sizeof("__format->colorspace"));
436 else if( (strcmp(__format->format_label, "BGRX") == 0))
438 strncpy(__format->colorspace, "BGRX", sizeof("__format->colorspace"));
440 else mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] Check your colorspace format label", __func__, __LINE__);
444 _mm_set_input_image_format_struct(imgp_info* _imgp_info) //char* __format_label, int __width, int __height)
446 image_format* __format = NULL;
448 __format=(image_format*)malloc(sizeof(image_format));
449 strncpy(__format->format_label, _imgp_info->input_format_label, sizeof(__format->format_label));
450 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] input_format_label: %s\n", __func__, __LINE__, _imgp_info->input_format_label);
451 _mm_set_image_colorspace(__format);
453 __format->width=_imgp_info->src_width;
454 __format->height=_imgp_info->src_height;
456 __format->blocksize = mm_setup_image_size(_imgp_info->input_format_label, _imgp_info->src_width, _imgp_info->src_height);
457 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] input_format_label: %s\n", __func__, __LINE__, _imgp_info->input_format_label);
458 _mm_set_image_format_capabilities(__format);
464 _mm_round_up_output_image_widh_height(char* __colorsapce, imgp_info* _imgp_info) //char* __colorspace, int __width, int __height)
466 if(strcmp(__colorsapce,"YUV") ==0)
468 _imgp_info->src_width =GST_ROUND_UP_8(_imgp_info->src_width); // because the result of image size is multiples of 8
469 _imgp_info->src_height=GST_ROUND_UP_8(_imgp_info->src_height);
471 else if(strcmp(__colorsapce, "RGB") ==0)
473 _imgp_info->src_width=GST_ROUND_UP_4(_imgp_info->src_width);
478 _mm_set_output_image_format_struct(imgp_info* _imgp_info)
480 image_format* __format = NULL;
482 __format=(image_format*)malloc(sizeof(image_format));
483 strncpy(__format->format_label, _imgp_info->output_format_label, sizeof(__format->format_label));
484 _mm_set_image_colorspace(__format);
485 _mm_round_up_output_image_widh_height(__format->colorspace, _imgp_info);
486 __format->width=_imgp_info->dst_width;
487 __format->height=_imgp_info->dst_height;
489 __format->blocksize = mm_setup_image_size(_imgp_info->output_format_label, _imgp_info->dst_width, _imgp_info->dst_height);
490 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] output_format_label: %s", __func__, __LINE__, _imgp_info->output_format_label);
491 _mm_set_image_format_capabilities(__format);
492 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] _imgp_info->dst: %p", __func__, __LINE__, _imgp_info->dst);
497 __mm_check_resize_format( char* _in_format_label, int _in_w, int _in_h, char* _out_format_label, int _out_w, int _out_h)
500 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] 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",
501 __func__, __LINE__,_in_format_label, _in_w,_in_h, _out_format_label, _out_w, _out_h);
502 if(_mm_check_resize_format(_in_w, _in_h, _out_w, _out_h))
504 if( !( _mm_check_resize_format_label(_in_format_label) ||_mm_check_resize_format_label(_out_format_label) ) )
506 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] ERROR -%s | %s Resize Only [AYUV] [YUY2] [YVYU] [UYVY] [Y800] [I420][YV12] format can use vidoeflip ERROR- Resize!!!#####\n", __func__, __LINE__, _in_format_label, _out_format_label);
514 __mm_check_rotate_format(int angle, const char* input_format_label, const char* output_format_label)
517 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] rotate value: %d input_format_label: %s output_format_label: %s\n",
518 __func__, __LINE__, angle, input_format_label, output_format_label);
519 if(_mm_check_rotate_format(angle))
521 if(!(_mm_check_rotate_format_label(input_format_label) || _mm_check_rotate_format_label(output_format_label)))
523 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] ERROR - %s | %s Rotate Only IYUV, I420, YV12 format can use vidoeflip ERROR -Rotate", __func__, __LINE__, input_format_label, output_format_label);
531 _mm_push_buffer_into_pipeline(imgp_info* _imgp_info, gstreamer * gstreamer_h, GstCaps*_caps)
533 int ret = MM_ERROR_NONE;
536 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] caps is NULL\n", __func__, __LINE__);
537 return MM_ERROR_IMAGE_INVALID_VALUE;
540 if(gstreamer_h->pipeline == NULL)
542 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] pipeline is NULL\n", __func__, __LINE__);
543 return MM_ERROR_IMAGE_INVALID_VALUE;
546 GstBuffer* gst_buf = (GstBuffer *) gst_mini_object_new (GST_TYPE_BUFFER);
550 mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] buffer is NULL\n", __func__, __LINE__);
551 return MM_ERROR_IMAGE_INVALID_VALUE;
553 GST_BUFFER_DATA (gst_buf) = (guint8 *) _imgp_info->src;
554 GST_BUFFER_SIZE (gst_buf) = mm_setup_image_size(_imgp_info->input_format_label, _imgp_info->src_width, _imgp_info->src_height);
555 GST_BUFFER_FLAG_SET (gst_buf, GST_BUFFER_FLAG_READONLY);
557 gst_buffer_set_caps (gst_buf, _caps);
558 gst_app_src_push_buffer (GST_APP_SRC (gstreamer_h->appsrc), gst_buf); //push buffer to pipeline
559 g_free(GST_BUFFER_MALLOCDATA(gst_buf)); gst_buf = NULL; //gst_buffer_finalize(gst_buf) { buffer->free_func (buffer->malloc_data); }
560 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #g_free#gst_buf: 0x%2x\n", __func__, __LINE__, gst_buf);
566 _mm_imgp_gstcs_processing( gstreamer* gstreamer_h, image_format* input_format, image_format* output_format, imgp_info* _imgp_info)
569 GstStateChangeReturn ret_state;
570 int ret = MM_ERROR_NONE;
572 ret = _mm_create_pipeline(gstreamer_h);
573 if(ret != MM_ERROR_NONE)
575 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] ERROR - mm_create_pipeline ", __func__, __LINE__);
579 /* Make appsink emit the "new-preroll" and "new-buffer" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
580 gst_app_sink_set_emit_signals ((GstAppSink*)gstreamer_h->appsink, TRUE);
582 bus = gst_pipeline_get_bus (GST_PIPELINE (gstreamer_h->pipeline)); //GST_PIPELINE (pipeline));
583 gst_bus_add_watch (bus, (GstBusFunc) _mm_on_sink_message , gstreamer_h); // thow to appplicaton
584 gst_object_unref(bus);
586 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start mm_push_buffer_into_pipeline", __func__, __LINE__);
587 ret = _mm_push_buffer_into_pipeline(_imgp_info, gstreamer_h, input_format->caps);
588 if(ret != MM_ERROR_NONE)
590 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] ERROR - mm_push_buffer_into_pipeline ", __func__, __LINE__);
592 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End mm_push_buffer_into_pipeline", __func__, __LINE__);
595 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start mm_link_pipeline", __func__, __LINE__);
596 _mm_link_pipeline( gstreamer_h, input_format, output_format, _imgp_info->angle);
597 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End mm_link_pipeline", __func__, __LINE__);
599 /* Conecting to the new-buffer signal emited by the appsink*/
600 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start G_CALLBACK (_mm_sink_preroll)", __func__, __LINE__);
601 g_signal_connect (gstreamer_h->appsink, "new-preroll", G_CALLBACK (_mm_sink_preroll), gstreamer_h);
602 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End G_CALLBACK (mm_sink_preroll)", __func__, __LINE__);
604 /* GST_STATE_PLAYING*/
605 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start GST_STATE_PLAYING", __func__, __LINE__);
606 ret_state = gst_element_set_state (gstreamer_h->pipeline, GST_STATE_PLAYING);
607 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End GST_STATE_PLAYING ret_state: %d", __func__, __LINE__, ret_state);
608 ret_state = gst_element_get_state (gstreamer_h->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
609 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Success Get GST_STATE_PLAYING ret_state: %d", __func__, __LINE__, ret_state);
611 /* Conecting to the new-buffer signal emited by the appsink*/
612 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Start G_CALLBACK (mm_sink_buffer)", __func__, __LINE__);
613 g_signal_connect (gstreamer_h->appsink, "new-buffer", G_CALLBACK (_mm_sink_buffer), gstreamer_h);
614 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End G_CALLBACK (mm_sink_buffer)", __func__, __LINE__);
616 if(ret_state == 1) { mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_SUCCESS)", __func__, __LINE__, ret_state); }
617 else if( ret_state == 2) { mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] GST_STATE_PLAYING ret = %d( GST_STATE_CHANGE_ASYNC)", __func__, __LINE__, ret_state); }
619 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] Sucess GST_STATE_CHANGE", __func__, __LINE__);
624 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] GST_STATE_CHANGE_FAILURE", __func__, __LINE__); // GST_STATE_CHANGE_SUCCESS = 1, GST_STATE_CHANGE_ASYNC = 2, GST_STATE_CHANGE_NO_PREROLL= 3
625 return MM_ERROR_IMAGE_INVALID_VALUE;
630 g_main_loop_run (gstreamer_h->loop);
632 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] Set GST_STATE_NULL", __func__, __LINE__);
635 gst_element_set_state (gstreamer_h->pipeline, GST_STATE_NULL);
636 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] End GST_STATE_NULL", __func__, __LINE__);
638 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] ###gstreamer_h->output_buffer### : %p", __func__, __LINE__, gstreamer_h->output_buffer);
640 ret_state = gst_element_get_state (gstreamer_h->pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
642 if(ret_state == 1) { mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_SUCCESS)\n", __func__, __LINE__, ret_state);}
643 else if( ret_state == 2) { mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] GST_STATE_NULL ret_state = %d (GST_STATE_CHANGE_ASYNC)\n", __func__, __LINE__, ret_state);}
645 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] Success gst_element_get_state\n", __func__, __LINE__);
649 mmf_debug(MMF_DEBUG_ERROR, "GST_STATE_CHANGE_FAILURE"); // GST_STATE_CHANGE_SUCCESS = 1, GST_STATE_CHANGE_ASYNC = 2, GST_STATE_CHANGE_NO_PREROLL= 3
653 if(gstreamer_h->output_buffer != NULL)
655 int buffer_size = GST_BUFFER_SIZE(gstreamer_h->output_buffer) ;
656 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] buffer size: %d\n", __func__, __LINE__, buffer_size);
657 if( buffer_size != mm_setup_image_size(_imgp_info->output_format_label, _imgp_info->output_stride, _imgp_info->output_elevation))
659 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] Buffer size is different\n", __func__, __LINE__);
661 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] gstreamer_h->output_buffer: 0x%2x\n", __func__, __LINE__, gstreamer_h->output_buffer);
662 memcpy( _imgp_info->dst, (char*)GST_BUFFER_DATA(gstreamer_h->output_buffer), buffer_size);
666 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] gstreamer_h->output_buffer is NULL", __func__, __LINE__);
669 gst_object_unref (gstreamer_h->pipeline);
670 g_free(GST_BUFFER_MALLOCDATA(gstreamer_h->output_buffer)); gstreamer_h->output_buffer = NULL; //gst_buffer_finalize(gstreamer_h->output_buffer) { buffer->free_func (buffer->malloc_data);}
671 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #g_free# gstreamer_h->output_buffer: 0x%2x\n", __func__, __LINE__, gstreamer_h->output_buffer);
673 g_free (gstreamer_h);
674 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] gstreamer_h: 0x%2x\n", __func__, __LINE__, gstreamer_h);
676 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] End mm_convert_colorspace \n", __func__, __LINE__);
678 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] _imgp_info->dst: %p", __func__, __LINE__, _imgp_info->dst);
684 mm_setup_image_size(const char* _format_label, int width, int height)
688 if(strcmp(_format_label, "I420") == 0) { setup_image_size_I420(width, height); } //width * height *1.5;
689 else if(strcmp(_format_label, "Y42B") == 0) { setup_image_size_Y42B(width, height); } //width * height *2;
690 else if(strcmp(_format_label, "YUV422") == 0) { setup_image_size_Y42B(width, height); } //width * height *2;
691 else if(strcmp(_format_label, "Y444") == 0) { setup_image_size_Y444(width, height); } //width * height *3;
692 else if(strcmp(_format_label, "YV12") == 0) { setup_image_size_YV12(width, height); } //width * height *1;
693 else if(strcmp(_format_label, "NV12") == 0) { setup_image_size_NV12(width, height); } //width * height *1.5;
694 else if(strcmp(_format_label, "RGB565") == 0) { setup_image_size_RGB565(width, height); } //width * height *2;
695 else if(strcmp(_format_label, "RGB888") == 0) { setup_image_size_RGB888(width, height); } //width * height *3;
696 else if(strcmp(_format_label, "BGR888") == 0) { setup_image_size_BGR888(width, height); } //width * height *3;
697 else if(strcmp(_format_label, "UYVY") == 0) { setup_image_size_UYVY(width, height); } //width * height *2;
698 else if(strcmp(_format_label, "YUYV") == 0) { setup_image_size_YUYV(width, height); } //width * height *2;
699 else if(strcmp(_format_label, "ARGB8888") == 0) { size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);}
700 else if(strcmp(_format_label, "BGRA8888") == 0) { size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);}
701 else if(strcmp(_format_label, "RGBA8888") == 0) { size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);}
702 else if(strcmp(_format_label, "ABGR8888") == 0) { size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);}
703 else if(strcmp(_format_label, "BGRX") == 0) { size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);}
709 _mm_imgp_gstcs(imgp_info* _imgp_info)
711 image_format* input_format=NULL, *output_format=NULL;
712 gstreamer* gstreamer_h;
713 int ret = MM_ERROR_NONE;
715 if(_imgp_info == NULL)
717 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] imgp_info is NULL", __func__, __LINE__);
720 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] [input] format label : %s width: %d height: %d\t[output] format label: %s width: %d height: %d rotation vaule: %d dst: 0x%2x", __func__, __LINE__,
721 _imgp_info->input_format_label, _imgp_info->src_width, _imgp_info->src_height, _imgp_info->output_format_label, _imgp_info->dst_width, _imgp_info->dst_height, _imgp_info->angle, _imgp_info->dst);
723 _imgp_info->output_stride = _imgp_info->dst_width;
724 _imgp_info->output_elevation = _imgp_info->dst_height;
725 if(_imgp_info->dst == NULL)
727 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] imgp_info->dst is NULL", __func__, __LINE__);
729 input_format= _mm_set_input_image_format_struct(_imgp_info);
730 output_format= _mm_set_output_image_format_struct(_imgp_info);
732 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] mm_check_resize_format&&mm_check_rotate_format ", __func__, __LINE__);
734 if( __mm_check_resize_format(_imgp_info->input_format_label, _imgp_info->src_width, _imgp_info->src_height, _imgp_info->output_format_label, _imgp_info->output_stride, _imgp_info->output_elevation)
735 && __mm_check_rotate_format(_imgp_info->angle, _imgp_info->input_format_label, _imgp_info->output_format_label) )
737 #if 0 // def GST_EXT_TIME_ANALYSIS
740 gst_init (NULL, NULL);
742 gstreamer_h = g_new0 (gstreamer, 1);
744 #if 0 // def GST_EXT_TIME_ANALYSIS
745 MMTA_ACUM_ITEM_BEGIN("ffmpegcolorspace", 0);
747 /* _format_label : I420, RGB888 etc*/
748 mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] Start mm_convert_colorspace ", __func__, __LINE__);
749 ret =_mm_imgp_gstcs_processing(gstreamer_h, input_format, output_format, _imgp_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)
753 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] End mm_convert_colorspace [_imgp_info->dst: %p]", __func__, __LINE__, _imgp_info->dst);
755 else if (ret != MM_ERROR_NONE)
757 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] ERROR -mm_convert_colorspace", __func__, __LINE__);
759 #if 0 //def GST_EXT_TIME_ANALYSIS
760 MMTA_ACUM_ITEM_END("ffmpegcolorspace", 0);
761 MMTA_ACUM_ITEM_SHOW_RESULT();
762 MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_FILE);
768 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] 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 ",__func__, __LINE__,
769 _imgp_info->input_format_label, _imgp_info->src_width, _imgp_info->src_height, _imgp_info->output_format_label, _imgp_info->output_stride, _imgp_info->output_elevation, _imgp_info->angle);
770 return MM_ERROR_IMAGE_INVALID_VALUE;
772 if(input_format) { free(input_format); input_format = NULL; }
773 if(output_format) { free(output_format); output_format = NULL; }
778 mm_imgp(imgp_info* _imgp_info, imgp_type _imgp_type)
780 if (_imgp_info == NULL)
782 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] input vaule is error", __func__, __LINE__);
784 return _mm_imgp_gstcs(_imgp_info);