From 0c9c558b3ba57e7e6dbe5db2d28a8fb27a7d2614 Mon Sep 17 00:00:00 2001 From: "jiyong.min" Date: Mon, 19 Aug 2019 10:39:21 +0900 Subject: [PATCH] Relocate converting image and media_packet to reduce duplicated code. Change-Id: I075c718c46c31ceba2a0445ff77506ca053186e5 --- include/image_util_private.h | 19 ++- src/image_util.c | 303 +--------------------------------- src/image_util_decode.c | 4 +- src/image_util_encode.c | 4 +- src/image_util_private.c | 306 ++++++++++++++++++++++++++++++++++- test/image_util_testsuite.c | 376 ++++++------------------------------------- 6 files changed, 372 insertions(+), 640 deletions(-) diff --git a/include/image_util_private.h b/include/image_util_private.h index 921d13d..67277c8 100644 --- a/include/image_util_private.h +++ b/include/image_util_private.h @@ -90,10 +90,10 @@ extern "C" #define IMGCV_FUNC_NAME "mm_util_cv_extract_representative_color" #define PATH_MMUTIL_IMGCV_LIB PATH_LIBDIR"/libmmutil_imgcv.so" -#define TYPECAST_COLOR(c) convert_type_of_colorspace(c) -#define TYPECAST_COLOR_BY_TYPE(c, t) convert_type_of_colorspace_with_image_type(c, t) +#define TYPECAST_COLOR(c) _convert_type_of_colorspace(c) +#define TYPECAST_COLOR_BY_TYPE(c, t) _convert_type_of_colorspace_with_image_type(c, t) -#define NUM_OF_COLORSPACE get_number_of_colorspace() +#define NUM_OF_COLORSPACE _get_number_of_colorspace() typedef gboolean(*ModuleFunc)(void *, int, int, unsigned char *, unsigned char *, unsigned char *); @@ -203,16 +203,19 @@ typedef struct { #define IMAGE_UTIL_SUPPORT_TYPE_CHECK(value, support) \ image_util_retvm_if((value != support), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "Not supported format") -gboolean is_valid_colorspace(image_util_colorspace_e colorspace); -gboolean is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type); +gboolean _is_valid_colorspace(image_util_colorspace_e colorspace); +gboolean _is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type); -unsigned int get_number_of_colorspace(void); -int convert_type_of_colorspace(const image_util_colorspace_e colorspace); -int convert_type_of_colorspace_with_image_type(const image_util_colorspace_e colorspace, const image_util_type_e type); +unsigned int _get_number_of_colorspace(void); +int _convert_type_of_colorspace(const image_util_colorspace_e colorspace); +int _convert_type_of_colorspace_with_image_type(const image_util_colorspace_e colorspace, const image_util_type_e type); int _check_valid_file(const char *path, int mode); int _image_error_capi(int error_code); +int _image_util_image_to_packet(image_util_image_h image, media_packet_h *packet); +int _image_util_packet_to_image(media_packet_h packet, image_util_image_h *image); + /** * @} */ diff --git a/src/image_util.c b/src/image_util.c index 63f57e8..b7e6ac9 100644 --- a/src/image_util.c +++ b/src/image_util.c @@ -23,12 +23,6 @@ #include typedef struct { - mm_util_color_format_e image_format; - media_format_mimetype_e mimetype; - const char *mimetype_name; -} image_format_mimetype_pair_s; - -typedef struct { gboolean thread_stop; transformation_h transform; image_util_image_h src; @@ -37,169 +31,6 @@ typedef struct { gboolean is_transform2; } t_data_s; -static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_COLOR_NUM] = { - { MM_UTIL_COLOR_YUV420, MEDIA_FORMAT_YV12, "MEDIA_FORMAT_YV12" }, - { MM_UTIL_COLOR_YUV422, MEDIA_FORMAT_422P, "MEDIA_FORMAT_422P" }, - { MM_UTIL_COLOR_I420, MEDIA_FORMAT_I420, "MEDIA_FORMAT_I420" }, - { MM_UTIL_COLOR_NV12, MEDIA_FORMAT_NV12, "Not support" }, - { MM_UTIL_COLOR_UYVY, MEDIA_FORMAT_UYVY, "MEDIA_FORMAT_UYVY" }, - { MM_UTIL_COLOR_YUYV, MEDIA_FORMAT_YUYV, "MEDIA_FORMAT_YUYV" }, - { MM_UTIL_COLOR_RGB16, MEDIA_FORMAT_RGB565, "MEDIA_FORMAT_RGB565" }, - { MM_UTIL_COLOR_RGB24, MEDIA_FORMAT_RGB888, "MEDIA_FORMAT_RGB888" }, - { MM_UTIL_COLOR_ARGB, MEDIA_FORMAT_ARGB, "MEDIA_FORMAT_ARGB" }, - { MM_UTIL_COLOR_BGRA, MEDIA_FORMAT_BGRA, "MEDIA_FORMAT_BGRA" }, - { MM_UTIL_COLOR_RGBA, MEDIA_FORMAT_RGBA, "MEDIA_FORMAT_RGBA" }, - { MM_UTIL_COLOR_BGRX, -1, "Not support" }, - { MM_UTIL_COLOR_NV12_TILED, MEDIA_FORMAT_NV12T, "MEDIA_FORMAT_NV12T" }, - { MM_UTIL_COLOR_NV16, MEDIA_FORMAT_NV16, "MEDIA_FORMAT_NV16" }, - { MM_UTIL_COLOR_NV61, -1, "Not support" } -}; - -static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e format) -{ - unsigned int i = 0; - media_format_mimetype_e mimetype = -1; - - for (i = 0; i < MM_UTIL_COLOR_NUM; i++) { - if (image_format_mimetype_table[i].image_format == format) { - mimetype = image_format_mimetype_table[i].mimetype; - break; - } - } - - image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name); - - return mimetype; -} - -static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e mimetype) -{ - unsigned int i = 0; - mm_util_color_format_e format = -1; - - for (i = 0; i < MM_UTIL_COLOR_NUM; i++) { - if (image_format_mimetype_table[i].mimetype == mimetype) { - format = image_format_mimetype_table[i].image_format; - break; - } - } - - image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, format); - - return format; -} - -static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *format) -{ - int ret = MEDIA_FORMAT_ERROR_NONE; - media_format_h new_format = NULL; - - image_util_retvm_if((!format) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - - ret = media_format_create(&new_format); - image_util_retvm_if((ret != MEDIA_FORMAT_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_format_make_writable failed (%d)", ret); - - ret = media_format_set_video_mime(new_format, mimetype); - if (ret != MEDIA_FORMAT_ERROR_NONE) { - image_util_error("media_format_set_video_mime failed (%d)", ret); - goto ERROR; - } - - ret = media_format_set_video_width(new_format, width); - if (ret != MEDIA_FORMAT_ERROR_NONE) { - image_util_error("media_format_set_video_width failed (%d)", ret); - goto ERROR; - } - - ret = media_format_set_video_height(new_format, height); - if (ret != MEDIA_FORMAT_ERROR_NONE) { - image_util_error("media_format_set_video_height failed (%d)", ret); - goto ERROR; - } - - *format = new_format; - - return IMAGE_UTIL_ERROR_NONE; - -ERROR: - media_format_unref(new_format); - - return IMAGE_UTIL_ERROR_INVALID_OPERATION; -} - -static int __create_media_packet(media_format_h fmt, unsigned char *buffer, size_t buffer_size, media_packet_h *packet) -{ - int ret = MEDIA_FORMAT_ERROR_NONE; - media_packet_h new_packet = NULL; - unsigned int i = 0; - uint32_t plane_num = 0; - int stride_width = 0, stride_height = 0; - void *plane_ptr = NULL; - size_t plane_size = 0; - size_t buffer_offset = 0; - size_t memcpy_size = 0; - - image_util_retvm_if(!fmt, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid fmt"); - image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid new_pkt"); - image_util_retvm_if(!buffer, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid pkt_ptr"); - image_util_retvm_if(buffer_size == 0, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid pkt_size"); - - ret = media_packet_create_alloc(fmt, NULL, NULL, &new_packet); - image_util_retvm_if((ret != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_packet_create_alloc failed (%d)", ret); - - ret = media_packet_get_number_of_video_planes(new_packet, &plane_num); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); - goto ERROR; - } - - image_util_debug("plane_num: %u", plane_num); - - for (i = 0; i < plane_num; i++) { - ret = media_packet_get_video_stride_width(new_packet, i, &stride_width); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_video_stride_width failed (%d)", ret); - goto ERROR; - } - - ret = media_packet_get_video_stride_height(new_packet, i, &stride_height); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_video_stride_height failed (%d)", ret); - goto ERROR; - } - - plane_size = stride_width * stride_height; - - ret = media_packet_get_video_plane_data_ptr(new_packet, i, &plane_ptr); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_video_plane_data_ptr failed (%d)", ret); - goto ERROR; - } - - image_util_debug("stride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu", - stride_width, stride_height, plane_ptr,plane_size); - - if (buffer_offset + plane_size > buffer_size) - memcpy_size = buffer_size - buffer_offset; - else - memcpy_size = plane_size; - - memcpy(plane_ptr, buffer + buffer_offset, memcpy_size); - buffer_offset += memcpy_size; - } - - image_util_debug("Success - media_packet is created (%p)", new_packet); - - *packet = new_packet; - - return IMAGE_UTIL_ERROR_NONE; - -ERROR: - media_packet_destroy(new_packet); - - return IMAGE_UTIL_ERROR_INVALID_OPERATION; -} - static int __mm_util_transform(transformation_h handle, image_util_image_h origin, image_util_image_h *transform) { int ret = IMAGE_UTIL_ERROR_NONE; @@ -245,45 +76,7 @@ static int __mm_util_transform(transformation_h handle, image_util_image_h origi return _image_error_capi(ret); } -static int __image_util_image_to_packet(image_util_image_h image, media_packet_h *packet) -{ - int ret = IMAGE_UTIL_ERROR_NONE; - mm_util_color_format_e format = 0; - unsigned int width = 0, height = 0; - unsigned char *buffer = NULL; - size_t buffer_size = 0; - media_format_h fmt = NULL; - - image_util_fenter(); - - image_util_retvm_if(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); - image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); - - ret = mm_image_get_image(image, &width, &height, &format, &buffer, &buffer_size); - image_util_retvm_if((ret != MM_UTIL_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "mm_image_get_image failed"); - - image_util_debug("Buffer: %p buffer_size: %zu", buffer, buffer_size); - - ret = __create_media_format(__image_format_to_mimetype(format), width, height, &fmt); - if (ret != IMAGE_UTIL_ERROR_NONE) { - image_util_error("__create_media_format failed (%d)", ret); - IMAGE_UTIL_SAFE_FREE(buffer); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - ret = __create_media_packet(fmt, buffer, buffer_size, packet); - if (ret != IMAGE_UTIL_ERROR_NONE) - image_util_error("__create_media_packet failed (%d)", ret); - - media_format_unref(fmt); - IMAGE_UTIL_SAFE_FREE(buffer); - - image_util_fleave(); - - return ret; -} - -gpointer __transform_thread(gpointer data) +static gpointer __transform_thread(gpointer data) { int ret = IMAGE_UTIL_ERROR_NONE; GAsyncQueue *_queue = (GAsyncQueue *)data; @@ -335,7 +128,7 @@ gpointer __transform_thread(gpointer data) image_util_error("__mm_util_transform failed (%d)", ret); callback(NULL, ret, _t_data->user_data); } else { - ret = __image_util_image_to_packet(_transformed_image, &_transformed_packet); + ret = _image_util_image_to_packet(_transformed_image, &_transformed_packet); if (ret != IMAGE_UTIL_ERROR_NONE) { callback(NULL, ret, _t_data->user_data); } else { @@ -356,88 +149,6 @@ gpointer __transform_thread(gpointer data) return NULL; } -static int __image_util_packet_to_image(media_packet_h packet, image_util_image_h *image) -{ - int ret = IMAGE_UTIL_ERROR_NONE; - media_format_mimetype_e mimetype = 0; - int width = 0, height = 0; - media_format_h fmt = NULL; - unsigned int i = 0; - uint32_t plane_num = 0; - int stride_width = 0, stride_height = 0; - void *plane_ptr = NULL; - size_t plane_size = 0; - void *ptr = NULL; - size_t size = 0; - - image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); - image_util_retvm_if(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); - - ret = media_packet_get_format(packet, &fmt); - image_util_retvm_if((ret != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", ret); - - ret = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL); - if (ret != MEDIA_FORMAT_ERROR_NONE) { - image_util_error("media_packet_get_format failed (%d)", ret); - media_format_unref(fmt); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - media_format_unref(fmt); - - ret = media_packet_get_number_of_video_planes(packet, &plane_num); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); - goto ERROR; - } - - image_util_debug("plane_num: %u", plane_num); - - for (i = 0; i < plane_num; i++) { - ret = media_packet_get_video_stride_width(packet, i, &stride_width); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); - goto ERROR; - } - - ret = media_packet_get_video_stride_height(packet, i, &stride_height); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); - goto ERROR; - } - - plane_size = stride_width * stride_height; - - ret = media_packet_get_video_plane_data_ptr(packet, i, &plane_ptr); - if (ret != MEDIA_PACKET_ERROR_NONE) { - image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); - goto ERROR; - } - - image_util_debug("stride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu", - stride_width, stride_height, plane_ptr,plane_size); - - ptr = realloc(ptr, size + plane_size); - image_util_retvm_if(!ptr, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed"); - - memcpy(ptr + size, plane_ptr, plane_size); - size += plane_size; - } - - image_util_debug("[Fotmat: %u] W x H : %d x %d buffer(size) : %p(%zu)", mimetype, width, height, ptr, size); - - ret = mm_image_create_image(width, height, __mimetype_to_image_format(mimetype), ptr, size, image); - IMAGE_UTIL_SAFE_FREE(ptr); - image_util_retvm_if((ret != IMAGE_UTIL_ERROR_NONE), ret, "mm_image_create_image failed (%d)", ret); - - image_util_debug("__image_util_packet_to_image succeed"); - - return IMAGE_UTIL_ERROR_NONE; - -ERROR: - IMAGE_UTIL_SAFE_FREE(ptr); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; -} - static int __clone_transform(transformation_h src, transformation_h *dst) { int ret = IMAGE_UTIL_ERROR_NONE; @@ -553,7 +264,7 @@ int image_util_transform_set_colorspace(transformation_h handle, image_util_colo image_util_debug("Set colorspace_convert_info [%d]", colorspace); image_util_retvm_if((_handle == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); - image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + image_util_retvm_if(!_is_valid_colorspace(colorspace), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); _handle->color = colorspace; _handle->set_convert = true; @@ -734,8 +445,8 @@ int image_util_transform_run(transformation_h handle, media_packet_h src, image_ int ret = IMAGE_UTIL_ERROR_NONE; image_util_image_h _src = NULL; - ret = __image_util_packet_to_image(src, &_src); - image_util_retvm_if(ret != IMAGE_UTIL_ERROR_NONE, ret, "Fail to __image_util_packet_to_image"); + ret = _image_util_packet_to_image(src, &_src); + image_util_retvm_if(ret != IMAGE_UTIL_ERROR_NONE, ret, "Fail to _image_util_packet_to_image"); return __mm_util_transform_async(handle, _src, callback, user_data, FALSE); } @@ -824,7 +535,7 @@ int image_util_calculate_buffer_size(int width, int height, image_util_colorspac image_util_warning("DEPRECATION WARNING: image_util_calculate_buffer_size() is deprecated and will be removed from next release."); - image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + image_util_retvm_if(!_is_valid_colorspace(colorspace), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); image_util_retvm_if((width <= 0 || height <= 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid width or Invalid height"); image_util_retvm_if((size == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "size is null"); @@ -881,7 +592,7 @@ int image_util_foreach_supported_colorspace(image_util_type_e image_type, image_ image_util_retvm_if((callback == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "callback is null"); for (idx = (int)(NUM_OF_COLORSPACE - 1); idx >= 0; idx--) { - if (is_supported_colorspace(idx, image_type)) + if (_is_supported_colorspace(idx, image_type)) if (false == callback(idx, user_data)) return IMAGE_UTIL_ERROR_NONE; diff --git a/src/image_util_decode.c b/src/image_util_decode.c index 0007b1a..c6290b3 100755 --- a/src/image_util_decode.c +++ b/src/image_util_decode.c @@ -215,8 +215,8 @@ int image_util_decode_set_colorspace(image_util_decode_h handle, image_util_colo image_util_retvm_if(_handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); IMAGE_UTIL_TYPE_CHECK(_handle->image_type); - image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); - image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + image_util_retvm_if(!_is_valid_colorspace(colorspace), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + image_util_retvm_if(!_is_supported_colorspace(colorspace, _handle->image_type), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); _handle->colorspace = colorspace; diff --git a/src/image_util_encode.c b/src/image_util_encode.c index 5d35009..1a885a1 100644 --- a/src/image_util_encode.c +++ b/src/image_util_encode.c @@ -166,8 +166,8 @@ int image_util_encode_set_colorspace(image_util_encode_h handle, image_util_colo image_util_retvm_if(_handle == NULL, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid Handle"); IMAGE_UTIL_TYPE_CHECK(_handle->image_type); - image_util_retvm_if((is_valid_colorspace(colorspace) == FALSE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); - image_util_retvm_if((is_supported_colorspace(colorspace, _handle->image_type) == FALSE), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); + image_util_retvm_if(!_is_valid_colorspace(colorspace), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid colorspace"); + image_util_retvm_if(!_is_supported_colorspace(colorspace, _handle->image_type), IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported format"); _handle->src.color = TYPECAST_COLOR_BY_TYPE(colorspace, _handle->image_type); diff --git a/src/image_util_private.c b/src/image_util_private.c index 411752d..d8f7b9e 100755 --- a/src/image_util_private.c +++ b/src/image_util_private.c @@ -19,12 +19,37 @@ #include #include +#include #include #include #include #include #include +typedef struct { + mm_util_color_format_e image_format; + media_format_mimetype_e mimetype; + const char *mimetype_name; +} image_format_mimetype_pair_s; + +static const image_format_mimetype_pair_s image_format_mimetype_table[MM_UTIL_COLOR_NUM] = { + { MM_UTIL_COLOR_YUV420, MEDIA_FORMAT_YV12, "MEDIA_FORMAT_YV12" }, + { MM_UTIL_COLOR_YUV422, MEDIA_FORMAT_422P, "MEDIA_FORMAT_422P" }, + { MM_UTIL_COLOR_I420, MEDIA_FORMAT_I420, "MEDIA_FORMAT_I420" }, + { MM_UTIL_COLOR_NV12, MEDIA_FORMAT_NV12, "Not support" }, + { MM_UTIL_COLOR_UYVY, MEDIA_FORMAT_UYVY, "MEDIA_FORMAT_UYVY" }, + { MM_UTIL_COLOR_YUYV, MEDIA_FORMAT_YUYV, "MEDIA_FORMAT_YUYV" }, + { MM_UTIL_COLOR_RGB16, MEDIA_FORMAT_RGB565, "MEDIA_FORMAT_RGB565" }, + { MM_UTIL_COLOR_RGB24, MEDIA_FORMAT_RGB888, "MEDIA_FORMAT_RGB888" }, + { MM_UTIL_COLOR_ARGB, MEDIA_FORMAT_ARGB, "MEDIA_FORMAT_ARGB" }, + { MM_UTIL_COLOR_BGRA, MEDIA_FORMAT_BGRA, "MEDIA_FORMAT_BGRA" }, + { MM_UTIL_COLOR_RGBA, MEDIA_FORMAT_RGBA, "MEDIA_FORMAT_RGBA" }, + { MM_UTIL_COLOR_BGRX, -1, "Not support" }, + { MM_UTIL_COLOR_NV12_TILED, MEDIA_FORMAT_NV12T, "MEDIA_FORMAT_NV12T" }, + { MM_UTIL_COLOR_NV16, MEDIA_FORMAT_NV16, "MEDIA_FORMAT_NV16" }, + { MM_UTIL_COLOR_NV61, -1, "Not support" } +}; + static int _convert_colorspace_tbl[] = { MM_UTIL_COLOR_YUV420, /* IMAGE_UTIL_COLORSPACE_YUV420 */ MM_UTIL_COLOR_YUV422, /* IMAGE_UTIL_COLORSPACE_YUV422 */ @@ -115,12 +140,12 @@ static int _convert_bmp_colorspace_tbl[] = { _NOT_SUPPORTED_COLORSPACE, /* IMAGE_UTIL_COLORSPACE_NV61 */ }; -unsigned int get_number_of_colorspace(void) +unsigned int _get_number_of_colorspace(void) { return (unsigned int)(sizeof(_convert_colorspace_tbl)/sizeof(int)); } -gboolean is_valid_colorspace(image_util_colorspace_e colorspace) +gboolean _is_valid_colorspace(image_util_colorspace_e colorspace) { if ((colorspace < IMAGE_UTIL_COLORSPACE_YV12) || (colorspace >= NUM_OF_COLORSPACE)) return FALSE; @@ -128,7 +153,7 @@ gboolean is_valid_colorspace(image_util_colorspace_e colorspace) return TRUE; } -gboolean is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type) +gboolean _is_supported_colorspace(image_util_colorspace_e colorspace, image_util_type_e type) { switch (type) { case IMAGE_UTIL_JPEG: @@ -149,22 +174,22 @@ gboolean is_supported_colorspace(image_util_colorspace_e colorspace, image_util_ return TRUE; } -int convert_type_of_colorspace(const image_util_colorspace_e colorspace) +int _convert_type_of_colorspace(const image_util_colorspace_e colorspace) { - if (is_valid_colorspace(colorspace) == FALSE) + if (!_is_valid_colorspace(colorspace)) return _NOT_SUPPORTED_COLORSPACE; return _convert_colorspace_tbl[colorspace]; } -int convert_type_of_colorspace_with_image_type(const image_util_colorspace_e colorspace, const image_util_type_e type) +int _convert_type_of_colorspace_with_image_type(const image_util_colorspace_e colorspace, const image_util_type_e type) { int new_colorspace = _NOT_SUPPORTED_COLORSPACE; - if (is_valid_colorspace(colorspace) == FALSE) + if (!_is_valid_colorspace(colorspace)) return _NOT_SUPPORTED_COLORSPACE; - if (is_supported_colorspace(colorspace, type) == FALSE) + if (!_is_supported_colorspace(colorspace, type)) return _NOT_SUPPORTED_COLORSPACE; switch (type) { @@ -243,3 +268,268 @@ int _image_error_capi(int error_code) return IMAGE_UTIL_ERROR_INVALID_OPERATION; } + +static media_format_mimetype_e __image_format_to_mimetype(mm_util_color_format_e format) +{ + unsigned int i = 0; + + for (i = 0; i < MM_UTIL_COLOR_NUM; i++) { + if (image_format_mimetype_table[i].image_format == format) { + image_util_debug("imgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name); + return image_format_mimetype_table[i].mimetype; + } + } + + image_util_error("imgp fmt: %d", format); + + return MEDIA_FORMAT_MAX; +} + +static mm_util_color_format_e __mimetype_to_image_format(media_format_mimetype_e mimetype) +{ + unsigned int i = 0; + + for (i = 0; i < MM_UTIL_COLOR_NUM; i++) { + if (image_format_mimetype_table[i].mimetype == mimetype) { + image_util_debug("mimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, image_format_mimetype_table[i].image_format); + return image_format_mimetype_table[i].image_format; + } + } + + image_util_error("mimetype: %s", image_format_mimetype_table[i].mimetype_name); + + return MM_UTIL_COLOR_NUM; +} + +static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *format) +{ + int ret = MEDIA_FORMAT_ERROR_NONE; + media_format_h new_format = NULL; + + image_util_retvm_if((!format) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); + + ret = media_format_create(&new_format); + image_util_retvm_if((ret != MEDIA_FORMAT_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_format_make_writable failed (%d)", ret); + + ret = media_format_set_video_mime(new_format, mimetype); + if (ret != MEDIA_FORMAT_ERROR_NONE) { + image_util_error("media_format_set_video_mime failed (%d)", ret); + goto ERROR; + } + + ret = media_format_set_video_width(new_format, width); + if (ret != MEDIA_FORMAT_ERROR_NONE) { + image_util_error("media_format_set_video_width failed (%d)", ret); + goto ERROR; + } + + ret = media_format_set_video_height(new_format, height); + if (ret != MEDIA_FORMAT_ERROR_NONE) { + image_util_error("media_format_set_video_height failed (%d)", ret); + goto ERROR; + } + + *format = new_format; + + return IMAGE_UTIL_ERROR_NONE; + +ERROR: + media_format_unref(new_format); + + return IMAGE_UTIL_ERROR_INVALID_OPERATION; +} + +static int __create_media_packet(media_format_h fmt, unsigned char *buffer, size_t buffer_size, media_packet_h *packet) +{ + int ret = MEDIA_FORMAT_ERROR_NONE; + media_packet_h new_packet = NULL; + unsigned int i = 0; + uint32_t plane_num = 0; + int stride_width = 0, stride_height = 0; + void *plane_ptr = NULL; + size_t plane_size = 0; + size_t buffer_offset = 0; + size_t memcpy_size = 0; + + image_util_retvm_if(!fmt, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid fmt"); + image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); + image_util_retvm_if(!buffer, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid buffer"); + image_util_retvm_if(buffer_size == 0, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid buffer_size"); + + ret = media_packet_create_alloc(fmt, NULL, NULL, &new_packet); + image_util_retvm_if((ret != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_packet_create_alloc failed (%d)", ret); + + ret = media_packet_get_number_of_video_planes(new_packet, &plane_num); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); + goto ERROR; + } + + image_util_debug("plane_num: %u", plane_num); + + for (i = 0; i < plane_num; i++) { + ret = media_packet_get_video_stride_width(new_packet, i, &stride_width); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_stride_width failed (%d)", ret); + goto ERROR; + } + + ret = media_packet_get_video_stride_height(new_packet, i, &stride_height); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_stride_height failed (%d)", ret); + goto ERROR; + } + + plane_size = stride_width * stride_height; + + ret = media_packet_get_video_plane_data_ptr(new_packet, i, &plane_ptr); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_plane_data_ptr failed (%d)", ret); + goto ERROR; + } + + image_util_debug("stride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu", + stride_width, stride_height, plane_ptr,plane_size); + + if (buffer_offset + plane_size > buffer_size) + memcpy_size = buffer_size - buffer_offset; + else + memcpy_size = plane_size; + + memcpy(plane_ptr, buffer + buffer_offset, memcpy_size); + buffer_offset += memcpy_size; + } + + image_util_debug("Success - media_packet is created (%p)", new_packet); + + *packet = new_packet; + + return IMAGE_UTIL_ERROR_NONE; + +ERROR: + media_packet_destroy(new_packet); + + return IMAGE_UTIL_ERROR_INVALID_OPERATION; +} + +int _image_util_image_to_packet(image_util_image_h image, media_packet_h *packet) +{ + int ret = IMAGE_UTIL_ERROR_NONE; + mm_util_color_format_e format = 0; + unsigned int width = 0, height = 0; + unsigned char *buffer = NULL; + size_t buffer_size = 0; + media_format_h fmt = NULL; + + image_util_fenter(); + + image_util_retvm_if(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); + image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); + + ret = mm_image_get_image(image, &width, &height, &format, &buffer, &buffer_size); + image_util_retvm_if((ret != MM_UTIL_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "mm_image_get_image failed"); + + image_util_debug("Buffer: %p buffer_size: %zu", buffer, buffer_size); + + ret = __create_media_format(__image_format_to_mimetype(format), width, height, &fmt); + if (ret != IMAGE_UTIL_ERROR_NONE) { + image_util_error("__create_media_format failed (%d)", ret); + IMAGE_UTIL_SAFE_FREE(buffer); + return IMAGE_UTIL_ERROR_INVALID_OPERATION; + } + + ret = __create_media_packet(fmt, buffer, buffer_size, packet); + if (ret != IMAGE_UTIL_ERROR_NONE) + image_util_error("__create_media_packet failed (%d)", ret); + + media_format_unref(fmt); + IMAGE_UTIL_SAFE_FREE(buffer); + + image_util_fleave(); + + return ret; +} + +int _image_util_packet_to_image(media_packet_h packet, image_util_image_h *image) +{ + int ret = IMAGE_UTIL_ERROR_NONE; + media_format_mimetype_e mimetype = 0; + int width = 0, height = 0; + media_format_h fmt = NULL; + unsigned int i = 0; + uint32_t plane_num = 0; + int stride_width = 0, stride_height = 0; + void *plane_ptr = NULL; + size_t plane_size = 0; + void *ptr = NULL; + size_t size = 0; + + image_util_fenter(); + + image_util_retvm_if(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); + image_util_retvm_if(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); + + ret = media_packet_get_format(packet, &fmt); + image_util_retvm_if((ret != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", ret); + + ret = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL); + if (ret != MEDIA_FORMAT_ERROR_NONE) { + image_util_error("media_packet_get_format failed (%d)", ret); + media_format_unref(fmt); + return IMAGE_UTIL_ERROR_INVALID_PARAMETER; + } + media_format_unref(fmt); + + ret = media_packet_get_number_of_video_planes(packet, &plane_num); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_number_of_video_plances failed (%d)", ret); + goto ERROR; + } + + image_util_debug("plane_num: %u", plane_num); + + for (i = 0; i < plane_num; i++) { + ret = media_packet_get_video_stride_width(packet, i, &stride_width); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_stride_width failed (%d)", ret); + goto ERROR; + } + + ret = media_packet_get_video_stride_height(packet, i, &stride_height); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_stride_height failed (%d)", ret); + goto ERROR; + } + + plane_size = stride_width * stride_height; + + ret = media_packet_get_video_plane_data_ptr(packet, i, &plane_ptr); + if (ret != MEDIA_PACKET_ERROR_NONE) { + image_util_error("media_packet_get_video_plane_data_ptr failed (%d)", ret); + goto ERROR; + } + + image_util_debug("stride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu", + stride_width, stride_height, plane_ptr,plane_size); + + ptr = realloc(ptr, size + plane_size); + image_util_retvm_if(!ptr, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed"); + + memcpy(ptr + size, plane_ptr, plane_size); + size += plane_size; + } + + image_util_debug("[Fotmat: %u] W x H : %d x %d buffer(size) : %p(%zu)", mimetype, width, height, ptr, size); + + ret = mm_image_create_image(width, height, __mimetype_to_image_format(mimetype), ptr, size, image); + IMAGE_UTIL_SAFE_FREE(ptr); + image_util_retvm_if((ret != IMAGE_UTIL_ERROR_NONE), ret, "mm_image_create_image failed (%d)", ret); + + image_util_fleave(); + + return IMAGE_UTIL_ERROR_NONE; + +ERROR: + IMAGE_UTIL_SAFE_FREE(ptr); + return IMAGE_UTIL_ERROR_INVALID_OPERATION; +} diff --git a/test/image_util_testsuite.c b/test/image_util_testsuite.c index 490cd58..be4800a 100644 --- a/test/image_util_testsuite.c +++ b/test/image_util_testsuite.c @@ -21,15 +21,18 @@ #include #include #include +#include /* logs for testsuite */ -#define VERSION 0.1 -#define DEBUG_QUEUE 0 -#define DUMP_RAW_IMAGE 0 +#define VERSION "0.1.1 of 16 Aug 2019" +/* prints log of menu-queue for testsuite */ +#define DEBUG_QUEUE 0 /* 0:disable, 1:enable */ +/* saves the intermediate image to process name("transform" or "decode"), Not supported for encoding */ +#define DUMP_RAW_IMAGE 0 /* 0:disable, 1:enable */ /* commons for testsuite */ #define MAX_STRING_LEN PATH_MAX -#define MAX_AGIF_CNT 100 +#define MAX_AGIF_CNT 100 /* supports MAX_AGIF_CNT agif frames */ #define IMAGE_UTIL_COLOR_NUM IMAGE_UTIL_COLORSPACE_NV61 + 1 #define SAFE_FREE(src) { if (src) {free(src); src = NULL; } } @@ -97,34 +100,34 @@ typedef struct { } test_size_s; typedef struct { - /* for input */ + /* for input path or dir */ char *path; + /* for buffer test */ gboolean has_buffer; unsigned char *buffer; size_t buffer_size; + /* for raw image test */ gboolean is_raw; test_size_s size; image_util_colorspace_e color; - /* for result of test or pass through */ + /* intermediate or result image for test */ image_util_image_h image; - /* for a-gif */ + /* image count and handles for a-gif */ unsigned int image_cnt; image_util_image_h images[MAX_AGIF_CNT]; } test_complex_data_s; -typedef struct { - test_complex_data_s *cdata; -} test_queue_data_s; - +/* for decode parameters */ typedef struct { image_util_colorspace_e color; image_util_scale_e downscale; } test_decode_params_s; +/* for encode parameters */ typedef struct { image_util_type_e type; int quality; @@ -132,11 +135,13 @@ typedef struct { gboolean use_buffer; } test_encode_params_s; +/* for encode a-gif parameters */ typedef struct { int delay; gboolean use_buffer; } test_encode_agif_params_s; +/* for transform parameters */ typedef struct { gboolean has_convert_color; image_util_colorspace_e convert_color; @@ -148,9 +153,12 @@ typedef struct { image_util_rotation_e rotation; } test_transform_params_s; +/* for current menu to display */ static test_menu_state_e g_current_menu = MAIN_MENU; +/* for previous menu to go back */ static test_menu_state_e g_previous_menu = MAIN_MENU; +/* for input data to process */ static test_complex_data_s g_input_data; /* decode */ @@ -159,7 +167,6 @@ static test_complex_data_s g_decode_result; /* encode */ static test_encode_params_s g_encode_params; static test_complex_data_s g_encode_result; - /* encode a-gif */ static test_encode_agif_params_s g_encode_agif_params; static test_complex_data_s g_encode_agif_result; @@ -167,8 +174,11 @@ static test_complex_data_s g_encode_agif_result; static test_transform_params_s g_transform_params; static test_complex_data_s g_transform_result; +/* for menu queue */ static GQueue *g_queue_menu = NULL; +/* for images queue to encode a-gif */ static GQueue *g_queue_agif_images = NULL; +/* for input data queue */ static GQueue *g_queue_data = NULL; static GMainLoop *g_mainloop = NULL; @@ -209,7 +219,7 @@ static void __display_set_raw_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Set raw data menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Set raw data menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < RAW_IMG_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_raw_img_menu[idx].e, g_disp_raw_img_menu[idx].t); @@ -252,7 +262,7 @@ static void __display_decode_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Decode menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Decode menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < DECODE_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_decode_menu[idx].e, g_disp_decode_menu[idx].t); @@ -299,7 +309,7 @@ static void __display_encode_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Encode menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Encode menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < ENCODE_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_encode_menu[idx].e, g_disp_encode_menu[idx].t); @@ -340,7 +350,7 @@ static void __display_encode_agif_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Encode A-GIF menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Encode A-GIF menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < ENCODE_AGIF_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_encode_agif_menu[idx].e, g_disp_encode_agif_menu[idx].t); @@ -381,7 +391,7 @@ static void __display_transform_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Transform menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Transform menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < TRANSFORM_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_transform_menu[idx].e, g_disp_transform_menu[idx].t); @@ -420,7 +430,7 @@ static void __display_main_menu(void) unsigned int idx = 0; g_print("\n"); g_print("====================================================\n"); - g_print(" image-util Core-API test: Main menu v%2.1f\n", VERSION); + g_print(" image-util Core-API test: Main menu v%s\n", VERSION); g_print("----------------------------------------------------\n"); for (idx = 0; idx < MAIN_MENU_MAX; idx++) g_print("%u. %s\n", g_disp_main_menu[idx].e, g_disp_main_menu[idx].t); @@ -462,29 +472,29 @@ static void __display_menu(test_menu_state_e menu_state) case SET_COLOR_MENU: case SET_RAW_COLOR_MENU: case SET_CONVERT_MENU: - __display_set("colorspace", "10"); + __display_set("colorspace", "i420:2, rgb:7, argb:8, rgba:10"); break; case SET_COMPRESSION_MENU: - __display_set("comprssion", "0~9"); + __display_set("comprssion", "0(NoC/HighQ)~9(MaxC/LowQ)"); break; case SET_CROP_MENU: - __display_set("crop", "(sx)x(sy)x(w)x(h), 0x0x1920x1080"); + __display_set("crop", "(sx)x(sy)x(w)x(h) 0x0x1920x1080"); break; case SET_DELAY_MENU: __display_set("delay", "20~"); break; case SET_DOWNSCALE_MENU: - __display_set("downscale", "0~3"); + __display_set("downscale", "0(1/1)~3(1/8)"); break; case SET_QUALITY_MENU: - __display_set("quality", "1~100"); + __display_set("quality", "1(LowQ)~100(HighQ)"); break; case SET_RESIZE_MENU: case SET_SIZE_MENU: __display_set("size", "1920x1080"); break; case SET_ROTATE_MENU: - __display_set("rotation", "0=none, 1=90, 2=180, 3=270"); + __display_set("rotation", "0:none, 1:90, 2:180, 3:270"); break; case SET_TYPE_MENU: __display_set("image_type", "0:jpeg, 1:png, 2:gif, 3:bmp"); @@ -753,6 +763,7 @@ static gboolean __set_input_dir(const char *path) g_queue_free_full(g_queue_agif_images, __queue_free_func); + /* read files in dir */ while ((dp = readdir(fd)) != NULL) { if (strlen(dp->d_name) == 0) continue; @@ -772,6 +783,7 @@ static gboolean __set_input_dir(const char *path) /* sort files */ g_queue_sort(g_queue_agif_images, __sort_compare, NULL); + /* decode files of dir */ if (!__decode_file_in_dir()) { g_print("Fail to decode file from dir! %s\n", path); return FALSE; @@ -809,7 +821,7 @@ static gboolean __set_input_crop_area(const char *data, test_size_s *size) if (data == NULL || strlen(data) == 0) return FALSE; - params = g_strsplit(data, "x", 0); + params = g_strsplit(data, ",", 0); if (params == NULL || params[0] == NULL || params[1] == NULL || params[2] == NULL || params[3] == NULL) return FALSE; @@ -947,293 +959,6 @@ static void __set_encode_file(image_util_type_e image_type, const char *base, ch *path = g_strdup_printf("%s.%s", base, ext); } - -typedef struct { - image_util_colorspace_e image_format; - media_format_mimetype_e mimetype; - const char *mimetype_name; -} image_format_mimetype_pair_s; - -static const image_format_mimetype_pair_s image_format_mimetype_table[IMAGE_UTIL_COLOR_NUM] = { - { IMAGE_UTIL_COLORSPACE_YV12, MEDIA_FORMAT_YV12, "MEDIA_FORMAT_YV12" }, - { IMAGE_UTIL_COLORSPACE_YUV422, MEDIA_FORMAT_422P, "MEDIA_FORMAT_422P" }, - { IMAGE_UTIL_COLORSPACE_I420, MEDIA_FORMAT_I420, "MEDIA_FORMAT_I420" }, - { IMAGE_UTIL_COLORSPACE_NV12, MEDIA_FORMAT_NV12, "Not support" }, - { IMAGE_UTIL_COLORSPACE_UYVY, MEDIA_FORMAT_UYVY, "MEDIA_FORMAT_UYVY" }, - { IMAGE_UTIL_COLORSPACE_YUYV, MEDIA_FORMAT_YUYV, "MEDIA_FORMAT_YUYV" }, - { IMAGE_UTIL_COLORSPACE_RGB565, MEDIA_FORMAT_RGB565, "MEDIA_FORMAT_RGB565" }, - { IMAGE_UTIL_COLORSPACE_RGB888, MEDIA_FORMAT_RGB888, "MEDIA_FORMAT_RGB888" }, - { IMAGE_UTIL_COLORSPACE_ARGB8888, MEDIA_FORMAT_ARGB, "MEDIA_FORMAT_ARGB" }, - { IMAGE_UTIL_COLORSPACE_BGRA8888, MEDIA_FORMAT_BGRA, "MEDIA_FORMAT_BGRA" }, - { IMAGE_UTIL_COLORSPACE_RGBA8888, MEDIA_FORMAT_RGBA, "MEDIA_FORMAT_RGBA" }, - { IMAGE_UTIL_COLORSPACE_BGRX8888, -1, "Not support" }, - { IMAGE_UTIL_COLORSPACE_NV21, MEDIA_FORMAT_NV12T, "MEDIA_FORMAT_NV12T" }, - { IMAGE_UTIL_COLORSPACE_NV16, MEDIA_FORMAT_NV16, "MEDIA_FORMAT_NV16" }, - { IMAGE_UTIL_COLORSPACE_NV61, -1, "Not support" } -}; - -static media_format_mimetype_e __image_format_to_mimetype(image_util_colorspace_e format) -{ - unsigned int i = 0; - media_format_mimetype_e mimetype = -1; - - for (i = 0; i < IMAGE_UTIL_COLOR_NUM; i++) { - if (image_format_mimetype_table[i].image_format == format) { - mimetype = image_format_mimetype_table[i].mimetype; - break; - } - } - - g_print("\t\timgp fmt: %d mimetype fmt: %s", format, image_format_mimetype_table[i].mimetype_name); - - return mimetype; -} - -static image_util_colorspace_e __mimetype_to_image_format(media_format_mimetype_e mimetype) -{ - unsigned int i = 0; - image_util_colorspace_e format = -1; - - for (i = 0; i < IMAGE_UTIL_COLOR_NUM; i++) { - if (image_format_mimetype_table[i].mimetype == mimetype) { - format = image_format_mimetype_table[i].image_format; - break; - } - } - - g_print("\t\tmimetype: %s imgp fmt: %d", image_format_mimetype_table[i].mimetype_name, format); - - return format; -} - -static int __create_media_format(media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *new_fmt) -{ - int err = MEDIA_FORMAT_ERROR_NONE; - - RETVM_IF((new_fmt == NULL) || (width == 0) || (height == 0), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - - err = media_format_create(new_fmt); - RETVM_IF((err != MEDIA_FORMAT_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_OPERATION, "media_format_make_writable failed (%d)", err); - - err = media_format_set_video_mime(*new_fmt, mimetype); - if (err != MEDIA_FORMAT_ERROR_NONE) { - media_format_unref(*new_fmt); - g_print("\tmedia_format_set_video_mime failed (%d)\n", err); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - err = media_format_set_video_width(*new_fmt, width); - if (err != MEDIA_FORMAT_ERROR_NONE) { - media_format_unref(*new_fmt); - g_print("\tmedia_format_set_video_width failed (%d)\n", err); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - err = media_format_set_video_height(*new_fmt, height); - if (err != MEDIA_FORMAT_ERROR_NONE) { - media_format_unref(*new_fmt); - g_print("\tmedia_format_set_video_height failed (%d)\n", err); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - return IMAGE_UTIL_ERROR_NONE; -} - -static int __create_media_packet(media_format_h fmt, unsigned char *buffer, size_t buffer_size, media_packet_h *packet) -{ - int ret = MEDIA_FORMAT_ERROR_NONE; - unsigned int i = 0; - uint32_t plane_num = 0; - int stride_width = 0, stride_height = 0; - void *plane_ptr = NULL; - size_t plane_size = 0; - size_t buffer_offset = 0; - size_t memcpy_size = 0; - - RETVM_IF(!fmt, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid fmt"); - RETVM_IF(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid new_pkt"); - RETVM_IF(!buffer, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid pkt_ptr"); - RETVM_IF(buffer_size == 0, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid pkt_size"); - - ret = media_packet_create_alloc(fmt, NULL, NULL, packet); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_create_from_external_memory failed (%d)\n", ret); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - ret = media_packet_get_number_of_video_planes(*packet, &plane_num); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_number_of_video_plances failed (%d)\n", ret); - ret = IMAGE_UTIL_ERROR_INVALID_OPERATION; - goto ERROR; - } - - g_print("\tplane_num: %u\n", plane_num); - - for (i = 0; i < plane_num; i++) { - ret = media_packet_get_video_stride_width(*packet, i, &stride_width); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_video_stride_width failed (%d)\n", ret); - ret = IMAGE_UTIL_ERROR_INVALID_OPERATION; - goto ERROR; - } - - ret = media_packet_get_video_stride_height(*packet, i, &stride_height); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_video_stride_height failed (%d)\n", ret); - ret = IMAGE_UTIL_ERROR_INVALID_OPERATION; - goto ERROR; - } - - plane_size = stride_width * stride_height; - - ret = media_packet_get_video_plane_data_ptr(*packet, i, &plane_ptr); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_video_plane_data_ptr failed (%d)\n", ret); - ret = IMAGE_UTIL_ERROR_INVALID_OPERATION; - goto ERROR; - } - - g_print("\tstride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu\n", - stride_width, stride_height, plane_ptr,plane_size); - - if (buffer_offset + plane_size > buffer_size) - memcpy_size = buffer_size - buffer_offset; - else - memcpy_size = plane_size; - - memcpy(plane_ptr, buffer + buffer_offset, memcpy_size); - buffer_offset += memcpy_size; - } - - g_print("\tSuccess - media_packet is created (%p)\n", *packet); - - return IMAGE_UTIL_ERROR_NONE; - -ERROR: - if (*packet != NULL) - media_packet_destroy(*packet); - *packet = NULL; - - return ret; -} - -static int __convert_image_to_packet(image_util_image_h image, media_packet_h *packet) -{ - int ret = IMAGE_UTIL_ERROR_NONE; - image_util_colorspace_e color = 0; - unsigned int width = 0, height = 0; - unsigned char *buffer = NULL; - size_t buffer_size = 0; - media_format_h fmt = NULL; - - RETVM_IF(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); - RETVM_IF(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); - - ret = image_util_get_image(image, &width, &height, &color, &buffer, &buffer_size); - RETVM_IF((ret != IMAGE_UTIL_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "mm_image_get_image failed"); - - g_print("\tBuffer: %p buffer_size: %zu\n", buffer, buffer_size); - - ret = __create_media_format(__image_format_to_mimetype(color), width, height, &fmt); - if (ret != IMAGE_UTIL_ERROR_NONE) { - g_print("\t__create_media_format failed (%d)\n", ret); - SAFE_FREE(buffer); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; - } - - ret = __create_media_packet(fmt, buffer, buffer_size, packet); - if (ret != MEDIA_PACKET_ERROR_NONE) - g_print("\t__create_media_packet failed (%d)\n", ret); - - media_format_unref(fmt); - SAFE_FREE(buffer); - - return ret; -} - -static int __convert_packet_to_image(media_packet_h packet, image_util_image_h *image) -{ - int ret = IMAGE_UTIL_ERROR_NONE; - media_format_mimetype_e mimetype = 0; - int width = 0, height = 0; - media_format_h fmt = NULL; - unsigned int i = 0; - uint32_t plane_num = 0; - int stride_width = 0, stride_height = 0; - void *plane_ptr = NULL; - size_t plane_size = 0; - void *ptr = NULL; - size_t size = 0; - - RETVM_IF(!packet, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid packet"); - RETVM_IF(!image, IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid image"); - - ret = media_packet_get_format(packet, &fmt); - RETVM_IF((ret != MEDIA_PACKET_ERROR_NONE), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "media_packet_get_format failed (%d)", ret); - - ret = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL); - if (ret != MEDIA_FORMAT_ERROR_NONE) { - g_print("\tmedia_packet_get_format failed (%d)\n", ret); - media_format_unref(fmt); - return IMAGE_UTIL_ERROR_INVALID_PARAMETER; - } - media_format_unref(fmt); - - ret = media_packet_get_number_of_video_planes(packet, &plane_num); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_number_of_video_plances failed (%d)\n", ret); - goto ERROR; - } - - g_print("\tplane_num: %u", plane_num); - - for (i = 0; i < plane_num; i++) { - ret = media_packet_get_video_stride_width(packet, i, &stride_width); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_number_of_video_plances failed (%d)\n", ret); - goto ERROR; - } - - ret = media_packet_get_video_stride_height(packet, i, &stride_height); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_number_of_video_plances failed (%d)\n", ret); - goto ERROR; - } - - plane_size = stride_width * stride_height; - - ret = media_packet_get_video_plane_data_ptr(packet, i, &plane_ptr); - if (ret != MEDIA_PACKET_ERROR_NONE) { - g_print("\tmedia_packet_get_number_of_video_plances failed (%d)\n", ret); - goto ERROR; - } - - g_print("\tstride_width: %d, stride_height: %d, plane_ptr: %p, plane_size: %zu\n", - stride_width, stride_height, plane_ptr,plane_size); - - ptr = realloc(ptr, size + plane_size); - RETVM_IF(!ptr, IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Memory allocation failed"); - - memcpy(ptr + size, plane_ptr, plane_size); - size += plane_size; - } - - g_print("\t[Fotmat: %u] W x H : %d x %d, buffer(size) : %p(%zu)\n", mimetype, width, height, ptr, size); - - ret = image_util_create_image(width, height, __mimetype_to_image_format(mimetype), ptr, size, image); - SAFE_FREE(ptr); - RETVM_IF((ret != IMAGE_UTIL_ERROR_NONE), ret, "mm_image_create_image failed (%d)", ret); - - g_print("\t__image_util_packet_to_image succeed\n"); - - return IMAGE_UTIL_ERROR_NONE; - -ERROR: - SAFE_FREE(ptr); - return IMAGE_UTIL_ERROR_INVALID_OPERATION; -} - /* internal functions for testsuite */ @@ -1942,9 +1667,9 @@ static int __run_transform(test_transform_menu_e menu, test_complex_data_s *inpu switch(menu) { case TRANSFORM_RUN: - ret = __convert_image_to_packet(input->image, &mpacket); + ret = _image_util_image_to_packet(input->image, &mpacket); if (ret != IMAGE_UTIL_ERROR_NONE) { - g_print("__convert_image_to_packet failed %d\n", ret); + g_print("_image_util_image_to_packet failed %d\n", ret); break; } @@ -1955,9 +1680,9 @@ static int __run_transform(test_transform_menu_e menu, test_complex_data_s *inpu } __wait(); - ret = __convert_packet_to_image(res_mpacket, &result->image); + ret = _image_util_packet_to_image(res_mpacket, &result->image); if (ret != IMAGE_UTIL_ERROR_NONE) { - g_print("__convert_packet_to_image failed %d\n", ret); + g_print("_image_util_packet_to_image failed %d\n", ret); break; } break; @@ -2185,11 +1910,13 @@ static void __menu_move(test_menu_state_e new_menu) g_print("[menu move] %u -> %u\n", g_current_menu, new_menu); #endif + /* push current menu into queue */ data->menu_state = g_current_menu; g_queue_push_tail(g_queue_menu, data); g_previous_menu = g_current_menu; + /* go to new menu */ g_current_menu = new_menu; } @@ -2198,9 +1925,11 @@ static void __menu_back(void) test_queue_menu_s *data = NULL; if (g_queue_is_empty(g_queue_menu)) { + g_print("empty previous menu!\n\n"); return; } + /* pop previous menu from queue */ data = g_queue_pop_tail(g_queue_menu); if (!data) { g_print("wrong menu data!\n\n"); @@ -2213,6 +1942,7 @@ static void __menu_back(void) g_previous_menu = g_current_menu; + /* go to previous menu */ g_current_menu = data->menu_state; SAFE_FREE(data); @@ -2411,8 +2141,10 @@ static gboolean __interpret_set_menu(test_menu_state_e menu_state, const char *c case SET_BUFFER_MENU: if (g_ascii_strcasecmp(cmd, "y") == 0) { SAFE_FREE(g_input_data.buffer); - if (__read_file(g_input_data.path, (void **)&g_input_data.buffer, &g_input_data.buffer_size)) + if (__read_file(g_input_data.path, (void **)&g_input_data.buffer, &g_input_data.buffer_size)) { g_input_data.has_buffer = TRUE; + g_print("Success to read file(%s), read_size(%zu)\n", g_input_data.path, g_input_data.buffer_size); + } } break; case SET_TO_BUFFER_MENU: @@ -2583,7 +2315,8 @@ static void __interpret_cmd(char *cmd) case SET_PATH_MENU: if (!__set_input_path(cmd)) { g_print("[Warn] Fail to set input path! %s\n", cmd); - goto ERROR; + __menu_back(); + break; } __menu_back(); if (IS_RAW_FILE(g_input_data.path)) { @@ -2638,11 +2371,6 @@ static void __interpret_cmd(char *cmd) __display_menu(g_current_menu); return; - -ERROR: - __menu_back(); - __display_menu(g_current_menu); - return; } /* interprete menu */ @@ -2746,7 +2474,7 @@ static gboolean _input_func(GIOChannel *channel) static void _display_help(const char *cmd) { g_print("====================================================\n"); - g_print("[image-util CAPI test: v%2.1f\n", VERSION); + g_print("[image-util CAPI test: v%s\n", VERSION); g_print("%s {file_path} {test_mode} [for jpg/png/gif/bmp]\n", cmd); g_print("%s {directory} {test_mode} [for a-gif]\n", cmd); g_print("{test_mode} 0:auto, 1:decode, 2:encode, 3:a-gif, 4:transform\n"); -- 2.7.4