Fix coverity issues
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index 89f2d72..16fb4fd 100755 (executable)
@@ -30,6 +30,7 @@
 #undef __USE_XOPEN
 #endif
 #include <string.h>
+#include <system_info.h>
 #include <sys/vfs.h>
 #include <glib/gstdio.h>
 #include <sys/stat.h>
 #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 <img-codec-parser.h>
-#include <image_util.h>
-#include <image_util_internal.h>
+#include <mm_util_magick.h>
 #include "media-util-err.h"
 #include "media-svc-util.h"
 #include "media-svc-db-utils.h"
 #define _MP4_FILE ".mp4"
 #define _ASF_FILE ".asf"
 #define MEDIA_SVC_ARTWORK_SIZE 2000
+#define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
+
+
+static int media_svc_pinyin_support = -1;
 
 typedef struct {
        char content_type[15];
@@ -297,7 +299,21 @@ time_t __media_svc_get_timeline_from_str(const char *timstr)
                if (t.tm_isdst != 0)
                        media_svc_debug("DST %d", t.tm_isdst);
 
-               modified_t = mktime(&t);
+               /* If time string has timezone */
+               if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
+                       GTimeVal timeval;
+                       char tim_tmp_str[255] = { 0, };
+
+                       /* ISO8601 Time string format */
+                       strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
+                       g_time_val_from_iso8601(tim_tmp_str, &timeval);
+                       modified_t = timeval.tv_sec;
+                       media_svc_debug("Calibrated timeval : [%d][%s]", modified_t, tim_tmp_str);
+               } else {
+                       /* Just localtime */
+                       modified_t = mktime(&t);
+               }
+
                if (modified_t > 0)
                        return modified_t;
                else
@@ -363,7 +379,7 @@ static int __media_svc_get_content_type_from_mime(const char *path, const char *
                }
        }
 
-       /*check music file in soun files. */
+       /*check music file in sound files. */
        if (*category & MEDIA_SVC_CATEGORY_SOUND) {
                int prefix_len = strlen(content_category[0].content_type) + 1;
 
@@ -495,150 +511,25 @@ static int __media_svc_get_location_value(MMHandleType tag, double *longitude, d
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __media_svc_encode_jpeg(unsigned char *src, unsigned long width, unsigned long height, image_util_colorspace_e colorspace, int quality, unsigned char **dst, unsigned long long *dst_size)
-{
-       int res = IMAGE_UTIL_ERROR_NONE;
-       image_util_encode_h encoder = NULL;
-       unsigned char *encoded_data = NULL;
-       res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_create failed! (%d)", res);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_set_resolution(encoder, width, height);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_set_colorspace(encoder, colorspace);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_set_quality(encoder, quality);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_set_quality failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_set_input_buffer(encoder, src);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_set_output_buffer(encoder, &encoded_data);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_encode_run(encoder, dst_size);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_run failed! (%d)", res);
-               image_util_encode_destroy(encoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       if (encoded_data != NULL) {
-               *dst = (unsigned char *)calloc(1, *dst_size);
-               if (*dst == NULL) {
-                       media_svc_error("memory allocation failed! (%lld)", *dst_size);
-                       image_util_encode_destroy(encoder);
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-               memcpy(*dst, encoded_data, *dst_size);
-       }
-       res = image_util_encode_destroy(encoder);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_encode_destroy failed! (%d)", res);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       SAFE_FREE(encoded_data);
-       return MS_MEDIA_ERR_NONE;
-}
-
-static int __media_svc_decode_jpeg(unsigned char *src, unsigned long long size, image_util_colorspace_e colorspace, unsigned char **dst, unsigned long *width, unsigned long *height, unsigned long long *dst_size)
-{
-       int res = IMAGE_UTIL_ERROR_NONE;
-       image_util_decode_h decoder = NULL;
-       res = image_util_decode_create(&decoder);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_create failed! (%d)", res);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_decode_set_input_buffer(decoder, src, size);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
-               image_util_decode_destroy(decoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_decode_set_colorspace(decoder, colorspace);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
-               image_util_decode_destroy(decoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_decode_set_output_buffer(decoder, dst);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
-               image_util_decode_destroy(decoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       res = image_util_decode_run(decoder, width, height, dst_size);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_run failed! (%d)", res);
-               image_util_decode_destroy(decoder);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-
-       res = image_util_decode_destroy(decoder);
-       if (res != IMAGE_UTIL_ERROR_NONE) {
-               media_svc_error("image_util_decode_destroy failed! (%d)", res);
-               return MS_MEDIA_ERR_INTERNAL;
-       }
-       return MS_MEDIA_ERR_NONE;
-}
-
-static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
+static int __media_svc_resize_artwork(const char *path, const char *img_format)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       unsigned char *raw_image = NULL;
-       int width = 0;
-       int height = 0;
-       unsigned long long raw_size = 0;
-       void *resized_raw_image = NULL;
-       int resized_width = 0;
-       int resized_height = 0;
-       unsigned int buf_size = 0;
-       image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
+       unsigned int width = 0;
+       unsigned int height = 0;
+       unsigned int resized_width = 0;
+       unsigned int resized_height = 0;
+       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] size [%d]", size);
-               /* decoding */
-               ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, &raw_size);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("__media_svc_decode_jpeg failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       return MS_MEDIA_ERR_NONE;
-               }
+               media_svc_debug("type [jpeg]");
 
-               if (raw_image == NULL) {
-                       media_svc_error("raw_image is null");
-                       *resize_image = image;
-                       *resize_size = size;
-                       return MS_MEDIA_ERR_NONE;
-               }
+               mm_util_extract_image_info(path, &img_type, &width, &height);
 
                if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
                        media_svc_debug("No need resizing");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(raw_image);
                        return MS_MEDIA_ERR_NONE;
                }
+
                /* resizing */
                if (width > height) {
                        resized_height = MEDIA_SVC_ARTWORK_SIZE;
@@ -648,57 +539,12 @@ static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, c
                        resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
                }
 
-               image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
-
-               resized_raw_image = malloc(buf_size);
+               ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
 
-               if (resized_raw_image == NULL) {
-                       media_svc_error("malloc failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(raw_image);
-                       return MS_MEDIA_ERR_NONE;
-               }
-
-               memset(resized_raw_image, 0, buf_size);
-
-               ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("image_util_resize failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(raw_image);
-                       SAFE_FREE(resized_raw_image);
-                       return MS_MEDIA_ERR_NONE;
-               }
-               SAFE_FREE(raw_image);
-
-               /* encoding */
-               ret = __media_svc_encode_jpeg((unsigned char *)resized_raw_image, (unsigned long)resized_width, (unsigned long)resized_height, colorspace, 90, resize_image, (unsigned long long *)resize_size);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       media_svc_error("__media_svc_encode_jpeg failed");
-                       *resize_image = image;
-                       *resize_size = size;
-                       SAFE_FREE(resized_raw_image);
-                       return MS_MEDIA_ERR_NONE;
-               }
-               SAFE_FREE(resized_raw_image);
-
-               if (*resize_image == NULL) {
-                       media_svc_error("*resize_image is null");
-                       *resize_image = image;
-                       *resize_size = size;
-                       return MS_MEDIA_ERR_NONE;
-               }
        } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
-               media_svc_debug("type [png] size [%d]", size);
-               *resize_image = image;
-               *resize_size = size;
-
+               media_svc_debug("type [png]");
        } else {
                media_svc_debug("Not proper img format");
-               *resize_image = image;
-               *resize_size = size;
        }
 
        return ret;
@@ -820,6 +666,23 @@ static char *__media_svc_get_title_from_filepath(const char *path)
        return title;
 }
 
+bool __media_svc_check_support_pinyin()
+{
+       int ret = SYSTEM_INFO_ERROR_NONE;
+       bool is_supported = false;
+
+       if (media_svc_pinyin_support == -1) {
+               ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
+               if (ret != SYSTEM_INFO_ERROR_NONE) {
+                       media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
+                       return false;
+               }
+
+               media_svc_pinyin_support = is_supported;
+       }
+
+       return media_svc_pinyin_support;
+}
 int _media_svc_rename_file(const char *old_name, const char *new_name)
 {
        if ((old_name == NULL) || (new_name == NULL)) {
@@ -877,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)) {
@@ -909,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;
@@ -1088,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;
@@ -1136,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) {
@@ -1162,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) {
@@ -1368,9 +1239,9 @@ GET_WIDTH_HEIGHT:
                /*Get image width, height*/
                unsigned int img_width = 0;
                unsigned int img_height = 0;
-               ImgCodecType img_type = IMG_CODEC_NONE;
+               mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
-               ImgGetImageInfo(path, &img_type, &img_width, &img_height);
+               mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
                if (content_info->media_meta.width == 0)
                        content_info->media_meta.width = img_width;
 
@@ -1484,13 +1355,9 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
        unsigned int size = 0;
        int mmf_error = FILEINFO_ERROR_NONE;
        char *err_attr_name = NULL;
-       bool extract_thumbnail = FALSE;
-       bool append_album = FALSE;
        int album_id = 0;
        int ret = MS_MEDIA_ERR_NONE;
        int cdis_value = 0;
-       unsigned int resize_size = 0;
-       unsigned char *resize_image = NULL;
 
        /*Get Content Tag attribute ===========*/
        mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
@@ -1561,9 +1428,16 @@ 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_RECDATE, &p, &size, NULL);
                if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
+                       char mime_type[255] = {0, };
+                       ret = __media_svc_get_mime_type(content_info->path, mime_type);
+                       /*if 3gp that audio only, media_type is music */
+                       if ((ret == MS_MEDIA_ERR_NONE) &&
+                               ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
+                               (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
                                /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
-                               char *time_info = (char*)calloc(1, (size + 1));
+                               char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
                                char *p_value = p;
                                char *time_value = time_info;
                                if (time_info != NULL) {
@@ -1575,26 +1449,31 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                                                time_value++;
                                                p_value++;
                                        }
-                                       *time_value = '\0';
                                        content_info->media_meta.recorded_date = g_strdup(time_info);
                                        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);
                        }
 
                        if (STRING_VALID(content_info->media_meta.recorded_date)) {
-                               /* This is same as datetaken */
-                               content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
-
                                content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
                                if (content_info->timeline == 0)
                                        content_info->timeline = content_info->modified_time;
                                else
                                        media_svc_debug("Timeline : %ld", content_info->timeline);
+
+                               /* This is same as datetaken */
+                               /* Remove compensation string */
+                               if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
+                                       content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
+                                       G_SAFE_FREE(content_info->media_meta.recorded_date);
+                                       content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
+                               } else {
+                                       content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
+                               }
                        }
                } else {
                        SAFE_FREE(err_attr_name);
@@ -1639,39 +1518,8 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        content_info->media_meta.rating = 0;
                }
 
-               /*Initialize album_id to 0. below code will set the album_id*/
-               content_info->album_id = album_id;
-               ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
-
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
-                               media_svc_debug("album does not exist. So start to make album art");
-                               extract_thumbnail = TRUE;
-                               append_album = TRUE;
-                       } else {
-                               extract_thumbnail = TRUE;
-                               append_album = FALSE;
-                       }
-               } else {
-                       content_info->album_id = album_id;
-                       append_album = FALSE;
-
-                       if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
-                               (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
-                               media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
-                               extract_thumbnail = TRUE;
-                       } else {
-                               media_svc_debug("album already exists. don't need to make album art");
-                               ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
-                               extract_thumbnail = TRUE;
-                       }
-               }
-
                /*Do not extract artwork for the USB Storage content*/
-               if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
-                       extract_thumbnail = FALSE;
-
-               if (extract_thumbnail == TRUE) {
+               if (content_info->storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
                        mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
                        if (mmf_error != FILEINFO_ERROR_NONE) {
                                media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
@@ -1687,43 +1535,54 @@ int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s
                        } else {
                                /*media_svc_debug("artwork size2 [%d]", size); */
                        }
+
                        if (image != NULL && size > 0) {
                                char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
                                int artwork_mime_size = -1;
 
                                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");
-                                       /* albumart resizing */
-                                       __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
+
+                                       if (strlen(thumb_path) > 0) {
+                                               ret = __media_svc_save_image(image, size, thumb_path, uid);
+                                               if (ret != MS_MEDIA_ERR_NONE) {
+                                                       media_svc_error("Fail to Save Image");
+                                               } else {
+                                                       /* albumart resizing */
+                                                       ret = __media_svc_resize_artwork(thumb_path, p);
+                                                       if (ret != MS_MEDIA_ERR_NONE) {
+                                                               media_svc_error("Fail to Make Thumbnail Image");
+                                                               _media_svc_remove_file(thumb_path);
+
+                                                       } else {
+                                                               content_info->thumbnail_path = g_strdup(thumb_path);
+                                                       }
+                                               }
+                                       }
                                } else {
                                        SAFE_FREE(err_attr_name);
                                }
-
-                               if (strlen(thumb_path) > 0) {
-                                       ret = __media_svc_save_image(resize_image, resize_size, thumb_path, uid);
-                                       if (ret != MS_MEDIA_ERR_NONE)
-                                               media_svc_error("Fail to Save Thumbnail Image");
-                                       else
-                                               content_info->thumbnail_path = g_strdup(thumb_path);
-                               }
-
-                               if (size != resize_size) {
-                                       media_svc_error("Albumart is resized");
-                                       SAFE_FREE(resize_image);
-                               }
                        }
                }
 
-               if (append_album == TRUE) {
-                       if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
-                               (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
-                               ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
-                       else
-                               ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
+               /*Initialize album_id to 0. below code will set the album_id*/
+               content_info->album_id = album_id;
+               ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+                               media_svc_debug("album does not exist. So start to make album art");
+                               if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
+                                       (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
+                                       ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
+                               else
+                                       ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
 
+                               content_info->album_id = album_id;
+                       }
+               } else {
                        content_info->album_id = album_id;
                }
 
@@ -1917,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;
+
+       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;
+       }
 
-       ret = thumbnail_request_from_db(path, thumb_path, max_length, uid);
+       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;
@@ -1960,8 +2028,10 @@ int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
 
 bool _media_svc_check_pinyin_support(void)
 {
-       /*Check CSC*/
-       return TRUE;
+       /* Check CSC : TODO : need to check CSC */
+
+       /* Check content.filter.pinyin feature */
+       return __media_svc_check_support_pinyin();
 }
 
 int _media_svc_get_media_type(const char *path, int *mediatype)