Replace the interface of libmm-utility from media_packet_h to mm_util_color_image_h 04/161204/3 submit/tizen/20171123.062739 submit/tizen/20171124.024723
authorJiyong Min <jiyong.min@samsung.com>
Wed, 22 Nov 2017 06:01:10 +0000 (15:01 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Wed, 22 Nov 2017 06:52:55 +0000 (15:52 +0900)
Change-Id: I30b701dad2b23227a9d5078384af7e99a0d492ae
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
include/image_util_private.h
src/image_util.c

index 51753d5..30f3091 100755 (executable)
@@ -87,7 +87,7 @@ typedef gboolean(*ModuleFunc)(void *, int, int, unsigned char *, unsigned char *
 typedef struct {
        void *user_data;
        media_packet_h dst;
-       image_util_transform_completed_cb image_processing_completed_cb;
+       image_util_transform_completed_cb completed_cb;
 } image_util_cb_s;
 
 typedef struct {
index 49a7b06..82bd7b3 100755 (executable)
 * limitations under the License.
 */
 
+#include <inttypes.h>
 #include <mm_util_imgp.h>
 
 #include <image_util.h>
 #include <image_util_private.h>
 
-static void _image_util_transform_completed_cb(media_packet_h * dst, int error, void *user_data)
+typedef struct {
+       mm_util_img_format 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_IMG_FMT_NUM] = {
+       { MM_UTIL_IMG_FMT_YUV420,       MEDIA_FORMAT_YV12,              "MEDIA_FORMAT_YV12" },
+       { MM_UTIL_IMG_FMT_YUV422,       MEDIA_FORMAT_422P,              "MEDIA_FORMAT_422P" },
+       { MM_UTIL_IMG_FMT_I420,         MEDIA_FORMAT_I420,              "MEDIA_FORMAT_I420" },
+       { MM_UTIL_IMG_FMT_NV12,         -1,                                             "Not support" },
+       { MM_UTIL_IMG_FMT_UYVY,         MEDIA_FORMAT_UYVY,              "MEDIA_FORMAT_UYVY" },
+       { MM_UTIL_IMG_FMT_YUYV,         MEDIA_FORMAT_YUYV,              "MEDIA_FORMAT_YUYV" },
+       { MM_UTIL_IMG_FMT_RGB565,       MEDIA_FORMAT_RGB565,    "MEDIA_FORMAT_RGB565" },
+       { MM_UTIL_IMG_FMT_RGB888,       MEDIA_FORMAT_RGB888,    "MEDIA_FORMAT_RGB888" },
+       { MM_UTIL_IMG_FMT_ARGB8888,     MEDIA_FORMAT_ARGB,              "MEDIA_FORMAT_ARGB" },
+       { MM_UTIL_IMG_FMT_BGRA8888,     MEDIA_FORMAT_BGRA,              "MEDIA_FORMAT_BGRA" },
+       { MM_UTIL_IMG_FMT_RGBA8888,     MEDIA_FORMAT_RGBA,              "MEDIA_FORMAT_RGBA" },
+       { MM_UTIL_IMG_FMT_BGRX8888,     -1,                                             "Not support" },
+       { MM_UTIL_IMG_FMT_NV12_TILED,   MEDIA_FORMAT_NV12T,     "MEDIA_FORMAT_NV12T" },
+       { MM_UTIL_IMG_FMT_NV16,         MEDIA_FORMAT_NV16,              "MEDIA_FORMAT_NV16" },
+       { MM_UTIL_IMG_FMT_NV61,         -1,                                             "Not support" }
+};
+
+static media_format_mimetype_e __image_format_to_mimetype(mm_util_img_format format)
 {
-       int error_value = IMAGE_UTIL_ERROR_NONE;
-       image_util_cb_s *_util_cb = (image_util_cb_s *) user_data;
+       unsigned int i = 0;
+       media_format_mimetype_e mimetype = -1;
+
+       for (i = 0; i < MM_UTIL_IMG_FMT_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_img_format __mimetype_to_image_format(media_format_mimetype_e mimetype)
+{
+       unsigned int i = 0;
+       mm_util_img_format format = -1;
+
+       for (i = 0; i < MM_UTIL_IMG_FMT_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 *new_fmt)
+{
+       int err = MEDIA_FORMAT_ERROR_NONE;
+
+       if ((new_fmt == NULL) || (width == 0) || (height == 0)) {
+               image_util_error("Invalid parameter");
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_format_create(new_fmt);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               image_util_error("media_format_make_writable failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_format_set_video_mime(*new_fmt, mimetype);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               image_util_error("media_format_set_video_mime failed (%d)", 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);
+               image_util_error("media_format_set_video_width failed (%d)", 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);
+               image_util_error("media_format_set_video_height failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       return IMAGE_UTIL_ERROR_NONE;
+}
+
+static int _image_util_packet_to_image(media_packet_h packet, mm_util_color_image_h *color_image)
+{
+       int err = IMAGE_UTIL_ERROR_NONE;
+       media_format_mimetype_e mimetype = 0;
+       int width = 0, height = 0;
+       uint64_t size = 0;
+       void *ptr = NULL;
+       media_format_h fmt = NULL;
+
+       if ((packet == NULL) || (color_image == NULL)) {
+               image_util_error("Invalid parameter");
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       err = media_packet_get_format(packet, &fmt);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_format failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       err = media_format_get_video_info(fmt, &mimetype, &width, &height, NULL, NULL);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               image_util_error("media_packet_get_format failed (%d)", err);
+               media_format_unref(fmt);
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+       media_format_unref(fmt);
+
+       err = media_packet_get_buffer_size(packet, &size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_buffer_size failed (%d)", err);
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       if (size) {
+               err = media_packet_get_buffer_data_ptr(packet, &ptr);
+               if (err != MM_UTIL_ERROR_NONE) {
+                       image_util_error("media_packet_get_buffer_data_ptr failed (%d)", err);
+                       return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+               }
+       }
+
+       image_util_debug("[Fotmat: %u] W x H : %d x %d", mimetype, width, height);
+       if ((width == 0) || (height == 0) || (size == 0) || (ptr == NULL)) {
+               image_util_error("Invalid source packet");
+               return IMAGE_UTIL_ERROR_INVALID_PARAMETER;
+       }
+       image_util_debug("_image_util_packet_to_image succeed");
+
+       err = mm_util_create_color_image(color_image);
+       if (err != MM_UTIL_ERROR_NONE) {
+               image_util_error("Error - Run transform (%d)", err);
+               return _image_error_capi(ERR_TYPE_TRANSFORM, err);
+       }
+       image_util_debug("_image_util_packet_to_image succeed");
+
+       err = mm_util_set_color_image(*color_image, width, height, __mimetype_to_image_format(mimetype), ptr, size);
+       if (err != MM_UTIL_ERROR_NONE) {
+               image_util_error("mm_util_push_request_image failed (%d)", err);
+               mm_util_destroy_color_image(*color_image);
+               return _image_error_capi(ERR_TYPE_TRANSFORM, err);
+       }
 
-       if ((_util_cb != NULL) && (_util_cb->image_processing_completed_cb != NULL)) {
-               error_value = _image_error_capi(ERR_TYPE_TRANSFORM, error);
-               _util_cb->image_processing_completed_cb(dst, error_value, _util_cb->user_data);
+       image_util_debug("_image_util_packet_to_image succeed");
+
+       return IMAGE_UTIL_ERROR_NONE;
+}
+
+static int _image_util_image_to_packet(mm_util_color_image_h image, media_packet_h *packet)
+{
+       int err = IMAGE_UTIL_ERROR_NONE;
+       mm_util_img_format format = 0;
+       int width = 0, height = 0;
+       void *buffer = NULL;
+       size_t buffer_size = 0;
+       media_format_h fmt = NULL;
+       void *packet_ptr = NULL;
+       uint64_t packet_size = 0;
+       size_t size = 0;
+
+       err = mm_util_get_color_image(image, &width, &height, &format, &buffer, &buffer_size);
+       if (err != MM_UTIL_ERROR_NONE) {
+               image_util_error("mm_util_get_color_image failed (%d)", err);
+               return _image_error_capi(ERR_TYPE_TRANSFORM, err);
+       }
+
+       err = __create_media_format(__image_format_to_mimetype(format), width, height, &fmt);
+       if (err != IMAGE_UTIL_ERROR_NONE) {
+               image_util_error("mm_util_push_request_image failed (%d)", err);
+               return err;
+       }
+
+       err = media_packet_create_alloc(fmt, NULL, NULL, packet);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_create_alloc failed (%d)", err);
+               media_format_unref(fmt);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_packet_get_buffer_size(*packet, &packet_size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_buffer_size failed (%d)", err);
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       err = media_packet_get_buffer_data_ptr(*packet, &packet_ptr);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               image_util_error("media_packet_get_buffer_data_ptr failed");
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+
+       if (packet_ptr == NULL || packet_size == 0) {
+               image_util_error("media_packet creation failed (%p, %" PRIu64 ")", packet_ptr, packet_size);
+               media_packet_destroy(*packet);
+               return IMAGE_UTIL_ERROR_INVALID_OPERATION;
+       }
+       image_util_debug("Success - media_packet is created (%p, %" PRIu64 ")", packet_ptr, packet_size);
+
+       if ((uint64_t)buffer_size < packet_size) {
+               size = (size_t)buffer_size;
+       } else {
+               size = (size_t)packet_size;
+       }
+
+       image_util_debug("Size: result(%u) media_packet(%" PRIu64 ") copied(%zu)", buffer_size, packet_size, size);
+       memcpy(packet_ptr, buffer, size);
+
+       image_util_debug("_image_util_image_to_packet succeed");
+
+       return IMAGE_UTIL_ERROR_NONE;
+}
+
+static void _image_util_transform_completed_cb(mm_util_color_image_h raw_image, int error, void *user_data)
+{
+       int err = IMAGE_UTIL_ERROR_NONE;
+       image_util_cb_s *_util_cb = (image_util_cb_s *) user_data;
+       media_packet_h packet = NULL;
+
+       if ((_util_cb != NULL) && (_util_cb->completed_cb != NULL)) {
+               err = _image_util_image_to_packet(raw_image, &packet);
+               if (err != MM_UTIL_ERROR_NONE) {
+                       image_util_error("_image_util_image_to_packet failed (%d)", err);
+                       _util_cb->completed_cb(NULL, err, _util_cb->user_data);
+               } else {
+                       _util_cb->completed_cb(&packet, _image_error_capi(ERR_TYPE_TRANSFORM, error), _util_cb->user_data);
+               }
        }
 
        return;
@@ -307,20 +545,28 @@ int image_util_transform_run(transformation_h handle, media_packet_h src, image_
        image_util_retvm_if((src == NULL), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid source");
        image_util_retvm_if((!_handle->set_convert && !_handle->set_resize && !_handle->set_rotate && !_handle->set_crop), IMAGE_UTIL_ERROR_INVALID_PARAMETER, "Invalid transform");
 
-       if (_handle->_util_cb != NULL) {
-               IMAGE_UTIL_SAFE_FREE(_handle->_util_cb);
-               _handle->_util_cb = NULL;
-       }
-       _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
-       image_util_retvm_if((_handle->_util_cb == NULL), IMAGE_UTIL_ERROR_OUT_OF_MEMORY, "Out of memory");
+       mm_util_color_image_h color_image = NULL;
+
+       err = _image_util_packet_to_image(src, &color_image);
+       image_util_retvm_if((err != IMAGE_UTIL_ERROR_NONE), err, "_image_util_packet_to_image failed");
 
+       _handle->_util_cb = (image_util_cb_s *) calloc(1, sizeof(image_util_cb_s));
+       if (_handle->_util_cb == NULL) {
+               image_util_error("Memory allocation failed");
+               mm_util_destroy_color_image(color_image);
+               return IMAGE_UTIL_ERROR_OUT_OF_MEMORY;
+       }
        _handle->_util_cb->user_data = user_data;
-       _handle->_util_cb->image_processing_completed_cb = completed_cb;
+       _handle->_util_cb->completed_cb = completed_cb;
 
-       if (_handle->_util_cb)
-               err = mm_util_transform(_handle->image_h, src, (mm_util_completed_callback) _image_util_transform_completed_cb, (void *)_handle->_util_cb);
+       err = mm_util_transform(_handle->image_h, color_image, (mm_util_completed_callback) _image_util_transform_completed_cb, (void *)_handle->_util_cb);
+       if (err != MM_UTIL_ERROR_NONE) {
+               image_util_error("Error - Run transform (%d)", err);
+               mm_util_destroy_color_image(color_image);
+               return _image_error_capi(ERR_TYPE_TRANSFORM, err);
+       }
 
-       return _image_error_capi(ERR_TYPE_TRANSFORM, err);
+       return IMAGE_UTIL_ERROR_NONE;
 }
 
 int image_util_transform_destroy(transformation_h handle)