Fix coverity issues
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index 24a3e2e..16fb4fd 100755 (executable)
@@ -39,7 +39,6 @@
 #include <aul/aul.h>
 #include <mm_file.h>
 #include <libexif/exif-data.h>
-#include <media-thumbnail.h>
 #include <media-util.h>
 #include <uuid/uuid.h>
 #include <mm_util_magick.h>
@@ -519,7 +518,7 @@ static int __media_svc_resize_artwork(const char *path, const char *img_format)
        unsigned int height = 0;
        unsigned int resized_width = 0;
        unsigned int resized_height = 0;
-       mm_util_img_code_type img_type = IMG_CODEC_NONE;
+       mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
        if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
                media_svc_debug("type [jpeg]");
@@ -741,14 +740,13 @@ int _media_svc_remove_all_files_in_dir(const char *dir_path)
        return MS_MEDIA_ERR_NONE;
 }
 
-int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
+int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
        char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
        char hash[255 + 1] = {0, };
-       char *thumbfile_ext = NULL;
        char *thumb_dir = NULL;
+       char *thumbfile_ext = NULL;
 
        ret = ms_user_get_thumb_store_path(uid, storage_type, &thumb_dir);
        if (!STRING_VALID(thumb_dir)) {
@@ -773,26 +771,29 @@ int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *t
                return MS_MEDIA_ERR_INTERNAL;
        }
 
-       /*media_svc_debug("img format is [%s]", img_format); */
+       if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
+               if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
+                       thumbfile_ext = (char *)"jpg";
+               } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
+                       thumbfile_ext = (char *)"png";
+               } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
+                       thumbfile_ext = (char *)"gif";
+               } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
+                       thumbfile_ext = (char *)"bmp";
+               } else {
+                       media_svc_error("Not proper img format");
+                       SAFE_FREE(thumb_dir);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
 
-       if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
-               thumbfile_ext = (char *)"jpg";
-       } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
-               thumbfile_ext = (char *)"png";
-       } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
-               thumbfile_ext = (char *)"gif";
-       } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
-               thumbfile_ext = (char *)"bmp";
+               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
        } else {
-               media_svc_error("Not proper img format");
-               SAFE_FREE(thumb_dir);
-               return MS_MEDIA_ERR_INTERNAL;
+               if (strcasecmp(file_ext, "PNG") == 0)
+                       snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
+               else
+                       snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
        }
 
-       snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
-       SAFE_STRLCPY(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
-       /*media_svc_debug("thumb_path is [%s]", thumb_path); */
-
        SAFE_FREE(thumb_dir);
 
        return MS_MEDIA_ERR_NONE;
@@ -952,17 +953,22 @@ int image_360_check(char *path)
        FILE *fp = NULL;
        long app1_size = 0;
        int size = 1;
-       unsigned char exif_header[4];
-       unsigned char exif_app1[2];
-       unsigned char exif_app1_xmp[2];
+       unsigned char exif_header[4] = {0, };
+       unsigned char exif_app1[2] = {0, };
+       unsigned char exif_app1_xmp[2] = {0, };
        long exif_app1_xmp_size = 0;
-       unsigned char exif_app1_xmp_t[2];
+       unsigned char exif_app1_xmp_t[2] = {0, };
        char *xmp_data = NULL;
        int size1 = 0;
        int size2 = 0;
        int fdata = 0;
        int temp = 0;
 
+       memset(exif_header, 0x00, sizeof(exif_header));
+       memset(exif_app1, 0x00, sizeof(exif_app1));
+       memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
+       memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
+
        fp = fopen(path, "rb");
        if (fp == NULL)
                goto ERROR;
@@ -1000,9 +1006,10 @@ int image_360_check(char *path)
 
                        exif_app1_xmp_size = size1 * 256 + size2 - 2;
 
-                       xmp_data = (char *)malloc(exif_app1_xmp_size);
-                       if (xmp_data != NULL) {
+                       if (exif_app1_xmp_size > 0) {
+                               xmp_data = (char *)malloc(exif_app1_xmp_size);
                                memset(xmp_data, 0x0, exif_app1_xmp_size);
+
                                ptr = xmp_data;
 
                                while (exif_app1_xmp_size >= 0) {
@@ -1026,7 +1033,7 @@ int image_360_check(char *path)
 
                                SAFE_FREE(xmp_data);
                        } else {
-                               media_svc_error("malloc failed");
+                               media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
                        }
 
                        if (fp) {
@@ -1232,7 +1239,7 @@ GET_WIDTH_HEIGHT:
                /*Get image width, height*/
                unsigned int img_width = 0;
                unsigned int img_height = 0;
-               mm_util_img_code_type img_type = IMG_CODEC_NONE;
+               mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
                mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
                if (content_info->media_meta.width == 0)
@@ -1446,7 +1453,6 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                                        SAFE_FREE(time_info);
                                } else {
                                        media_svc_error("memory allocation error");
-                                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
                                }
                        } else {
                                content_info->media_meta.recorded_date = g_strdup(p);
@@ -1536,7 +1542,7 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
 
                                mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
                                if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
-                                       ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
+                                       ret = _media_svc_get_thumbnail_path(content_info->storage_type, content_info->media_type, thumb_path, content_info->path, p, uid);
                                        if (ret != MS_MEDIA_ERR_NONE)
                                                media_svc_error("Fail to Get Thumbnail Path");
 
@@ -1770,16 +1776,225 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
        return;
 }
 
-int _media_svc_request_thumbnail(const char *path, char *thumb_path, int max_length, uid_t uid)
+int __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
+{
+       bool portrait = false;
+       double ratio;
+
+       if (orig_w < orig_h)
+               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;
+       } else {
+               if (orig_h < *thumb_h)
+                       *thumb_h = orig_h;
+               ratio = (double)orig_w / (double)orig_h;
+               *thumb_w = *thumb_h * ratio;
+       }
+
+       media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int _media_svc_create_thumbnail(const char *path, char *thumb_path, int max_length, media_svc_media_type_e media_type, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
+       unsigned int origin_w = 0;
+       unsigned int origin_h = 0;
+       unsigned int thumb_w = THUMB_WIDTH;
+       unsigned int thumb_h = THUMB_HEIGHT;
+       mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
 
-       ret = thumbnail_request_from_db(path, thumb_path, max_length, uid);
+       if (path == NULL || thumb_path == NULL) {
+               media_svc_error("Invalid parameter");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
+                       media_svc_error("Original path(%s) doesn't exist.", path);
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       if (max_length <= 0) {
+               media_svc_error("Length is invalid");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       ms_user_storage_type_e store_type = -1;
+       ret = ms_user_get_storage_type(uid, path, &store_type);
+
+       if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
+               media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
+
+       //1. make hash path
+       ret = _media_svc_get_thumbnail_path(store_type, media_type, thumb_path, path, THUMB_EXT, uid);
        if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("thumbnail_request_from_db failed: %d", ret);
-               ret = MS_MEDIA_ERR_INTERNAL;
+               media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
+               SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+               return ret;
+       }
+
+       //2. save thumbnail
+       if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
+               ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       media_svc_error("Getting image info is failed err: %d", ret);
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+
+               if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
+                       media_svc_error("This original image is too big");
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_THUMB_TOO_BIG;
+               }
+
+               __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
+               ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
+               if (ret != MM_UTIL_ERROR_NONE) {
+                       media_svc_error("mm_util_resize_P_P err: %d", ret);
+                       SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
        } else {
-               media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
+               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;
+               mm_util_image_h img = NULL;
+               mm_util_image_h resize_img = NULL;
+
+               /* Get Content Tag attribute for orientation */
+               ret = mm_file_create_tag_attrs(&tag, path);
+               mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
+
+               if (ret == FILEINFO_ERROR_NONE) {
+                       ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
+                       if (ret == 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;
+                               }
+                               media_svc_debug("There is tag rotate : %d", rot_type);
+                       } else {
+                               media_svc_debug("There is NOT tag rotate");
+                               rot_type = MM_UTIL_ROTATE_0;
+                               SAFE_FREE(err_msg);
+                       }
+
+                       ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &cdis_value, NULL);
+                       if (ret != FILEINFO_ERROR_NONE) {
+                               cdis_value = 0;
+                               SAFE_FREE(err_msg);
+                       }
+               } else {
+                       rot_type = MM_UTIL_ROTATE_0;
+                       cdis_value = 0;
+               }
+
+               ret = mm_file_destroy_tag_attrs(tag);
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("fail to free tag attr - err(%x)", ret);
+               }
+
+               if (cdis_value == 1) {
+                       media_svc_debug("This is CDIS vlaue 1");
+                       ret = mm_file_create_content_attrs_safe(&content, path);
+               } else {
+                       ret = mm_file_create_content_attrs(&content, path);
+               }
+
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("mm_file_create_content_attrs fails : %d", ret);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+
+               ret = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
+               if (ret != FILEINFO_ERROR_NONE) {
+                       media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+                       SAFE_FREE(err_msg);
+                       goto ERROR;
+               }
+
+               if (video_track_num > 0) {
+                       ret = mm_file_get_attrs(content, &err_msg,
+                                               MM_FILE_CONTENT_VIDEO_WIDTH,
+                                               &origin_w,
+                                               MM_FILE_CONTENT_VIDEO_HEIGHT,
+                                               &origin_h,
+                                               MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame, /* raw image is RGB888 format */
+                                               &size, NULL);
+
+                       if (ret != FILEINFO_ERROR_NONE) {
+                               media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+                               SAFE_FREE(err_msg);
+                               goto ERROR;
+                       }
+
+                       media_svc_debug("W[%d] H[%d] Size[%d] Frame[%p] Rotate[%d]", origin_w, origin_h, size, frame, rot_type);
+                       if (frame == NULL || origin_w == 0 || origin_h == 0) {
+                               media_svc_error("Failed to get frame data");
+                               goto ERROR;
+                       }
+
+                       ret = __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
+                       if (thumb_w <= 0 || thumb_h <= 0) {
+                               media_svc_error("Failed to get thumb size");
+                               goto ERROR;
+                       }
+
+                       media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", origin_w, origin_h, thumb_w, thumb_h);
+
+                       ret = mm_util_create_handle(&img, (unsigned char *)frame, origin_w, origin_h, size, MM_UTIL_COLOR_RGB24);
+                       if (origin_w > thumb_w || origin_h > thumb_h) {
+                               if (rot_type != MM_UTIL_ROTATE_0) {
+                                       ret = mm_util_resize_B_B(img, thumb_w, thumb_h, &resize_img);
+                                       if (ret != MM_UTIL_ERROR_NONE)
+                                               goto ERROR;
+                                       ret = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
+                               } else {
+                                       ret = mm_util_resize_B_P(img, thumb_w, thumb_h, thumb_path);
+                               }
+                       } else {
+                               if (rot_type != MM_UTIL_ROTATE_0)
+                                       ret = mm_util_rotate_B_P(img, rot_type, thumb_path);
+                               else
+                                       ret = mm_util_resize_B_P(img, origin_w, origin_h, thumb_path);
+                       }
+               }
+
+       ERROR:
+               mm_util_destroy_handle(img);
+               mm_util_destroy_handle(resize_img);
+               mm_file_destroy_content_attrs(content);
+
+               if (ret != MS_MEDIA_ERR_NONE)
+                       return MS_MEDIA_ERR_INTERNAL;
+
+               return MS_MEDIA_ERR_NONE;
        }
 
        return ret;