Code Cleanup for __mm_util_transform_exec 01/158201/3
authorJiyong Min <jiyong.min@samsung.com>
Mon, 30 Oct 2017 09:28:06 +0000 (18:28 +0900)
committerJiyong Min <jiyong.min@samsung.com>
Tue, 31 Oct 2017 01:16:35 +0000 (10:16 +0900)
 - Add function for __mm_util_transform_exec to be simplified

Change-Id: Ie91b1a69c004c1586a4806e5f85a99a9f1ec5f68
Signed-off-by: Jiyong Min <jiyong.min@samsung.com>
imgp/mm_util_imgp.c

index 174a871..f83bd67 100755 (executable)
@@ -698,21 +698,6 @@ unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_wid
        return ret;
 }
 
-static bool __mm_util_check_resolution(unsigned int width, unsigned int height)
-{
-       if (width == 0) {
-               mm_util_error("invalid width [%d]", width);
-               return FALSE;
-       }
-
-       if (height == 0) {
-               mm_util_error("invalid height [%d]", height);
-               return FALSE;
-       }
-
-       return TRUE;
-}
-
 static int __mm_util_handle_init(mm_util_s *handle)
 {
        int ret = MM_UTIL_ERROR_NONE;
@@ -976,15 +961,6 @@ static int __mm_util_processing(mm_util_s *handle)
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
-       if (handle->src_buf_size) {
-               handle->src = NULL;
-               if (media_packet_get_buffer_data_ptr(handle->src_packet, &handle->src) != MM_UTIL_ERROR_NONE) {
-                       mm_util_error("[src] media_packet_get_extra");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
-               mm_util_debug("src buffer pointer: %p", handle->src);
-       }
-
        if (handle->dst_buf_size) {
                handle->dst = NULL;
                if (media_packet_get_buffer_data_ptr(handle->dst_packet, &handle->dst) != MM_UTIL_ERROR_NONE) {
@@ -1106,161 +1082,231 @@ static int __mm_util_processing(mm_util_s *handle)
        return ret;
 }
 
-static int __mm_util_transform_exec(mm_util_s *handle, media_packet_h src_packet)
+static int __mm_get_info_from_media_packet(media_packet_h pkt, media_format_h *fmt, mm_util_s *handle)
 {
-       int ret = MM_UTIL_ERROR_NONE;
-       media_format_h src_fmt;
-       media_format_h dst_fmt;
-       media_format_mimetype_e src_mimetype;
-       int src_width, src_height, src_avg_bps, src_max_bps;
-       unsigned int dst_width = 0, dst_height = 0;
+       int err = MEDIA_PACKET_ERROR_NONE;
+       media_format_mimetype_e mimetype = 0;
+       int width = 0, height = 0;
        uint64_t size = 0;
+       void *ptr = NULL;
 
-       if (media_packet_get_format(src_packet, &src_fmt) != MEDIA_PACKET_ERROR_NONE) {
-               mm_util_error("Imedia_packet_get_format)");
+       if ((pkt == NULL) || (fmt == NULL) || (handle == NULL)) {
+               mm_util_error("Invalid parameter");
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
-       if (media_format_get_video_info(src_fmt, &src_mimetype, &src_width, &src_height, &src_avg_bps, &src_max_bps) == MEDIA_FORMAT_ERROR_NONE)
-               mm_util_debug("[Fotmat: %d] W x H : %d x %d", src_mimetype, src_width, src_height);
+       err = media_packet_get_format(pkt, fmt);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               mm_util_error("media_packet_get_format failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-       if (__mm_util_check_resolution(src_width, src_height)) {
-               /* src */
-               handle->src_packet = src_packet;
+       err = media_format_get_video_info(*fmt, &mimetype, &width, &height, NULL, NULL);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               mm_util_error("media_packet_get_format failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               mm_util_debug("[W X H] %d X %d", src_width, src_height);
+       err = media_packet_get_buffer_size(pkt, &size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               mm_util_error("media_packet_get_buffer_size failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               if (handle->src_packet) {
-                       handle->src_format = __mm_util_mapping_mime_format_to_imgp(src_mimetype);
-                       handle->src_width = src_width;
-                       handle->src_height = src_height;
-               } else {
-                       mm_util_error("[Error] handle->src");
-                       media_format_unref(src_fmt);
+       if (size) {
+               handle->src = NULL;
+               err = media_packet_get_buffer_data_ptr(pkt, &ptr);
+               if (err != MM_UTIL_ERROR_NONE) {
+                       mm_util_error("[src] media_packet_get_extra");
                        return MM_UTIL_ERROR_INVALID_PARAMETER;
                }
+               mm_util_debug("src buffer pointer: %p", ptr);
+       }
 
-               if (media_packet_get_buffer_size(handle->src_packet, &size) == MEDIA_PACKET_ERROR_NONE) {
-                       handle->src_buf_size = (guint)size;
-                       mm_util_debug("src buffer(%p) size: %d", handle->src_packet, handle->src_buf_size);
-               } else {
-                       mm_util_error("Error buffer size");
-               }
+       mm_util_debug("[Fotmat: %u] W x H : %d x %d", mimetype, width, height);
+       if ((width == 0) || (height == 0) || (size == 0) || (ptr == NULL)) {
+               mm_util_error("Invalid source packet");
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               if (handle->dst_format == MM_UTIL_IMG_FMT_NUM) {
-                       mm_util_debug("dst format is changed");
-                       handle->dst_format = handle->src_format;
-                       handle->dst_format_mime = src_mimetype;
-               }
+       handle->src_format = __mm_util_mapping_mime_format_to_imgp(mimetype);
+       handle->src_width = width;
+       handle->src_height = height;
+       handle->src_buf_size = (guint)size;
+       handle->src_packet = pkt;
+       handle->src = ptr;
+
+       if (handle->dst_format == MM_UTIL_IMG_FMT_NUM) {
+               mm_util_debug("dst format is equal to src format");
+               handle->dst_format = handle->src_format;
+               handle->dst_format_mime = mimetype;
+       }
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+static int __mm_get_dst_resolution(mm_util_s *handle, unsigned int *width, unsigned int *height)
+{
+       if ((handle == NULL) || (width == NULL) || (height == NULL)) {
+               mm_util_error("Invalid parameter");
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               mm_util_debug("src: %p handle->src_packet: %p (%d X %d)", src_packet, handle->src_packet, handle->src_width, handle->src_height);
-               if (handle->set_rotate) {
-                       if ((handle->set_crop) || (handle->set_resize)) {
-                               switch (handle->dst_rotation) {
-                               case MM_UTIL_ROTATION_90:
-                               case MM_UTIL_ROTATION_270:
-                                       dst_width = handle->dst_height;
-                                       dst_height = handle->dst_width;
-                                       break;
-                               default:
-                                       dst_width = handle->dst_width;
-                                       dst_height = handle->dst_height;
-                                       break;
-                               }
-                       } else {
-                               switch (handle->dst_rotation) {
-                               case MM_UTIL_ROTATION_90:
-                               case MM_UTIL_ROTATION_270:
-                                       dst_width = handle->dst_width  = handle->src_height;
-                                       dst_height = handle->dst_height = handle->src_width;
-                                       break;
-                               case MM_UTIL_ROTATION_NONE:
-                               case MM_UTIL_ROTATION_180:
-                               case MM_UTIL_ROTATION_FLIP_HORZ:
-                               case MM_UTIL_ROTATION_FLIP_VERT:
-                                       dst_width = handle->dst_width  = handle->src_width;
-                                       dst_height = handle->dst_height = handle->src_height;
-                                       break;
-                               default:
-                                       mm_util_error("[Error] Wrong dst_rotation");
-                                       break;
-                               }
+       if (handle->set_rotate) {
+               if ((handle->set_crop) || (handle->set_resize)) {
+                       switch (handle->dst_rotation) {
+                       case MM_UTIL_ROTATION_90:
+                       case MM_UTIL_ROTATION_270:
+                               *width = handle->dst_height;
+                               *height = handle->dst_width;
+                               break;
+                       default:
+                               *width = handle->dst_width;
+                               *height = handle->dst_height;
+                               break;
                        }
                } else {
-                       if ((handle->set_crop) || (handle->set_resize)) {
-                               dst_width = handle->dst_width;
-                               dst_height = handle->dst_height;
-                       } else {
-                               dst_width = handle->dst_width  = handle->src_width;
-                               dst_height = handle->dst_height = handle->src_height;
+                       switch (handle->dst_rotation) {
+                       case MM_UTIL_ROTATION_90:
+                       case MM_UTIL_ROTATION_270:
+                               *width = handle->dst_width  = handle->src_height;
+                               *height = handle->dst_height = handle->src_width;
+                               break;
+                       case MM_UTIL_ROTATION_NONE:
+                       case MM_UTIL_ROTATION_180:
+                       case MM_UTIL_ROTATION_FLIP_HORZ:
+                       case MM_UTIL_ROTATION_FLIP_VERT:
+                               *width = handle->dst_width  = handle->src_width;
+                               *height = handle->dst_height = handle->src_height;
+                               break;
+                       default:
+                               mm_util_error("[Error] Wrong dst_rotation");
+                               break;
                        }
                }
-               mm_util_debug("dst (%d X %d)", dst_width, dst_height);
-               if (media_format_make_writable(src_fmt, &dst_fmt) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       mm_util_error("[Error] Writable - dst format");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
+       } else {
+               if ((handle->set_crop) || (handle->set_resize)) {
+                       *width = handle->dst_width;
+                       *height = handle->dst_height;
+               } else {
+                       *width = handle->dst_width  = handle->src_width;
+                       *height = handle->dst_height = handle->src_height;
                }
+       }
+       mm_util_debug("dst (%u X %u)", *width, *height);
 
-               if (media_format_set_video_mime(dst_fmt, handle->dst_format_mime) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       mm_util_error("[Error] Set - video mime");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
+       return MM_UTIL_ERROR_NONE;
+}
 
-               if (media_format_set_video_width(dst_fmt, dst_width) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       mm_util_error("[Error] Set - video width");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
+static int __mm_create_media_format(media_format_h fmt, media_format_mimetype_e mimetype, unsigned int width, unsigned int height, media_format_h *new_fmt)
+{
+       int err = MEDIA_FORMAT_ERROR_NONE;
 
-               if (media_format_set_video_height(dst_fmt, dst_height) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       mm_util_error("[Error] Set - video height");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
+       if ((fmt == NULL) || (new_fmt == NULL) || (width == 0) || (height == 0)) {
+               mm_util_error("Invalid parameter");
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               if (media_format_set_video_avg_bps(dst_fmt, src_avg_bps) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       mm_util_error("[Error] Set - video avg bps");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
+       err = media_format_make_writable(fmt, new_fmt);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               mm_util_error("media_format_make_writable failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               if (media_format_set_video_max_bps(dst_fmt, src_max_bps) != MEDIA_FORMAT_ERROR_NONE) {
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       mm_util_error("[Error] Set - video max bps");
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               }
+       err = media_format_set_video_mime(*new_fmt, mimetype);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               mm_util_error("media_format_set_video_mime failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
 
-               if (media_packet_create_alloc(dst_fmt, (media_packet_finalize_cb)_mm_util_transform_packet_finalize_callback, NULL, &handle->dst_packet) != MEDIA_PACKET_ERROR_NONE) {
-                       mm_util_error("[Error] Create allocation memory");
-                       media_format_unref(src_fmt);
-                       media_format_unref(dst_fmt);
-                       return MM_UTIL_ERROR_INVALID_PARAMETER;
-               } else {
-                       mm_util_debug("Success - dst media packet");
-                       if (media_packet_get_buffer_size(handle->dst_packet, &size) != MEDIA_PACKET_ERROR_NONE) {
-                               mm_util_error("Imedia_packet_get_format)");
-                               media_format_unref(src_fmt);
-                               media_format_unref(dst_fmt);
-                               return MM_UTIL_ERROR_INVALID_PARAMETER;
-                       }
-                       handle->dst_buf_size = (guint)size;
-                       mm_util_debug("handle->src_packet: %p [%d] %d X %d (%d) => handle->dst_packet: %p [%d] %d X %d (%d)",
-                               handle->src_packet, handle->src_format, handle->src_width, handle->src_height, handle->src_buf_size,
-                               handle->dst_packet, handle->dst_format, dst_width, dst_height, handle->dst_buf_size);
-               }
-       } else {
-               mm_util_error("%d %d", src_width, src_height);
-               media_format_unref(src_fmt);
+       err = media_format_set_video_width(*new_fmt, width);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               mm_util_error("media_format_set_video_width failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       err = media_format_set_video_height(*new_fmt, height);
+       if (err != MEDIA_FORMAT_ERROR_NONE) {
+               media_format_unref(*new_fmt);
+               mm_util_error("media_format_set_video_height failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+static int __mm_create_media_packet(media_format_h fmt, media_packet_h *pkt, guint *pkt_buf_size)
+{
+       int err = MEDIA_PACKET_ERROR_NONE;
+       uint64_t size = 0;
+
+       if ((fmt == NULL) || (pkt == NULL) || (pkt_buf_size == NULL)) {
+               mm_util_error("Invalid parameter");
                return MM_UTIL_ERROR_INVALID_PARAMETER;
        }
 
+       err = media_packet_create_alloc(fmt, (media_packet_finalize_cb)_mm_util_transform_packet_finalize_callback, NULL, pkt);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               mm_util_error("media_packet_create_alloc failed (%d)", err);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       err = media_packet_get_buffer_size(*pkt, &size);
+       if (err != MEDIA_PACKET_ERROR_NONE) {
+               mm_util_error("media_packet_get_buffer_size failed (%d)", err);
+               media_packet_destroy(*pkt);
+               return MM_UTIL_ERROR_INVALID_PARAMETER;
+       }
+
+       mm_util_debug("Success - media_packet is created (%p, %lu)", *pkt, size);
+       *pkt_buf_size = (guint)size;
+
+       return MM_UTIL_ERROR_NONE;
+}
+
+static int __mm_util_transform_exec(mm_util_s *handle, media_packet_h src_packet)
+{
+       int ret = MM_UTIL_ERROR_NONE;
+       media_format_h src_fmt;
+       media_format_h dst_fmt;
+       unsigned int dst_width = 0, dst_height = 0;
+
+       ret = __mm_get_info_from_media_packet(src_packet, &src_fmt, handle);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__mm_get_info_from_packet failed");
+               return ret;
+       }
+       mm_util_debug("src: %p handle->src_packet: %p (%u X %u)", src_packet, handle->src_packet, handle->src_width, handle->src_height);
+
+       ret = __mm_get_dst_resolution(handle, &dst_width, &dst_height);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__mm_get_dst_resolution failed");
+               media_format_unref(src_fmt);
+               return ret;
+       }
+       mm_util_debug("dst (%u X %u)", dst_width, dst_height);
+
+       ret = __mm_create_media_format(src_fmt, handle->dst_format_mime, dst_width, dst_height, &dst_fmt);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__mm_create_media_format failed");
+               media_format_unref(src_fmt);
+               return ret;
+       }
+       media_format_unref(src_fmt);
+
+       ret = __mm_create_media_packet(dst_fmt, &handle->dst_packet, &handle->dst_buf_size);
+       if (ret != MM_UTIL_ERROR_NONE) {
+               mm_util_error("__mm_create_media_packet failed");
+               media_format_unref(dst_fmt);
+               return ret;
+       }
+
+       mm_util_debug("handle->src_packet: %p [%d] %d X %d (%d) => handle->dst_packet: %p [%d] %d X %d (%d)",
+               handle->src_packet, handle->src_format, handle->src_width, handle->src_height, handle->src_buf_size,
+               handle->dst_packet, handle->dst_format, dst_width, dst_height, handle->dst_buf_size);
+
        ret = __mm_util_processing(handle);
 
        if (ret != MM_UTIL_ERROR_NONE) {