Update code to reduce Cyclomatic Complexity 54/212854/5
authorhj kim <backto.kim@samsung.com>
Tue, 27 Aug 2019 09:39:00 +0000 (18:39 +0900)
committerhj kim <backto.kim@samsung.com>
Tue, 3 Sep 2019 02:19:36 +0000 (11:19 +0900)
Change-Id: I935554429b800c6a27d40e8a46e6380a7933c4f8

src/media-thumb-internal.c

index 871e913..44a7c5e 100755 (executable)
 #include <mm_file.h>
 #include <mm_util_magick.h>
 
-int _media_thumb_get_proper_thumb_size(int orig_w, int orig_h, int *thumb_w, int *thumb_h)
+static void __media_thumb_get_proper_thumb_size(int origin_width, int origin_height, int *thumb_width, int *thumb_height)
 {
        bool portrait = false;
        double ratio;
 
-       if (orig_w < orig_h)
+       if (origin_width < origin_height)
                portrait = true;
 
        /* Set smaller length to default size */
        if (portrait) {
-               if (orig_w < *thumb_w)
-                       *thumb_w = orig_w;
-               ratio = (double)orig_h / (double)orig_w;
-               *thumb_h = *thumb_w * ratio;
+               if (origin_width < *thumb_width)
+                       *thumb_width = origin_width;
+               ratio = (double)origin_height / (double)origin_width;
+               *thumb_height = *thumb_width * ratio;
        } else {
-               if (orig_h < *thumb_h)
-                       *thumb_h = orig_h;
-               ratio = (double)orig_w / (double)orig_h;
-               *thumb_w = *thumb_h * ratio;
+               if (origin_height < *thumb_height)
+                       *thumb_height = origin_height;
+               ratio = (double)origin_width / (double)origin_height;
+               *thumb_width = *thumb_height * ratio;
        }
 
-       thumb_dbg("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
+       thumb_dbg("proper thumb w: %d h: %d", *thumb_width, *thumb_height);
 
-       return MS_MEDIA_ERR_NONE;
+       return;
 }
 
 int _media_thumb_general(const char *origin_path, const char *thumb_path, int thumb_width, int thumb_height, media_thumb_info *thumb_info)
@@ -78,12 +78,10 @@ int _media_thumb_general(const char *origin_path, const char *thumb_path, int th
                }
 
                mm_image_get_image(img, &width, &height, &format, &buf, &size);
-               thumb_info->data = calloc(1, size);
-               memcpy(thumb_info->data, buf, size);
+               thumb_info->data = buf;
                thumb_info->size = size;
                thumb_info->width = width;
                thumb_info->height = height;
-               SAFE_FREE(buf);
                mm_image_destroy_image(img);
        }
 
@@ -108,7 +106,7 @@ int _media_thumb_image(const char *origin_path, char *thumb_path, int thumb_widt
                return MS_MEDIA_ERR_THUMB_TOO_BIG;
        }
 
-       _media_thumb_get_proper_thumb_size(origin_w, origin_h, &thumb_width, &thumb_height);
+       __media_thumb_get_proper_thumb_size(origin_w, origin_h, &thumb_width, &thumb_height);
 
        if (image_type == IMG_CODEC_PNG) {
                if (thumb_path != NULL) {
@@ -132,72 +130,73 @@ int _media_thumb_image(const char *origin_path, char *thumb_path, int thumb_widt
        return err;
 }
 
-int _media_thumb_video(const char *origin_path, const char *thumb_path, int thumb_width, int thumb_height, media_thumb_info *thumb_info)
+static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
 {
        int err = MS_MEDIA_ERR_NONE;
-
-       MMHandleType content = (MMHandleType) NULL;
        MMHandleType tag = (MMHandleType) NULL;
-
        char *p = NULL;
-       int cdis_value = 0;
-       void *frame = NULL;
-       int video_track_num = 0;
-       char *err_msg = NULL;
        int size = 0;
-       int width = 0;
-       int height = 0;
-       mm_util_image_h img = NULL;
-       mm_util_image_h resize_img = NULL;
-       mm_util_image_h dst_img = NULL;
-
-       unsigned char *res_buf = NULL;
-       size_t res_size = 0;
-       unsigned int res_width = 0;
-       unsigned int res_height = 0;
-       mm_util_color_format_e res_format;
+       char *err_msg = NULL;
+       int _cdis_value = 0;
+       mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
 
        /* Get Content Tag attribute for orientation */
        err = mm_file_create_tag_attrs(&tag, origin_path);
-       mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
+       if (err != FILEINFO_ERROR_NONE) {
+               *rot_type = MM_UTIL_ROTATE_0;
+               *cdis_value = 0;
+               return;
+       }
 
-       if (err == FILEINFO_ERROR_NONE) {
-               err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
-               if (err == FILEINFO_ERROR_NONE && size >= 0) {
-                       if (p == NULL) {
-                               rot_type = MM_UTIL_ROTATE_0;
-                       } else {
-                               if (strncmp(p, "90", size) == 0)
-                                       rot_type = MM_UTIL_ROTATE_90;
-                               else if (strncmp(p, "180", size) == 0)
-                                       rot_type = MM_UTIL_ROTATE_180;
-                               else if (strncmp(p, "270", size) == 0)
-                                       rot_type = MM_UTIL_ROTATE_270;
-                               else
-                                       rot_type = MM_UTIL_ROTATE_0;
-                       }
-                       thumb_dbg("There is tag rotate : %d", rot_type);
+       err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
+       if (err == FILEINFO_ERROR_NONE && size >= 0) {
+               if (p == NULL) {
+                       _rot_type = MM_UTIL_ROTATE_0;
                } else {
-                       thumb_dbg("There is NOT tag rotate");
-                       rot_type = MM_UTIL_ROTATE_0;
-                       SAFE_FREE(err_msg);
-               }
-
-               err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &cdis_value, NULL);
-               if (err != FILEINFO_ERROR_NONE) {
-                       cdis_value = 0;
-                       SAFE_FREE(err_msg);
+                       if (strncmp(p, "90", size) == 0)
+                               _rot_type = MM_UTIL_ROTATE_90;
+                       else if (strncmp(p, "180", size) == 0)
+                               _rot_type = MM_UTIL_ROTATE_180;
+                       else if (strncmp(p, "270", size) == 0)
+                               _rot_type = MM_UTIL_ROTATE_270;
+                       else
+                               _rot_type = MM_UTIL_ROTATE_0;
                }
+               thumb_dbg("There is tag rotate : %d", _rot_type);
        } else {
-               rot_type = MM_UTIL_ROTATE_0;
-               cdis_value = 0;
+               thumb_dbg("There is NOT tag rotate");
+               _rot_type = MM_UTIL_ROTATE_0;
+               SAFE_FREE(err_msg);
+       }
+
+       err = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
+       if (err != FILEINFO_ERROR_NONE) {
+               _cdis_value = 0;
+               SAFE_FREE(err_msg);
        }
 
+       *rot_type = _rot_type;
+       *cdis_value = _cdis_value;
+
        err = mm_file_destroy_tag_attrs(tag);
        if (err != FILEINFO_ERROR_NONE) {
                thumb_err("fail to free tag attr - err(%x)", err);
        }
 
+       return;
+}
+
+static int __get_video_info(int cdis_value, const char *origin_path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *size)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       MMHandleType content = (MMHandleType) NULL;
+       char *err_msg = NULL;
+       int _video_track_num = 0;
+       int _width = 0;
+       int _height = 0;
+       size_t _size = 0;
+       void *_frame = NULL;
+
        if (cdis_value == 1) {
                thumb_warn("This is CDIS vlaue 1");
                err = mm_file_create_content_attrs_safe(&content, origin_path);
@@ -210,107 +209,105 @@ int _media_thumb_video(const char *origin_path, const char *thumb_path, int thum
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       err = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
+       err = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
        if (err != FILEINFO_ERROR_NONE) {
                thumb_err("mm_file_get_attrs fails : %s", err_msg);
                SAFE_FREE(err_msg);
-               goto ERROR;
+               mm_file_destroy_content_attrs(content);
+               return MS_MEDIA_ERR_INTERNAL;
        }
 
-       if (video_track_num > 0) {
-               err = mm_file_get_attrs(content, &err_msg,
-                                       MM_FILE_CONTENT_VIDEO_WIDTH,
-                                       &width,
-                                       MM_FILE_CONTENT_VIDEO_HEIGHT,
-                                       &height,
-                                       MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame, /* raw image is RGB888 format */
-                                       &size, NULL);
-
-               if (err != FILEINFO_ERROR_NONE) {
-                       thumb_err("mm_file_get_attrs fails : %s", err_msg);
-                       SAFE_FREE(err_msg);
-                       goto ERROR;
-               }
+       *video_track_num = _video_track_num;
 
-               thumb_dbg("W[%d] H[%d] Size[%d] Frame[%p] Rotate[%d]", width, height, size, frame, rot_type);
-               if (frame == NULL || width == 0 || height == 0) {
-                       thumb_err("Failed to get frame data");
-                       goto ERROR;
-               }
+       if (_video_track_num == 0) {
+               mm_file_destroy_content_attrs(content);
+               return MS_MEDIA_ERR_NONE;
+       }
 
-               err = _media_thumb_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
-               if (thumb_width <= 0 || thumb_height <= 0) {
-                       thumb_err("Failed to get thumb size");
-                       goto ERROR;
-               }
+       err = mm_file_get_attrs(content, &err_msg,
+                               MM_FILE_CONTENT_VIDEO_WIDTH,
+                               &_width,
+                               MM_FILE_CONTENT_VIDEO_HEIGHT,
+                               &_height,
+                               MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
+                               &_size, NULL);
+
+       if (err != FILEINFO_ERROR_NONE) {
+               thumb_err("mm_file_get_attrs fails : %s", err_msg);
+               SAFE_FREE(err_msg);
+               mm_file_destroy_content_attrs(content);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       thumb_dbg("W[%d] H[%d] Size[%d] Frame[%p]", _width, _height, _size, _frame);
+       if (!_frame || !_width || !_height) {
+               mm_file_destroy_content_attrs(content);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+
+       *width = _width;
+       *height = _height;
+       *size = _size;
+       *frame = calloc(1, _size);
+       memcpy(*frame, _frame, _size);
+
+       mm_file_destroy_content_attrs(content);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __get_video_thumb(int width, int height, void *frame, size_t size, mm_util_magick_rotate_type rot_type, const char *thumb_path, int thumb_width, int thumb_height, mm_util_image_h *dst_img)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       mm_util_image_h img = NULL;
+       mm_util_image_h resize_img = NULL;
+
+       __media_thumb_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
+       if (thumb_width <= 0 || thumb_height <= 0) {
+               thumb_err("Failed to get thumb size");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       thumb_dbg("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
+
+       err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
+       thumb_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
+
+       if (width > thumb_width || height > thumb_height) {
+               if (rot_type != MM_UTIL_ROTATE_0) {
+                       if (STRING_VALID(thumb_path)) {
+                               err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
+                               if (err != MM_UTIL_ERROR_NONE)
+                                       goto ERROR;
+
+                               err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
 
-               thumb_dbg("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
-
-               err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
-               if (width > thumb_width || height > thumb_height) {
-                       if (rot_type != MM_UTIL_ROTATE_0) {
-                               if (STRING_VALID(thumb_path)) {
-                                       err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
-                                       if (err != MM_UTIL_ERROR_NONE)
-                                               goto ERROR;
-                                       err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
-                               } else {
-                                       err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
-                                       if (err != MM_UTIL_ERROR_NONE)
-                                               goto ERROR;
-                                       err = mm_util_rotate_B_B(resize_img, rot_type, &dst_img);
-                                       if (err != MM_UTIL_ERROR_NONE)
-                                               goto ERROR;
-                                       mm_image_get_image(dst_img, &res_width, &res_height, &res_format, &res_buf, &res_size);
-                                       thumb_info->data = calloc(1, res_size);
-                                       memcpy(thumb_info->data, res_buf, res_size);
-                                       thumb_info->size = res_size;
-                                       thumb_info->width = res_width;
-                                       thumb_info->height = res_height;
-                                       SAFE_FREE(res_buf);
-                               }
                        } else {
-                               if (STRING_VALID(thumb_path)) {
-                                       err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
-                               } else {
-                                       err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
-                                       if (err != MM_UTIL_ERROR_NONE)
-                                               goto ERROR;
-                                       mm_image_get_image(resize_img, &res_width, &res_height, &res_format, &res_buf, &res_size);
-                                       thumb_info->data = calloc(1, res_size);
-                                       memcpy(thumb_info->data, res_buf, res_size);
-                                       thumb_info->size = res_size;
-                                       thumb_info->width = res_width;
-                                       thumb_info->height = res_height;
-                                       SAFE_FREE(res_buf);
-                               }
+                               err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
+                               if (err != MM_UTIL_ERROR_NONE)
+                                       goto ERROR;
+
+                               err = mm_util_rotate_B_B(resize_img, rot_type, dst_img);
                        }
                } else {
-                       if (rot_type != MM_UTIL_ROTATE_0) {
-                               if (STRING_VALID(thumb_path)) {
-                                       err = mm_util_rotate_B_P(img, rot_type, thumb_path);
-                               } else {
-                                       err = mm_util_rotate_B_B(img, rot_type, &dst_img);
-                                       if (err != MM_UTIL_ERROR_NONE)
-                                               goto ERROR;
-                                       mm_image_get_image(dst_img, &res_width, &res_height, &res_format, &res_buf, &res_size);
-                                       thumb_info->data = calloc(1, res_size);
-                                       memcpy(thumb_info->data, res_buf, res_size);
-                                       thumb_info->size = res_size;
-                                       thumb_info->width = res_width;
-                                       thumb_info->height = res_height;
-                                       SAFE_FREE(res_buf);
-                               }
+                       if (STRING_VALID(thumb_path))
+                               err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
+                       else
+                               err = mm_util_resize_B_B(img, thumb_width, thumb_height, dst_img);
+               }
+       } else {
+               if (rot_type != MM_UTIL_ROTATE_0) {
+                       if (STRING_VALID(thumb_path)) {
+                               err = mm_util_rotate_B_P(img, rot_type, thumb_path);
                        } else {
-                               if (STRING_VALID(thumb_path)) {
-                                       err = mm_util_resize_B_P(img, width, height, thumb_path);
-                               } else {
-                                       thumb_info->data = calloc(1, size);
-                                       memcpy(thumb_info->data, frame, size);
-                                       thumb_info->size = size;
-                                       thumb_info->width = width;
-                                       thumb_info->height = height;
-                               }
+                               err = mm_util_rotate_B_B(img, rot_type, dst_img);
+                       }
+               } else {
+                       if (STRING_VALID(thumb_path)) {
+                               err = mm_util_resize_B_P(img, width, height, thumb_path);
+                       } else {
+                               err = mm_image_clone_image(img, dst_img);
                        }
                }
        }
@@ -318,15 +315,55 @@ int _media_thumb_video(const char *origin_path, const char *thumb_path, int thum
 ERROR:
        mm_image_destroy_image(img);
        mm_image_destroy_image(resize_img);
-       mm_image_destroy_image(dst_img);
-       mm_file_destroy_content_attrs(content);
-
        if (err != MS_MEDIA_ERR_NONE)
                return MS_MEDIA_ERR_INTERNAL;
 
        return MS_MEDIA_ERR_NONE;
 }
 
+int _media_thumb_video(const char *origin_path, const char *thumb_path, int thumb_width, int thumb_height, media_thumb_info *thumb_info)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       int cdis_value = 0;
+       void *frame = NULL;
+       int video_track_num = 0;
+       unsigned int width = 0;
+       unsigned int height = 0;
+       mm_util_image_h dst_img = NULL;
+       unsigned char *buf = NULL;
+       size_t size = 0;
+       mm_util_color_format_e format = MM_UTIL_COLOR_NUM;
+       mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
+
+       __get_rotation_and_cdis(origin_path, &rot_type, &cdis_value);
+       err = __get_video_info(cdis_value, origin_path, &video_track_num, &width, &height, &frame, &size);
+       thumb_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to __get_video_info [%d]", err);
+       thumb_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
+
+       if (STRING_VALID(thumb_path)) {
+               err = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, thumb_width, thumb_height, NULL);
+
+       } else if (thumb_info) {
+               err = __get_video_thumb(width, height, frame, size, rot_type, NULL, thumb_width, thumb_height, &dst_img);
+               if (err == MS_MEDIA_ERR_NONE) {
+                       err = mm_image_get_image(dst_img, &width, &height, &format, &buf, &size);
+                                       thumb_info->data = buf;
+                                       thumb_info->size = size;
+                                       thumb_info->width = width;
+                                       thumb_info->height = height;
+               }
+
+               mm_image_destroy_image(dst_img);
+       } else {
+               thumb_err("Invalid parameter");
+               err = MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       SAFE_FREE(frame);
+
+       return err;
+}
+
 int _media_thumb_get_hash_name(const char *file_full_path, char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
 {
        char *hash_name = NULL;