summaryrefslogtreecommitdiff
path: root/src/media-thumb-internal.c
blob: 0238d15a7ed12aa8d06011d929d92498f7c20fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * libmedia-thumbnail
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-thumb-debug.h"
#include "media-thumb-util.h"
#include "media-thumb-internal.h"
#include "media-thumb-ipc.h"

#include "img-codec-parser.h"

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#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)
{
	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;
	}

	thumb_dbg("proper thumb w: %d h: %d", *thumb_w, *thumb_h);

	return MS_MEDIA_ERR_NONE;
}

int _media_thumb_general(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;
	mm_util_image_h img = NULL;
	unsigned char *buf = NULL;
	unsigned int width = 0;
	unsigned int height = 0;
	size_t size = 0;
	mm_util_magick_format format = MM_UTIL_IMG_FMT_NUM;

	if (thumb_path != NULL) {
		err = mm_util_resize_P_P(origin_path, thumb_width, thumb_height, thumb_path);
	} else {
		err = mm_util_resize_P_B(origin_path, thumb_width, thumb_height, MM_UTIL_IMG_FMT_BGRA8888, &img);
		if (err != MM_UTIL_ERROR_NONE) {
			thumb_err("mm_util_resize_P_B failed : %d", err);
			return MS_MEDIA_ERR_INTERNAL;
		}

		mm_util_get_image(img, &buf, &width, &height, &size, &format);
		thumb_info->data = calloc(1, size);
		memcpy(thumb_info->data, buf, size);
		thumb_info->size = size;
		thumb_info->width = width;
		thumb_info->height = height;
		SAFE_FREE(buf);
		mm_util_destroy_handle(img);
	}

	return err;
}

int _media_thumb_image(const char *origin_path, char *thumb_path, int thumb_width, int thumb_height, media_thumb_info *thumb_info)
{
	int err = MS_MEDIA_ERR_NONE;
	ImgCodecType image_type = 0;
	unsigned int origin_w = 0;
	unsigned int origin_h = 0;

	err = ImgGetImageInfo(origin_path, &image_type, &origin_w, &origin_h);
	if (err != MS_MEDIA_ERR_NONE) {
		thumb_warn("Getting image info is failed err: %d", err);
		return MS_MEDIA_ERR_INTERNAL;
	}

	if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM_FOR_THUMB)) {
		thumb_warn("This original image is too big");
		return MS_MEDIA_ERR_THUMB_TOO_BIG;
	}

	_media_thumb_get_proper_thumb_size(origin_w, origin_h, &thumb_width, &thumb_height);

	if (image_type == IMG_CODEC_PNG) {
		if (thumb_path != NULL) {
			char file_ext[10];
			err = _media_thumb_get_file_ext(origin_path, file_ext, sizeof(file_ext));
			if (strncasecmp(file_ext, "png", 3) == 0) {
				int len = strlen(thumb_path);
				thumb_path[len - 3] = 'p';
				thumb_path[len - 2] = 'n';
				thumb_path[len - 1] = 'g';
			}
		}
		err = _media_thumb_general(origin_path, thumb_path, thumb_width, thumb_height, thumb_info);
	} else if (image_type == IMG_CODEC_JPEG || image_type == IMG_CODEC_AGIF || image_type == IMG_CODEC_GIF || image_type == IMG_CODEC_BMP || image_type == IMG_CODEC_WBMP) {
		err = _media_thumb_general(origin_path, thumb_path, thumb_width, thumb_height, thumb_info);
	} else {
		thumb_warn("Unsupported image type");
		return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
	}

	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)
{
	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_magick_format res_format;

	/* 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) {
		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);
		} 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);
		}
	} else {
		rot_type = MM_UTIL_ROTATE_0;
		cdis_value = 0;
	}

	err = mm_file_destroy_tag_attrs(tag);
	if (err != FILEINFO_ERROR_NONE) {
		thumb_err("fail to free tag attr - err(%x)", err);
	}

	if (cdis_value == 1) {
		thumb_warn("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) {
		thumb_err("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) {
		thumb_err("mm_file_get_attrs fails : %s", err_msg);
		SAFE_FREE(err_msg);
		goto ERROR;
	}

	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;
		}

		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;
		}

		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;
		}

		thumb_dbg("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);

		err = mm_util_create_handle(&img, (unsigned char *)frame, width, height, size, MM_UTIL_IMG_FMT_RGB888);
		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_util_get_image(dst_img, &res_buf, &res_width, &res_height, &res_size, &res_format);
					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_util_get_image(resize_img, &res_buf, &res_width, &res_height, &res_size, &res_format);
					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 (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_util_get_image(dst_img, &res_buf, &res_width, &res_height, &res_size, &res_format);
					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, 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;
				}
			}
		}
	}

ERROR:
	mm_util_destroy_handle(img);
	mm_util_destroy_handle(resize_img);
	mm_util_destroy_handle(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_get_hash_name(const char *file_full_path, char *thumb_hash_path, size_t max_thumb_path, uid_t uid)
{
	char *hash_name = NULL;
	char file_ext[255] = { 0 };
	char *get_path = NULL;
	int ret_len = 0;
	ms_user_storage_type_e storage_type = -1;
	int ret = MS_MEDIA_ERR_NONE;

	if (file_full_path == NULL || thumb_hash_path == NULL || max_thumb_path <= 0) {
		thumb_err("file_full_path==NULL || thumb_hash_path == NULL || max_thumb_path <= 0");
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	_media_thumb_get_file_ext(file_full_path, file_ext, sizeof(file_ext));

	ret = ms_user_get_storage_type(uid, file_full_path, &storage_type);
	if ((ret != MS_MEDIA_ERR_NONE) || ((storage_type != MS_USER_STORAGE_INTERNAL) && (storage_type != MS_USER_STORAGE_EXTERNAL))) {
		thumb_err_slog("origin path(%s) is invalid. err : [%d] storage_type [%d]", file_full_path, ret, storage_type);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	hash_name = _media_thumb_generate_hash_name(file_full_path);
	if (hash_name == NULL) {
		thumb_err("_media_thumb_generate_hash_name fail");
		return MS_MEDIA_ERR_INTERNAL;
	}

	ret = ms_user_get_thumb_store_path(uid, storage_type, &get_path);
	if (get_path != NULL)
		ret_len = snprintf(thumb_hash_path, max_thumb_path - 1, "%s/.%s-%s.jpg", get_path, file_ext, hash_name);

	SAFE_FREE(get_path);

	if ((ret_len < 0) || (ret_len > (int)max_thumb_path)) {
		thumb_err("invalid hash path ret_len[%d]", ret_len);
		return MS_MEDIA_ERR_INTERNAL;
	}

	return MS_MEDIA_ERR_NONE;
}