summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhj kim <backto.kim@samsung.com>2019-09-09 14:44:41 +0900
committerhj kim <backto.kim@samsung.com>2019-09-09 08:34:48 +0000
commit0c0fe86e226e6eecf86818b9d6e6e39e16ecc978 (patch)
treeb4941112689bd4c573e88d06187265bb31bd310f
parent57c856a3af34b9a79e769af5bfed622c34369532 (diff)
downloadlibmedia-service-0c0fe86e226e6eecf86818b9d6e6e39e16ecc978.tar.gz
libmedia-service-0c0fe86e226e6eecf86818b9d6e6e39e16ecc978.tar.bz2
libmedia-service-0c0fe86e226e6eecf86818b9d6e6e39e16ecc978.zip
Update _media_svc_create_thumbnail to reduce Cyclomatic Complexity
Change-Id: Ie6bbbffa29083b45a32d6e5bb9b8fedabe7d92de
-rwxr-xr-xsrc/common/media-svc-util.c323
1 files changed, 195 insertions, 128 deletions
diff --git a/src/common/media-svc-util.c b/src/common/media-svc-util.c
index 25493a3..7d6c70a 100755
--- a/src/common/media-svc-util.c
+++ b/src/common/media-svc-util.c
@@ -1719,6 +1719,200 @@ int __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h,
return MS_MEDIA_ERR_NONE;
}
+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 tag = (MMHandleType) NULL;
+ char *p = NULL;
+ int size = 0;
+ 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);
+ if (err != FILEINFO_ERROR_NONE) {
+ *rot_type = MM_UTIL_ROTATE_0;
+ *cdis_value = 0;
+ return;
+ }
+
+ 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;
+ }
+ 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);
+ }
+
+ 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) {
+ media_svc_error("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) {
+ media_svc_debug("This is CDIS vlaue 1");
+ err = mm_file_create_content_attrs_safe(&content, origin_path);
+ } else {
+ err = mm_file_create_content_attrs(&content, origin_path);
+ }
+
+ if (err != FILEINFO_ERROR_NONE) {
+ media_svc_error("mm_file_create_content_attrs fails : %d", err);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ err = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
+ if (err != FILEINFO_ERROR_NONE) {
+ media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+ SAFE_FREE(err_msg);
+ mm_file_destroy_content_attrs(content);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ *video_track_num = _video_track_num;
+
+ if (_video_track_num == 0) {
+ mm_file_destroy_content_attrs(content);
+ return MS_MEDIA_ERR_NONE;
+ }
+
+ 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) {
+ media_svc_error("mm_file_get_attrs fails : %s", err_msg);
+ SAFE_FREE(err_msg);
+ mm_file_destroy_content_attrs(content);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ media_svc_debug("W[%d] H[%d] Size[%zu] 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, unsigned int thumb_width, unsigned int thumb_height)
+{
+ int err = MS_MEDIA_ERR_NONE;
+ mm_util_image_h img = NULL;
+ mm_util_image_h resize_img = NULL;
+
+ media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
+ __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
+ if (thumb_width <= 0 || thumb_height <= 0) {
+ media_svc_error("Failed to get thumb size");
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ media_svc_debug("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);
+ media_svc_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) {
+ 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_P(img, thumb_width, thumb_height, thumb_path);
+ }
+ } else {
+ if (rot_type != MM_UTIL_ROTATE_0)
+ err = mm_util_rotate_B_P(img, rot_type, thumb_path);
+ else
+ err = mm_util_resize_B_P(img, width, height, thumb_path);
+ }
+
+ERROR:
+ mm_image_destroy_image(img);
+ mm_image_destroy_image(resize_img);
+ if (err != MS_MEDIA_ERR_NONE)
+ return MS_MEDIA_ERR_INTERNAL;
+
+ return MS_MEDIA_ERR_NONE;
+}
+
+static int __create_video_thumbnail(const char *path, char *thumb_path)
+{
+ int ret = MS_MEDIA_ERR_NONE;
+ int cdis_value = 0;
+ unsigned int width = 0;
+ unsigned int height = 0;
+ void *frame = NULL;
+ int video_track_num = 0;
+ size_t size = 0;
+ mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
+
+ __get_rotation_and_cdis(path, &rot_type, &cdis_value);
+ ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
+ media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
+ media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
+
+ ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
+
+ SAFE_FREE(frame);
+
+ return ret;
+}
+
int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
{
int ret = MS_MEDIA_ERR_NONE;
@@ -1784,134 +1978,7 @@ int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_me
return MS_MEDIA_ERR_INTERNAL;
}
} else {
- 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_image_create_image(origin_w, origin_h, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
- 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_image_destroy_image(img);
- mm_image_destroy_image(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 __create_video_thumbnail(path, thumb_path);
}
return ret;