summaryrefslogtreecommitdiff
path: root/src/media-thumbnail.c
blob: 6b4960e2b1dbe49610226dfaf76ccd988191bb67 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * 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 <mm_file.h>
#include <mm_util_magick.h>
#include <media-util.h>
#include "media-thumbnail.h"
#include "media-thumbnail-debug.h"
#include <aul.h>

#define MAX_THUMB_SIZE 2000
#define MIME_TYPE_TIFF "image/tiff"
#define MIME_TYPE_ASF "application/vnd.ms-asf"
#define MIME_MAX_LEN 255

typedef enum {
    MEDIA_THUMB_INVALID = 0,
    MEDIA_THUMB_IMAGE,
    MEDIA_THUMB_VIDEO,
} thumbnail_media_type_e;

static void __get_rotation_and_cdis(const char *path, mm_util_rotate_type_e *rot_type, int *cdis_value)
{
	int err = MS_MEDIA_ERR_NONE;
	MMHandleType tag = (MMHandleType) NULL;
	char *p = NULL;
	int size = 0;
	int _cdis_value = 0;
	mm_util_rotate_type_e _rot_type = MM_UTIL_ROTATE_0;

	/* Get Content Tag attribute for orientation */
	err = mm_file_create_tag_attrs(&tag, path);
	if (err != FILEINFO_ERROR_NONE)
		return;

	err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
	if (err == FILEINFO_ERROR_NONE && size >= 0 && p) {
		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, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
	if (err != FILEINFO_ERROR_NONE)
		_cdis_value = 0;

	*rot_type = _rot_type;
	*cdis_value = _cdis_value;

	mm_file_destroy_tag_attrs(tag);
}

static int __get_video_meta(int cdis_value, const char *path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *frame_size)
{
	int err = MS_MEDIA_ERR_NONE;
	MMHandleType content = (MMHandleType) NULL;
	int _video_track_num = 0;
	unsigned int _width = 0;
	unsigned int _height = 0;
	size_t _frame_size = 0;
	void *_frame = NULL;

	if (cdis_value == 1) {
		thumb_warn("This is CDIS vlaue 1");
		err = mm_file_create_content_attrs_safe(&content, path);
	} else {
		err = mm_file_create_content_attrs(&content, path);
	}
	thumb_retvm_if(err != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_INTERNAL, "mm_file_create_content_attrs fails : %d", err);

	err = mm_file_get_attrs(content,
				MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num,
				MM_FILE_CONTENT_VIDEO_WIDTH, &_width,
				MM_FILE_CONTENT_VIDEO_HEIGHT, &_height,
				MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, &_frame_size, /* raw image is RGB888 format */
				NULL);

	if (err != FILEINFO_ERROR_NONE) {
		thumb_err("mm_file_get_attrs fails : %d", err);
		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;
	}

	if (!_frame || !_width || !_height) {
		thumb_err("wrong video info W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _frame_size, _frame);
		mm_file_destroy_content_attrs(content);
		return MS_MEDIA_ERR_INTERNAL;
	}

	*width = _width;
	*height = _height;
	*frame_size = _frame_size;
	*frame = g_memdup2(_frame, _frame_size);

	mm_file_destroy_content_attrs(content);

	return MS_MEDIA_ERR_NONE;
}


static int __get_video_info(const char *path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *frame_size, mm_util_rotate_type_e *rot_type)
{
	int err = MS_MEDIA_ERR_NONE;
	int _cdis_value = 0;
	mm_util_rotate_type_e _rot_type = MM_UTIL_ROTATE_0;

	__get_rotation_and_cdis(path, &_rot_type, &_cdis_value);
	err = __get_video_meta(_cdis_value, path, video_track_num, width, height, frame, frame_size);

	if (rot_type)
		*rot_type = _rot_type;

	return err;
}

static void __media_thumb_get_proper_thumb_size(unsigned int origin_width, unsigned int origin_height, unsigned int *thumb_width, unsigned int *thumb_height)
{
	double ratio = 0.0;

	thumb_retm_if(origin_width == 0, "Invalid origin_width");
	thumb_retm_if(origin_height == 0, "Invalid origin_height");
	thumb_retm_if(!thumb_width, "Invalid thumb_width");
	thumb_retm_if(!thumb_height, "Invalid thumb_height");

	thumb_dbg("origin thumb w: %d h: %d", *thumb_width, *thumb_height);

	/* Set smaller length to default size */
	if (origin_width < origin_height) {
		if (origin_width < *thumb_width)
			*thumb_width = origin_width;
		ratio = (double)origin_height / (double)origin_width;
		*thumb_height = *thumb_width * ratio;
	} else {
		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_width, *thumb_height);
}

static int __get_video_thumb_to_file(unsigned int width, unsigned int height, void *frame, size_t frame_size, mm_util_rotate_type_e 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;
	unsigned int thumb_w = thumb_width;
	unsigned int thumb_h = thumb_height;

	thumb_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");

	__media_thumb_get_proper_thumb_size(width, height, &thumb_w, &thumb_h);
	thumb_retv_if(thumb_w == 0 || thumb_h == 0, MS_MEDIA_ERR_INTERNAL);

	err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, frame_size, &img);
	thumb_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);

	if (width > thumb_w || height > thumb_h) {
		if (rot_type != MM_UTIL_ROTATE_0) {
			err = mm_util_resize_B_B(img, thumb_w, thumb_h, &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_w, thumb_h, 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);

	return (err == MM_UTIL_ERROR_NONE) ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_INTERNAL;
}

static int __get_video_thumb_to_buffer(unsigned int width, unsigned int height, void *frame, size_t frame_size, unsigned int thumb_width, unsigned int thumb_height, mm_util_image_h *dst_img)
{
	int err = MS_MEDIA_ERR_NONE;
	mm_util_image_h img = NULL;
	unsigned int thumb_w = thumb_width;
	unsigned int thumb_h = thumb_height;

	__media_thumb_get_proper_thumb_size(width, height, &thumb_w, &thumb_h);
	thumb_retv_if(thumb_w == 0 || thumb_h == 0, MS_MEDIA_ERR_INTERNAL);

	err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, frame_size, &img);
	thumb_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);

	if (width > thumb_w || height > thumb_h)
		err = mm_util_resize_B_B(img, thumb_w, thumb_h, dst_img);
	else
		err = mm_image_clone_image(img, dst_img);
	mm_image_destroy_image(img);

	return (err == MM_UTIL_ERROR_NONE) ? MS_MEDIA_ERR_NONE : MS_MEDIA_ERR_INTERNAL;
}

static int __check_path_validity(const char *path)
{
	thumb_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");

	if (access(path, R_OK) < 0) {
		if (errno == EACCES || errno == EPERM) {
			thumb_err("Fail to open path: Permission Denied [%s]", path);
			return  MS_MEDIA_ERR_PERMISSION_DENIED;
		} else {
			thumb_err("Fail to open path: Invalid Path [%s]", path);
			return MS_MEDIA_ERR_INVALID_PARAMETER;
		}
	}

	return MS_MEDIA_ERR_NONE;
}

static int __check_thumb_path_validity(const char *path)
{
	char *dir_name = NULL;
	int ret = MS_MEDIA_ERR_NONE;

	thumb_retvm_if(!THUMB_STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");

	dir_name = g_path_get_dirname(path);

	if (access(dir_name, W_OK) != 0) {
		if (errno == EACCES || errno == EPERM) {
			thumb_err("No permission to write[%s]", dir_name);
			ret = MS_MEDIA_ERR_PERMISSION_DENIED;
		} else {
			thumb_err("Does not exists[%s]", dir_name);
			ret = MS_MEDIA_ERR_INVALID_PARAMETER;
		}
	}

	g_free(dir_name);

	return ret;
}

static int __check_parameter_validity_for_file(const char *path, unsigned int width, unsigned int height, const char *thumb_path)
{
	int err = MS_MEDIA_ERR_NONE;

	thumb_retvm_if((width > MAX_THUMB_SIZE || width == 0), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid width[%d]", width);
	thumb_retvm_if((height > MAX_THUMB_SIZE || height == 0), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid height[%d]", height);

	/* Check path is accessible */
	err = __check_path_validity(path);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid path");

	/* Check thumbnail path is writable */
	err = __check_thumb_path_validity(thumb_path);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid thumb_path");

	return MS_MEDIA_ERR_NONE;
}

static int __check_parameter_validity_for_buffer(const char *path, unsigned int width, unsigned int height, unsigned char **thumb_buffer, size_t *thumb_size, unsigned int *thumb_width, unsigned int *thumb_height)
{
	thumb_retvm_if((width > MAX_THUMB_SIZE || width == 0), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid width[%d]", width);
	thumb_retvm_if((height > MAX_THUMB_SIZE || height == 0), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid height[%d]", height);
	thumb_retvm_if(!thumb_buffer || !thumb_size || !thumb_width || !thumb_height, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid out param");

	//Check path is accessible
	return __check_path_validity(path);
}

int create_video_thumbnail_to_file(const char *path, unsigned int width, unsigned int height, const char *thumb_path, bool auto_rotate)
{
	int err = MS_MEDIA_ERR_NONE;
	int video_track_num = 0;
	unsigned int video_width = 0;
	unsigned int video_height = 0;
	void *frame = NULL;
	size_t frame_size = 0;
	mm_util_rotate_type_e rot_type = MM_UTIL_ROTATE_NUM;

	err = __check_parameter_validity_for_file(path, width, height, thumb_path);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid parameter");

	//Get video info
	err = __get_video_info(path, &video_track_num, &video_width, &video_height, &frame, &frame_size, &rot_type);
	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 (!auto_rotate)
		rot_type = MM_UTIL_ROTATE_0;

	//Extract thumbnail
	err = __get_video_thumb_to_file(video_width, video_height, frame, frame_size, rot_type, thumb_path, width, height);
	g_free(frame);

	return err;
}

int create_video_thumbnail_to_buffer(const char *path,
								unsigned int width,
								unsigned int height,
								unsigned char **thumb_buffer,
								size_t *thumb_size,
								unsigned int *thumb_width,
								unsigned int *thumb_height)
{
	int err = MS_MEDIA_ERR_NONE;
	int video_track_num = 0;
	unsigned int video_w = 0;
	unsigned int video_h = 0;
	void *frame = NULL;
	size_t frame_size = 0;
	mm_util_image_h img = NULL;

	err = __check_parameter_validity_for_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid parameter");

	//Get video info
	err = __get_video_info(path, &video_track_num, &video_w, &video_h, &frame, &frame_size, NULL);
	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");

	//Extract thumbnail
	err = __get_video_thumb_to_buffer(video_w, video_h, frame, frame_size, width, height, &img);
	g_free(frame);
	if (err != MS_MEDIA_ERR_NONE)
		return err;

	err = mm_image_get_image(img, thumb_width, thumb_height, NULL, thumb_buffer, thumb_size);
	mm_image_destroy_image(img);

	return err;
}

static int __adjust_thumb_ratio(const char *path, unsigned int *width, unsigned int *height)
{
	int err = MS_MEDIA_ERR_NONE;
	unsigned int image_w = 0;
	unsigned int image_h = 0;
	mm_util_img_codec_type image_type = 0;

	err = mm_util_extract_image_info(path, &image_type, &image_w, &image_h);
	thumb_retvm_if(err != MM_UTIL_ERROR_NONE, MS_MEDIA_ERR_INTERNAL, "mm_util_extract_image_info: %d", err);
	thumb_retvm_if(image_type == IMG_CODEC_UNKNOWN_TYPE, MS_MEDIA_ERR_THUMB_UNSUPPORTED, "Unsupported image codec");

	__media_thumb_get_proper_thumb_size(image_w, image_h, width, height);

	return MS_MEDIA_ERR_NONE;
}

int create_image_thumbnail_to_file(const char *path, unsigned int width, unsigned int height, const char *thumb_path, bool auto_rotate)
{
	int err = MS_MEDIA_ERR_NONE;
	unsigned int thumb_w = width;
	unsigned int thumb_h = height;

	err = __check_parameter_validity_for_file(path, width, height, thumb_path);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid parameter");

	err = __adjust_thumb_ratio(path, &thumb_w, &thumb_h);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "__adjust_thumb_ratio failed");

	if (auto_rotate)
		err = mm_util_resize_and_rotate_P_P(path, thumb_w, thumb_h, thumb_path);
	else
		err = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
	thumb_retvm_if(err != MM_UTIL_ERROR_NONE, MS_MEDIA_ERR_INTERNAL, "mm_util_resize_P_P failed : %d", err);

	return MS_MEDIA_ERR_NONE;
}

int create_image_thumbnail_to_buffer(const char *path, unsigned int width, unsigned int height, unsigned char **thumb_buffer, size_t *thumb_size, unsigned int *thumb_width, unsigned int *thumb_height)
{
	int err = MS_MEDIA_ERR_NONE;
	unsigned int thumb_w = width;
	unsigned int thumb_h = height;
	mm_util_image_h img = NULL;

	err = __check_parameter_validity_for_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "Invalid parameter");

	err = __adjust_thumb_ratio(path, &thumb_w, &thumb_h);
	thumb_retvm_if(err != MS_MEDIA_ERR_NONE, err, "__adjust_thumb_ratio failed");

	err = mm_util_resize_P_B(path, thumb_w, thumb_h, MM_UTIL_COLOR_BGRA, &img);
	thumb_retvm_if(err != MM_UTIL_ERROR_NONE, MS_MEDIA_ERR_INTERNAL, "mm_util_resize_P_B failed : %d", err);

	err = mm_image_get_image(img, thumb_width, thumb_height, NULL, thumb_buffer, thumb_size);

	mm_image_destroy_image(img);

	return err;
}

static int __get_media_type(const char *path, thumbnail_media_type_e *media_type)
{
	int ret = MS_MEDIA_ERR_NONE;
	char mime[MIME_MAX_LEN] = {0,};

	ret = __check_path_validity(path);
	thumb_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	ret = aul_get_mime_from_file(path, mime, MIME_MAX_LEN);
	thumb_retvm_if(ret < 0, MS_MEDIA_ERR_INTERNAL, "aul_get_mime_from_file failed");

	thumb_dbg("mime type : %s", mime);

	if (strstr(mime, "image") != NULL) {
		thumb_retvm_if(!strcmp(mime, MIME_TYPE_TIFF), MS_MEDIA_ERR_THUMB_UNSUPPORTED, "Unsupported type");
		*media_type = MEDIA_THUMB_IMAGE;
		return MS_MEDIA_ERR_NONE;
	}

	if (strstr(mime, "video") != NULL) {
		*media_type = MEDIA_THUMB_VIDEO;
		return MS_MEDIA_ERR_NONE;
	}

	if (strcmp(mime, MIME_TYPE_ASF) == 0) {
		*media_type = MEDIA_THUMB_VIDEO;
		return MS_MEDIA_ERR_NONE;
	}

	return MS_MEDIA_ERR_THUMB_UNSUPPORTED;
}

int create_thumbnail_to_buffer(const char *path, unsigned int width, unsigned int height, unsigned char **thumb_buffer, size_t *thumb_size, unsigned int *thumb_width, unsigned int *thumb_height)
{
	int ret = MS_MEDIA_ERR_NONE;
	thumbnail_media_type_e type = MEDIA_THUMB_INVALID;

	ret = __get_media_type(path, &type);
	thumb_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "__get_media_type failed");

	if (type == MEDIA_THUMB_IMAGE)
		return create_image_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
	else
		return create_video_thumbnail_to_buffer(path, width, height, thumb_buffer, thumb_size, thumb_width, thumb_height);
}

int create_thumbnail_to_file(const char *path, unsigned int width, unsigned int height, const char *thumb_path)
{
	int ret = MS_MEDIA_ERR_NONE;
	thumbnail_media_type_e type = MEDIA_THUMB_INVALID;

	ret = __get_media_type(path, &type);
	thumb_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "__get_media_type failed");

	if (type == MEDIA_THUMB_IMAGE)
		return create_image_thumbnail_to_file(path, width, height, thumb_path, false);

	if (!g_regex_match_simple("[^/]\\.jpe?g$", thumb_path, G_REGEX_CASELESS, 0)) {
		thumb_err("Unsupported path or extensions [%s]", thumb_path);
		return MS_MEDIA_ERR_INVALID_PARAMETER;
	}

	return create_video_thumbnail_to_file(path, width, height, thumb_path, false);
}