From 54787659a2d21825b39fb0e76c92c8eb38e48d7e Mon Sep 17 00:00:00 2001 From: hj kim Date: Wed, 14 Feb 2018 11:54:22 +0900 Subject: [PATCH] Remove input and output format structure Change-Id: Ied141fdbf684199e52c203882ad43e76e6b0e186 --- gstcs/mm_util_gstcs.c | 194 +++++++++++++++----------------------------------- 1 file changed, 56 insertions(+), 138 deletions(-) diff --git a/gstcs/mm_util_gstcs.c b/gstcs/mm_util_gstcs.c index 53dc2b2..b0b9c9a 100755 --- a/gstcs/mm_util_gstcs.c +++ b/gstcs/mm_util_gstcs.c @@ -268,145 +268,67 @@ static GstVideoFormat _mm_get_video_format(mm_util_color_format_e color_format) } -static void _mm_set_capabilities(image_format_s* __format) +static GstCaps* _mm_get_capabilities(mm_util_color_format_e color_format, unsigned int width, unsigned int height) { + GstCaps *caps = NULL; GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN; - if (__format == NULL) { - gstcs_error("Image format is NULL\n"); - return; - } - - videoFormat = _mm_get_video_format(__format->color_format); + videoFormat = _mm_get_video_format(color_format); - __format->caps = gst_caps_new_simple("video/x-raw", + caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat), "framerate", GST_TYPE_FRACTION, 25, 1, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1, - "width", G_TYPE_INT, __format->stride, - "height", G_TYPE_INT, __format->elevation, + "width", G_TYPE_INT, width, + "height", G_TYPE_INT, height, "framerate", GST_TYPE_FRACTION, 1, 1, NULL); - if (__format->caps) { - gstcs_debug("###__format->caps is not NULL###, %p", __format->caps); - _mm_check_caps_format(__format->caps); - } else { - gstcs_error("__format->caps is NULL"); - } -} - -static int _gstcs_create_image_format(image_format_s **format) -{ - int ret = GSTCS_ERROR_NONE; - image_format_s *_format = NULL; - - if (format == NULL) { - gstcs_error("format is wrong value"); - return GSTCS_ERROR_INVALID_OPERATION; - } - - _format = (image_format_s*)malloc(sizeof(image_format_s)); - if (_format == NULL) { - gstcs_error("memory allocation failed"); - return GSTCS_ERROR_OUT_OF_MEMORY; - } - memset(_format, 0, sizeof(image_format_s)); - *format = _format; - - return ret; -} + if (caps) + _mm_check_caps_format(caps); + else + gstcs_error("caps is NULL"); -static void _gstcs_destroy_image_format(image_format_s *format) -{ - if (format != NULL) { - gst_caps_unref(format->caps); - GSTCS_FREE(format); - } + return caps; } -static void _mm_round_up_widh_height(image_format_s* pFormat) +static void _mm_get_round_up_width_height(mm_util_color_format_e color_format, unsigned int width, unsigned int height, unsigned int *stride, unsigned int *elevation) { - switch (pFormat->color_format) { + switch (color_format) { case MM_UTIL_COLOR_YUV420: case MM_UTIL_COLOR_YUV422: case MM_UTIL_COLOR_I420: case MM_UTIL_COLOR_NV12: case MM_UTIL_COLOR_UYVY: case MM_UTIL_COLOR_YUYV: - pFormat->stride = pFormat->width; - pFormat->elevation = pFormat->height; + *stride = width; + *elevation = height; break; case MM_UTIL_COLOR_RGB16: case MM_UTIL_COLOR_RGB24: - pFormat->stride = MM_UTIL_ROUND_UP_4(pFormat->width); - pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height); + *stride = MM_UTIL_ROUND_UP_4(width); + *elevation = MM_UTIL_ROUND_UP_2(height); break; case MM_UTIL_COLOR_ARGB: case MM_UTIL_COLOR_BGRA: case MM_UTIL_COLOR_RGBA: case MM_UTIL_COLOR_BGRX: - pFormat->stride = pFormat->width; - pFormat->elevation = MM_UTIL_ROUND_UP_2(pFormat->height); + *stride = width; + *elevation = MM_UTIL_ROUND_UP_2(height); break; default: gstcs_error("Not supported format"); break; } - gstcs_debug("color_format [%d] stride [%d], elevation [%d]", pFormat->color_format, pFormat->stride, pFormat->elevation); -} - -static image_format_s* _mm_set_input_image_format_s_struct(imgp_info_s* pImgp_info) -{ - int ret = GSTCS_ERROR_NONE; - image_format_s* __format = NULL; - - ret = _gstcs_create_image_format(&__format); - if (ret != GSTCS_ERROR_NONE) { - gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret); - return NULL; - } - - __format->color_format = pImgp_info->src_format; - __format->width = pImgp_info->src_width; - __format->height = pImgp_info->src_height; - _mm_round_up_widh_height(__format); - - _mm_set_capabilities(__format); - - return __format; + gstcs_debug("color_format[%d] width[%u] height[%u] stride[%u], elevation[%u]", color_format, width, height, *stride, *elevation); } -static image_format_s* _mm_set_output_image_format_s_struct(imgp_info_s* pImgp_info) +static int _mm_setup_image_size(mm_util_color_format_e color_format, unsigned int width, unsigned int height) { - int ret = GSTCS_ERROR_NONE; - image_format_s* __format = NULL; + unsigned int size = 0; - ret = _gstcs_create_image_format(&__format); - if (ret != GSTCS_ERROR_NONE) { - gstcs_debug("Error: _gstcs_create_image_format is failed (%d)\n", ret); - return NULL; - } - - __format->color_format = pImgp_info->dst_format; - __format->width = pImgp_info->dst_width; - __format->height = pImgp_info->dst_height; - _mm_round_up_widh_height(__format); - - _mm_set_capabilities(__format); - - pImgp_info->output_stride = __format->stride; - pImgp_info->output_elevation = __format->elevation; - - return __format; -} - -static int _mm_setup_image_size(mm_util_color_format_e color_format, int width, int height) -{ - int size = 0; - - gstcs_debug("color_format [%d] width [%d] height [%d]", color_format, width, height); + gstcs_debug("color_format [%d] width [%u] height [%u]", color_format, width, height); switch (color_format) { case MM_UTIL_COLOR_YUV420: @@ -445,7 +367,7 @@ static int _mm_setup_image_size(mm_util_color_format_e color_format, int width, } - gstcs_debug("Image size: %d", size); + gstcs_debug("Image size [%u]", size); return size; } @@ -472,30 +394,28 @@ static int _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char return ret; } -static int _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsigned char *src, gstreamer_s * pGstreamer_s) +static int _mm_push_buffer_into_pipeline_new(unsigned char *src, gstreamer_s * pGstreamer_s, mm_util_color_format_e color_format, unsigned int width, unsigned int height, unsigned int stride, unsigned int elevation) { int ret = GSTCS_ERROR_NONE; GstBuffer *gst_buf = NULL; unsigned int src_size = 0; unsigned char *data = NULL; - unsigned int stride = input_format->stride; - unsigned int elevation = input_format->elevation; gstcs_fenter(); gstcs_retvm_if(pGstreamer_s->pipeline == NULL, GSTCS_ERROR_INVALID_PARAMETER, "Invalid pipeline"); - src_size = _mm_setup_image_size(input_format->color_format, stride, elevation); + src_size = _mm_setup_image_size(color_format, stride, elevation); - int byte_per_pixcel = _mm_get_byte_per_pixcel(input_format->color_format); - unsigned int src_row = input_format->width * byte_per_pixcel; + int byte_per_pixcel = _mm_get_byte_per_pixcel(color_format); + unsigned int src_row = width * byte_per_pixcel; unsigned int stride_row = stride * byte_per_pixcel; unsigned int i = 0, y = 0; data = (unsigned char *) malloc(src_size); gstcs_retvm_if(data == NULL, GSTCS_ERROR_OUT_OF_MEMORY, "data is NULL"); - for (y = 0; y < (unsigned int)(input_format->height); y++) { + for (y = 0; y < height; y++) { guint8 *pLine = (guint8 *) &(src[src_row * y]); for (i = 0; i < src_row; i++) data[y * stride_row + i] = pLine[i]; @@ -503,7 +423,7 @@ static int _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsig for (i = src_row; i < stride_row; i++) data[y * stride_row + i] = stride_row_color; } - for (y = (unsigned int)(input_format->height); y < (unsigned int)(input_format->elevation); y++) { + for (y = height; y < elevation; y++) { for (i = 0; i < stride_row; i++) data[y * stride_row + i] = data[(y - 1) * stride_row + i]; } @@ -522,11 +442,15 @@ static int _mm_push_buffer_into_pipeline_new(image_format_s *input_format, unsig return ret; } -static int _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) +static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *src, unsigned char **dst, imgp_info_s* pImgp_info) { GstBus *bus = NULL; GstStateChangeReturn ret_state; int ret = GSTCS_ERROR_NONE; + GstCaps *src_caps = NULL; + GstCaps *dst_caps = NULL; + unsigned int src_stride = 0; + unsigned int src_elevation = 0; gstcs_fenter(); @@ -542,17 +466,27 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s gst_bus_add_watch(bus, (GstBusFunc) _mm_on_src_message, pGstreamer_s); gst_object_unref(bus); - gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), input_format->caps); - gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), output_format->caps); + _mm_get_round_up_width_height(pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, &src_stride, &src_elevation); + _mm_get_round_up_width_height(pImgp_info->dst_format, pImgp_info->dst_width, pImgp_info->dst_height, &pImgp_info->output_stride, &pImgp_info->output_elevation); - if ((input_format->width != input_format->stride) || (input_format->height != input_format->elevation)) { - ret = _mm_push_buffer_into_pipeline_new(input_format, src, pGstreamer_s); + src_caps = _mm_get_capabilities(pImgp_info->src_format, src_stride, src_elevation); + dst_caps = _mm_get_capabilities(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation); + + gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), src_caps); + gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), dst_caps); + + if ((pImgp_info->src_width != src_stride) || (pImgp_info->src_height != src_elevation)) { + ret = _mm_push_buffer_into_pipeline_new(src, pGstreamer_s, pImgp_info->src_format, pImgp_info->src_width, pImgp_info->src_height, src_stride, src_elevation); } else { ret = _mm_push_buffer_into_pipeline(pImgp_info, src, pGstreamer_s); } if (ret != GSTCS_ERROR_NONE) { gstcs_error("ERROR - mm_push_buffer_into_pipeline "); + if (src_caps) + gst_caps_unref(src_caps); + if (dst_caps) + gst_caps_unref(dst_caps); gst_object_unref(pGstreamer_s->pipeline); return ret; } @@ -591,6 +525,11 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s gstcs_debug("Success gst_element_get_state\n"); + if (src_caps) + gst_caps_unref(src_caps); + if (dst_caps) + gst_caps_unref(dst_caps); + if (ret_state == GST_STATE_CHANGE_FAILURE) { gstcs_error("GST_STATE_CHANGE_FAILURE"); } else { @@ -600,7 +539,7 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s int buffer_size = mapinfo.size; int calc_buffer_size = 0; - calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, output_format->stride, output_format->elevation); + calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation); gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size); if (buffer_size != calc_buffer_size) { @@ -723,7 +662,6 @@ static int _gstcs_init(gstreamer_s** gstreamer) static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned char **dst) { - image_format_s* input_format = NULL, *output_format = NULL; gstreamer_s* pGstreamer_s; int ret = GSTCS_ERROR_NONE; @@ -738,34 +676,16 @@ static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned return ret; } - /* Create input/output format for gstreamer processing */ - input_format = _mm_set_input_image_format_s_struct(pImgp_info); - if (input_format == NULL) { - gstcs_error("Error: memory allocation failed"); - GSTCS_FREE(pGstreamer_s); - return GSTCS_ERROR_OUT_OF_MEMORY; - } - - output_format = _mm_set_output_image_format_s_struct(pImgp_info); - if (output_format == NULL) { - gstcs_error("Error: memory allocation failed"); - _gstcs_destroy_image_format(input_format); - GSTCS_FREE(pGstreamer_s); - return GSTCS_ERROR_OUT_OF_MEMORY; - } - /* Create default thread for async behavior */ ret = _gstcs_create_default_thread(pGstreamer_s); if (ret != GSTCS_ERROR_NONE) { gstcs_error("Error: _gstcs_create_default_thread is failed"); - _gstcs_destroy_image_format(input_format); - _gstcs_destroy_image_format(output_format); GSTCS_FREE(pGstreamer_s); return ret; } /* Do gstreamer processing */ - 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 */ + ret = _mm_imgp_gstcs_processing(pGstreamer_s, src, dst, pImgp_info); /* input: buffer pointer for input image , input image format, input image width, input image height, output: buffer porinter for output image */ if (ret == GSTCS_ERROR_NONE) gstcs_debug("End _mm_imgp_gstcs_processing [dst: %p]", *dst); @@ -777,8 +697,6 @@ static int _mm_imgp_gstcs(imgp_info_s* pImgp_info, unsigned char *src, unsigned if (ret != GSTCS_ERROR_NONE) gstcs_error("Error: _gstcs_create_default_thread is failed"); - _gstcs_destroy_image_format(input_format); - _gstcs_destroy_image_format(output_format); GSTCS_FREE(pGstreamer_s); return ret; -- 2.7.4