update latest
[framework/multimedia/libmm-utility.git] / imgp / mm_util_imgp.c
1 /*
2  * libmm-utility
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <limits.h>
22 #include <mm_debug.h>
23 #include "mm_util_imgp.h"
24 #include "mm_util_imgp_internal.h"
25 #include <gmodule.h>
26 #include <mm_error.h>
27
28 #define MM_UTIL_ROUND_UP_2(num)  (((num)+1)&~1)
29 #define MM_UTIL_ROUND_UP_4(num)  (((num)+3)&~3)
30 #define MM_UTIL_ROUND_UP_8(num)  (((num)+7)&~7)
31 #define MM_UTIL_ROUND_UP_16(num)  (((num)+15)&~15)
32 #define GEN_MASK(x) ((1<<(x))-1)
33 #define ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) & ~GEN_MASK(x))
34 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
35 #define GST "gstcs"
36
37 typedef gboolean(*IMGPInfoFunc)  (imgp_info_s*, imgp_plugin_type_e);
38 /*########################################################################################*/
39 #define setup_image_size_I420(width, height) { \
40         int size=0; \
41         size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) + MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) /2); \
42         return size; \
43 }
44
45 #define setup_image_size_Y42B(width, height)  { \
46         int size=0; \
47         size = (MM_UTIL_ROUND_UP_4 (width) * height + MM_UTIL_ROUND_UP_8 (width)  * height); \
48         return size; \
49 }
50
51 #define setup_image_size_Y444(width, height) { \
52         int size=0; \
53         size = (MM_UTIL_ROUND_UP_4 (width) * height  * 3); \
54         return size; \
55 }
56
57 #define setup_image_size_UYVY(width, height) { \
58         int size=0; \
59         size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
60         return size; \
61 }
62
63 #define setup_image_size_YUYV(width, height)  { \
64         int size=0; \
65         size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
66         return size; \
67 }
68
69 #define setup_image_size_YV12(width, height) { \
70         int size=0; \
71         size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height)+ MM_UTIL_ROUND_UP_8 (width) * MM_UTIL_ROUND_UP_2 (height) / 2); \
72         return size; \
73 }
74
75 #define setup_image_size_NV12(width, height) { \
76         int size=0; \
77         size = (MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5); \
78         return size; \
79 }
80
81 #define setup_image_size_RGB565(width, height)  { \
82         int size=0; \
83         size = (MM_UTIL_ROUND_UP_4 (width * 2) *height); \
84         return size; \
85 }
86
87 #define setup_image_size_RGB888(width, height)  { \
88         int size=0; \
89         size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
90         return size; \
91 }
92
93 #define setup_image_size_BGR888(width, height)  { \
94         int size=0; \
95         size = (MM_UTIL_ROUND_UP_4 (width*3) * height); \
96         return size; \
97 }
98
99 #define setup_image_size_SN12(width, height) { \
100         int size=0; \
101         size = MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5;  \
102         return size; \
103 }
104
105 #define setup_image_size_ST12(width, height) { \
106         int size=0; \
107         size = MM_UTIL_ROUND_UP_4 (width) * MM_UTIL_ROUND_UP_2 (height) *1.5;  \
108         return size; \
109 }
110
111 #define setup_image_size_UYVY(width, height) { \
112         int size=0; \
113         size = (MM_UTIL_ROUND_UP_2 (width) * 2 * height); \
114         return size; \
115 }
116
117 #define setup_image_size_BGRX888(width, height) {  \
118         int size=0; \
119         size = MM_UTIL_ROUND_UP_4 (width*3) * height); \
120         return size; \
121 }
122
123 /*########################################################################################*/
124
125 static int
126 check_valid_picture_size(int width, int height)
127 {
128         if((int)width>0 && (int)height>0 && (width+128)*(unsigned long long)(height+128) < INT_MAX/4) {
129                 return MM_ERROR_NONE;
130         }
131         return MM_ERROR_IMAGE_INVALID_VALUE;
132 }
133
134 static int
135 _mm_setup_image_size(const char* _format_label, int width, int height)
136 {
137         int size=0;
138         if(strcmp(_format_label, "I420") == 0) {
139                 setup_image_size_I420(width, height); //width * height *1.5;
140         }else if(strcmp(_format_label, "Y42B") == 0)     {
141                 setup_image_size_Y42B(width, height); //width * height *2;
142         }else if(strcmp(_format_label, "YUV422") == 0) {
143                 setup_image_size_Y42B(width, height); //width * height *2;
144         }else if(strcmp(_format_label, "Y444") == 0) {
145                 setup_image_size_Y444(width, height); //width * height *3;
146         }else if(strcmp(_format_label, "YV12") == 0)     {
147                 setup_image_size_YV12(width, height); //width * height *1.5; width must be 8 multiple
148         }else if(strcmp(_format_label, "NV12") == 0) {
149                 setup_image_size_NV12(width, height) //width * height *1.5;
150         }else if(strcmp(_format_label, "ST12") == 0) {
151                 setup_image_size_ST12(width, height); //width * height *1.5;
152         }else if(strcmp(_format_label, "SN12") == 0) {
153                 setup_image_size_SN12(width, height) //width * height *1.5;
154         }else if(strcmp(_format_label, "UYVY") == 0) {
155                 setup_image_size_UYVY(width, height); //width * height *2;
156         }else if(strcmp(_format_label, "YUYV") == 0) {
157                 setup_image_size_YUYV(width, height); //width * height *2;
158         }else if(strcmp(_format_label, "RGB565") == 0) {
159                 setup_image_size_RGB565(width, height); //width * height *2;
160         }else if(strcmp(_format_label, "RGB888") == 0) {
161                 setup_image_size_RGB888(width, height); //width * height *3;
162         }else if(strcmp(_format_label, "BGR888") == 0) {
163                 setup_image_size_BGR888(width, height);//width * height *3;
164         }else if(strcmp(_format_label, "ARGB8888") == 0) {
165                 size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);
166         }else if(strcmp(_format_label, "BGRA8888") == 0) {
167                 size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);
168         }else if(strcmp(_format_label, "RGBA8888") == 0) {
169                  size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);
170         }else if(strcmp(_format_label, "ABGR8888") == 0) {
171                  size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);
172         }else if(strcmp(_format_label, "BGRX") == 0) {
173                  size = width * height *4; mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] file_size: %d\n", __func__, __LINE__, size);
174         }
175         return size;
176 }
177
178 static gboolean
179 _mm_cannot_convert_format(mm_util_img_format src_format, mm_util_img_format dst_format )
180 {
181         gboolean _bool=FALSE;
182         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d]  src_format: %d,  dst_format:%d", __func__, __LINE__, src_format, dst_format);
183         if(((src_format == MM_UTIL_IMG_FMT_YUV422) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
184
185                 ((src_format == MM_UTIL_IMG_FMT_NV12) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
186
187                 ((src_format == MM_UTIL_IMG_FMT_UYVY) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
188
189                 ((src_format == MM_UTIL_IMG_FMT_YUYV) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
190
191                 ((src_format == MM_UTIL_IMG_FMT_RGB565) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
192
193                 ((src_format == MM_UTIL_IMG_FMT_RGB888) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
194
195                 ((src_format == MM_UTIL_IMG_FMT_BGRX8888) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
196                 
197                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_YUV422))  || ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_UYVY))  ||
198                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_YUYV))  || ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_ARGB8888))  ||
199                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_RGBA8888))  ||((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_BGRX8888)) ) {
200
201                 _bool = TRUE;
202         }
203
204         return _bool;
205 }
206
207 static gboolean
208 _mm_gst_can_resize_format(char* __format_label)
209 {
210         gboolean _bool = FALSE;
211         mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] Format label: %s", __func__, __LINE__,__format_label);
212         if(strcmp(__format_label, "AYUV") == 0
213                 || strcmp(__format_label, "UYVY") == 0 ||strcmp(__format_label, "Y800") == 0 || strcmp(__format_label, "I420") == 0  || strcmp(__format_label, "YV12") == 0
214                 || strcmp(__format_label, "RGB888") == 0  || strcmp(__format_label, "RGB565") == 0 || strcmp(__format_label, "BGR888") == 0  || strcmp(__format_label, "RGBA8888") == 0
215                 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ||strcmp(__format_label, "RGBX") == 0
216                 ||strcmp(__format_label, "XRGB") == 0 ||strcmp(__format_label, "BGRX") == 0 ||strcmp(__format_label, "XBGR") == 0 ||strcmp(__format_label, "Y444") == 0
217                 ||strcmp(__format_label, "Y42B") == 0 ||strcmp(__format_label, "YUY2") == 0 ||strcmp(__format_label, "YUYV") == 0 ||strcmp(__format_label, "UYVY") == 0
218                 ||strcmp(__format_label, "Y41B") == 0 ||strcmp(__format_label, "Y16") == 0 ||strcmp(__format_label, "Y800") == 0 ||strcmp(__format_label, "Y8") == 0
219                 ||strcmp(__format_label, "GREY") == 0 ||strcmp(__format_label, "AY64") == 0 || strcmp(__format_label, "YUV422") == 0) {
220
221                 _bool=TRUE;
222         }
223         return _bool;
224 }
225
226 static gboolean
227 _mm_gst_can_rotate_format(const char* __format_label)
228 {
229         gboolean _bool = FALSE;
230         mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] Format label: %s boolean: %d", __func__, __LINE__,__format_label, _bool);
231         if(strcmp(__format_label, "I420") == 0 ||strcmp(__format_label, "YV12") == 0 || strcmp(__format_label, "IYUV") == 0
232                 || strcmp(__format_label, "RGB888") == 0||strcmp(__format_label, "BGR888") == 0 ||strcmp(__format_label, "RGBA8888") == 0
233                 || strcmp(__format_label, "ARGB8888") == 0 ||strcmp(__format_label, "BGRA8888") == 0 ||strcmp(__format_label, "ABGR8888") == 0 ) {
234                 _bool=TRUE;
235         }
236         mmf_debug(MMF_DEBUG_LOG,"[%s][%05d] boolean: %d", __func__, __LINE__,_bool);
237         return _bool;
238 }
239
240 static gboolean
241 _mm_select_convert_plugin(mm_util_img_format src_format, mm_util_img_format dst_format )
242 {
243         gboolean _bool=FALSE;
244         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d]  src_format: %d,  dst_format:%d", __func__, __LINE__, src_format, dst_format);
245         if(((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_NV12))  || ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  ||
246                 ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  || ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_ARGB8888))  ||
247                 ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_BGRA8888))  || ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_RGBA8888))  ||
248                 ((src_format == MM_UTIL_IMG_FMT_YUV420) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
249
250                 ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_NV12))  || ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  ||
251                 ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  || ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_ARGB8888))  ||
252                 ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_BGRA8888))  || ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_RGBA8888))  ||
253                 ((src_format == MM_UTIL_IMG_FMT_I420) && (dst_format == MM_UTIL_IMG_FMT_NV12_TILED))  ||
254
255                 ((src_format == MM_UTIL_IMG_FMT_NV12) && (dst_format == MM_UTIL_IMG_FMT_YUV420))  || ((src_format == MM_UTIL_IMG_FMT_NV12) && (dst_format == MM_UTIL_IMG_FMT_I420))  ||
256                 ((src_format == MM_UTIL_IMG_FMT_NV12) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  || ((src_format == MM_UTIL_IMG_FMT_NV12) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  ||
257
258                 ((src_format == MM_UTIL_IMG_FMT_UYVY) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  || ((src_format == MM_UTIL_IMG_FMT_UYVY) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  ||
259
260                 ((src_format == MM_UTIL_IMG_FMT_YUYV) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  || ((src_format == MM_UTIL_IMG_FMT_YUYV) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  ||
261
262                 ((src_format == MM_UTIL_IMG_FMT_RGB565) && (dst_format == MM_UTIL_IMG_FMT_YUV420))  || ((src_format == MM_UTIL_IMG_FMT_RGB565) && (dst_format == MM_UTIL_IMG_FMT_I420))  ||
263                 ((src_format == MM_UTIL_IMG_FMT_RGB565) && (dst_format == MM_UTIL_IMG_FMT_NV12))  ||
264                 
265                 ((src_format == MM_UTIL_IMG_FMT_RGB888) && (dst_format == MM_UTIL_IMG_FMT_YUV420))  || ((src_format == MM_UTIL_IMG_FMT_RGB888) && (dst_format == MM_UTIL_IMG_FMT_I420))  ||
266                 ((src_format == MM_UTIL_IMG_FMT_RGB888) && (dst_format == MM_UTIL_IMG_FMT_NV12))  ||
267
268                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_YUV420))  || ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_I420))  ||
269                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_NV12))  || ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_RGB565))  ||
270                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_RGB888))  ||
271                 ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_BGRA8888))  || ((src_format == MM_UTIL_IMG_FMT_NV12_TILED) && (dst_format == MM_UTIL_IMG_FMT_RGBA8888))) {
272
273                 _bool = TRUE;
274         }
275
276         return _bool;
277 }
278
279 static gboolean
280 _mm_select_resize_plugin(mm_util_img_format _format)
281 {
282         gboolean _bool = FALSE;
283         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d]  _format: %d", __func__, __LINE__, _format);
284         if( (_format == MM_UTIL_IMG_FMT_UYVY) || (_format == MM_UTIL_IMG_FMT_YUYV) || (_format == MM_UTIL_IMG_FMT_RGBA8888) || (_format == MM_UTIL_IMG_FMT_BGRX8888) ) {
285                 _bool = FALSE;
286         }else {
287                 _bool = TRUE;
288         }
289         return _bool;
290 }
291
292 static gboolean
293 _mm_select_rotate_plugin(mm_util_img_format _format, unsigned int width, unsigned int height, mm_util_img_rotate_type angle)
294 {
295         gboolean _bool = FALSE;
296         gboolean _rgb_Flag = FALSE;
297         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _format: %d", __func__, __LINE__, _format);
298
299         if( _format == MM_UTIL_IMG_FMT_RGB888 ||_format == MM_UTIL_IMG_FMT_RGB565) {
300                 unsigned int imgsize = 0;
301                 unsigned int rotate_imgsize = 0;
302                 mm_util_get_image_size(_format, width, height, &imgsize);
303                 mm_util_get_image_size(_format, height, width, &rotate_imgsize);
304
305                 if( (imgsize == rotate_imgsize) ||((imgsize != rotate_imgsize) && angle == MM_UTIL_ROTATE_90) ) { //constraint of image processing because MM_UTIL_ROTATE_180 may be twice MM_UTIL_ROTATE_90
306                         _rgb_Flag = TRUE;
307                 }
308         }
309
310         if( (_format == MM_UTIL_IMG_FMT_YUV420) || (_format == MM_UTIL_IMG_FMT_I420) || (_format == MM_UTIL_IMG_FMT_NV12) ||(_rgb_Flag) ) {
311                 _bool = TRUE;
312         }
313
314                 return _bool;
315 }
316
317 static int
318 _mm_confirm_dst_width_height(unsigned int src_width, unsigned int src_height, unsigned int *dst_width, unsigned int *dst_height, mm_util_img_rotate_type angle)
319 {
320         int ret = MM_ERROR_NONE;
321
322         if(!dst_width || !dst_height) {
323                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] dst_width || dst_height Buffer is NULL", __func__, __LINE__);
324                 return MM_ERROR_IMAGE_INVALID_VALUE;
325         }
326
327         switch(angle) {
328                 case MM_UTIL_ROTATE_0:
329                 case MM_UTIL_ROTATE_180:
330                 case MM_UTIL_ROTATE_FLIP_HORZ:
331                 case MM_UTIL_ROTATE_FLIP_VERT:
332                         if(*dst_width != src_width) {
333                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] *dst_width: %d", __func__, __LINE__, *dst_width);
334                                 *dst_width = src_width;
335                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #Confirmed# *dst_width: %d", __func__, __LINE__, *dst_width);
336                         }
337                         if(*dst_height != src_height) {
338                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] *dst_height: %d", __func__, __LINE__, *dst_height);
339                                 *dst_height = src_height;
340                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #Confirmed# *dst_height: %d", __func__, __LINE__, *dst_height);
341                         }
342                         break;
343                 case MM_UTIL_ROTATE_90:
344                 case MM_UTIL_ROTATE_270:
345                         if(*dst_width != src_height) {
346                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] *dst_width: %d", __func__, __LINE__, *dst_width);
347                                 *dst_width = src_height;
348                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #Confirmed# *dst_width: %d", __func__, __LINE__, *dst_width);
349                         }
350                         if(*dst_height != src_width) {
351                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] *dst_height: %d", __func__, __LINE__, *dst_height);
352                                 *dst_height = src_width;
353                                 mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] #Confirmed# *dst_height: %d", __func__, __LINE__, *dst_height);
354                         }
355                         break;
356
357                 default:
358                         mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] Not supported rotate value\n", __func__, __LINE__);
359                         return MM_ERROR_IMAGE_INVALID_VALUE;
360         }
361         return ret;
362 }
363
364 static int
365 _mm_set_format_label(char* format_label, mm_util_img_format _format)
366 {
367         int ret = MM_ERROR_NONE;
368         if(format_label == NULL) {
369                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] format_label: %s", __func__, __LINE__, format_label);
370                 return MM_ERROR_IMAGE_INVALID_VALUE;
371         }
372
373         if(_format == MM_UTIL_IMG_FMT_YUV420) {
374                 strncpy(format_label, "YV12", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
375         }else if(_format == MM_UTIL_IMG_FMT_YUV422) {
376                 strncpy(format_label, "Y42B", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
377         }else if(_format == MM_UTIL_IMG_FMT_I420) {
378                 strncpy(format_label, "I420", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
379         }else if(_format == MM_UTIL_IMG_FMT_NV12) {
380                 strncpy(format_label, "NV12", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
381         }else if(_format == MM_UTIL_IMG_FMT_UYVY) {
382                 strncpy(format_label, "UYVY", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
383         }else if(_format == MM_UTIL_IMG_FMT_YUYV) {
384                 strncpy(format_label, "YUYV", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
385         }else if(_format ==MM_UTIL_IMG_FMT_RGB565) {
386                 strncpy(format_label, "RGB565", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
387         }else if(_format ==MM_UTIL_IMG_FMT_RGB888) {
388                 strncpy(format_label, "RGB888", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
389         }else if(_format ==MM_UTIL_IMG_FMT_ARGB8888) {
390                 strncpy(format_label, "ARGB8888", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
391         }else if(_format ==MM_UTIL_IMG_FMT_BGRA8888) {
392                 strncpy(format_label, "BGRA8888", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
393         }else if(_format ==MM_UTIL_IMG_FMT_RGBA8888) {
394                 strncpy(format_label, "RGBA8888", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
395         }else if(_format ==MM_UTIL_IMG_FMT_BGRX8888) {
396                 strncpy(format_label, "BGRX", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
397         }else if(_format ==MM_UTIL_IMG_FMT_NV12_TILED) {
398                 strncpy(format_label, "NV12T", IMAGE_FORMAT_LABEL_BUFFER_SIZE);
399         }else {
400                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] ERROR - You check mm_util_img_format", __func__, __LINE__);
401         }
402
403         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] format_label: %s", __func__, __LINE__, format_label);
404         return ret;
405 }
406
407 static int
408 _mm_set_imgp_info_s(imgp_info_s * _imgp_info_s, unsigned char *src,  mm_util_img_format src_format, unsigned int src_width, unsigned int src_height, mm_util_img_format dst_format, unsigned int dst_width, unsigned int dst_height, mm_util_img_rotate_type angle)
409 {
410         int ret = MM_ERROR_NONE;
411         if(_imgp_info_s == NULL) {
412                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _imgp_info_s is NULL", __func__, __LINE__);
413                 return MM_ERROR_IMAGE_INVALID_VALUE;
414         }
415
416         unsigned int src_size=0;
417         unsigned int dst_size=0;
418
419         char input_format_label[IMAGE_FORMAT_LABEL_BUFFER_SIZE];
420         char output_format_label[IMAGE_FORMAT_LABEL_BUFFER_SIZE];
421
422         ret=_mm_set_format_label(input_format_label, src_format); 
423         ret=_mm_set_format_label(output_format_label, dst_format);
424         if(ret != MM_ERROR_NONE) {
425                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] mm_set_format_label error", __func__, __LINE__);
426                 return MM_ERROR_IMAGE_INVALID_VALUE;
427         }
428         strncpy(_imgp_info_s->input_format_label, input_format_label, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
429         mm_util_get_image_size(src_format, src_width, src_height, &src_size);
430         if(src_size != _mm_setup_image_size(input_format_label, src_width, src_height)) {
431                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] src image size error", __func__, __LINE__);
432         }
433         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] src_size: %d\n", __func__, __LINE__, src_size);
434         _imgp_info_s->src=(unsigned char*)malloc(sizeof(char*) * src_size);
435         if(_imgp_info_s->src == NULL) {
436                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _imgp_info_s->src is NULL", __func__, __LINE__);
437                 free(_imgp_info_s->src);
438                 _imgp_info_s->src = NULL;
439                 return MM_ERROR_IMAGE_FILEOPEN;
440         }
441
442         memcpy(_imgp_info_s->src, src, src_size);
443         _imgp_info_s->src_format=src_format;
444         _imgp_info_s->src_width = src_width;
445         _imgp_info_s->src_height= src_height;
446
447         strncpy(_imgp_info_s->output_format_label, output_format_label, IMAGE_FORMAT_LABEL_BUFFER_SIZE);
448         mm_util_get_image_size(dst_format, dst_width, dst_height, &dst_size);
449         if(dst_size != _mm_setup_image_size(output_format_label,dst_width, dst_height)) {
450                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] dst image size error", __func__, __LINE__);
451         }
452         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] dst_size: %d\n", __func__, __LINE__, dst_size);
453         _imgp_info_s->dst=(unsigned char*)malloc(sizeof(char*) * dst_size);
454         if(_imgp_info_s->dst == NULL) {
455                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _imgp_info_s->src is NULL", __func__, __LINE__);
456                 free(_imgp_info_s->dst);
457                 _imgp_info_s->dst = NULL;
458                 return MM_ERROR_IMAGE_FILEOPEN;
459         }
460         _imgp_info_s->dst_format=dst_format;
461         _imgp_info_s->dst_width = dst_width;
462         _imgp_info_s->dst_height = dst_height;
463         _imgp_info_s->angle= angle;
464
465         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] [input] format label : %s  src: %p width: %d  height: %d [output] format label: %s width: %d height: %d rotation_value: %d",
466         __func__, __LINE__, _imgp_info_s->input_format_label, _imgp_info_s->src, _imgp_info_s->src_width, _imgp_info_s->src_height,
467         _imgp_info_s->output_format_label, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->angle);
468
469         return ret;
470 }
471
472 static GModule *
473 _mm_util_imgp_initialize(imgp_plugin_type_e _imgp_plugin_type_e)
474 {
475         GModule *module = NULL;
476         mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #Start dlopen#", __func__, __LINE__ );
477
478         if( _imgp_plugin_type_e == IMGP_NEON ) {
479                 module = g_module_open(PATH_NEON_LIB, G_MODULE_BIND_LAZY );
480         }else if( _imgp_plugin_type_e == IMGP_GSTCS) {
481                 module = g_module_open(PATH_GSTCS_LIB, G_MODULE_BIND_LAZY );
482         }
483         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] #Success g_module_open#", __func__, __LINE__ );
484         if( module == NULL ) {
485                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] %s | %s module open failed", __func__, __LINE__, PATH_NEON_LIB, PATH_GSTCS_LIB);
486                 return NULL;
487         }
488         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] module: %p,  g_module_name: %s", __func__, __LINE__, module, g_module_name (module));
489         return module;
490 }
491
492 static IMGPInfoFunc
493 _mm_util_imgp_process(GModule *module)
494 {
495         IMGPInfoFunc mm_util_imgp_func = NULL;
496
497         if(module == NULL) {
498                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] module is NULL", __func__, __LINE__);
499                 return NULL;
500         }
501
502         mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #_mm_util_imgp_process#", __func__, __LINE__ );
503
504         g_module_symbol(module, IMGP_FUNC_NAME, (gpointer*)&mm_util_imgp_func);
505         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] mm_util_imgp_func: %p", __func__, __LINE__, mm_util_imgp_func);
506
507         return mm_util_imgp_func;
508 }
509
510 static int
511 _mm_util_imgp_finalize(GModule *module, imgp_info_s *_imgp_info_s)
512 {
513         int ret = MM_ERROR_NONE;
514
515         if(module) {
516                 mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] module : %p", __func__, __LINE__, module);
517                 g_module_close( module );
518                 mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #End g_module_close#", __func__, __LINE__ );
519                 module = NULL;
520         }else {
521                 mmf_debug(MMF_DEBUG_ERROR,  "[%s][%05d] #module is NULL#", __func__, __LINE__ );
522                 return MM_ERROR_IMAGE_INVALID_VALUE;
523         }
524
525         if(_imgp_info_s) {
526                 if(_imgp_info_s->src) {
527                         free(_imgp_info_s->src);_imgp_info_s->src=NULL;
528                 }
529                 mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #Success _imgp_info_s->src#", __func__, __LINE__ );
530                 if(_imgp_info_s->dst) {
531                         free(_imgp_info_s->dst);_imgp_info_s->dst=NULL;
532                 }
533                 mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #Success _imgp_info_s->dst#", __func__, __LINE__ );
534                 free(_imgp_info_s); _imgp_info_s=NULL;
535                 mmf_debug(MMF_DEBUG_LOG,  "[%s][%05d] #Success _imgp_info_s#", __func__, __LINE__ );
536         }else {
537                 mmf_debug(MMF_DEBUG_ERROR,  "[%s][%05d] #_imgp_info_s is NULL#", __func__, __LINE__ );
538                 return MM_ERROR_IMAGE_INVALID_VALUE;
539         }
540         return ret;
541 }
542
543 static int
544 _mm_util_crop_rgba32(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format,
545 unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height, unsigned char *dst)
546 {
547         int ret = MM_ERROR_NONE;
548         int i;
549         int start_x = (src_width - crop_dest_width) / 2;
550         int start_y = (src_height - crop_dest_height) / 2;
551         int src_bytesperline = src_width * 4;
552         int dst_bytesperline = crop_dest_width * 4;
553
554         src += start_y * src_bytesperline + 3 * start_x;
555
556         for (i = 0; i < crop_dest_height; i++) {
557                 memcpy(dst, src, dst_bytesperline);
558                 src += src_bytesperline;
559                 dst += dst_bytesperline;
560         }
561
562         return ret;
563 }
564
565 static int
566 _mm_util_crop_rgb888(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format,
567 unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height, unsigned char *dst)
568 {
569         int ret = MM_ERROR_NONE;
570         int i;
571         int start_x = (src_width - crop_dest_width) / 2;
572         int start_y = (src_height - crop_dest_height) / 2;
573         int src_bytesperline = src_width * 3;
574         int dst_bytesperline = crop_dest_width * 3;
575
576         src += start_y * src_bytesperline + 3 * start_x;
577
578         for (i = 0; i < crop_dest_height; i++) {
579                 memcpy(dst, src, dst_bytesperline);
580                 src += src_bytesperline;
581                 dst += dst_bytesperline;
582         }
583
584         return ret;
585 }
586
587 static int
588 _mm_util_crop_rgb565(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format,
589 unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height, unsigned char *dst)
590 {
591         int ret = MM_ERROR_NONE;
592         int i;
593         int start_x = (src_width - crop_dest_width) / 2;
594         int start_y = (src_height - crop_dest_height) / 2;
595         int src_bytesperline = src_width * 2;
596         int dst_bytesperline = crop_dest_width * 2;
597
598         src += start_y * src_bytesperline + 3 * start_x;
599
600         for (i = 0; i < crop_dest_height; i++) {
601                 memcpy(dst, src, dst_bytesperline);
602                 src += src_bytesperline;
603                 dst += dst_bytesperline;
604         }
605
606         return ret;
607 }
608
609 static int
610 _mm_util_crop_yuv420(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format,
611 unsigned int crop_start_x, unsigned int crop_start_y, unsigned int crop_dest_width, unsigned int crop_dest_height, unsigned char *dst)
612 {
613         int ret = MM_ERROR_NONE;
614         int i;
615         int start_x = ((src_width - crop_dest_width) / 2) & ~1;
616         int start_y = ((src_height - crop_dest_height) / 2) & ~1;
617         unsigned char *_src = src + start_y * src_width + start_x;
618
619         /* Y */
620         for (i = 0; i < crop_dest_height; i++) {
621                 memcpy(dst, _src, crop_dest_width);
622                 _src += src_width;
623                 dst += crop_dest_width;
624         }
625
626         /* U */
627         _src = src + src_height * src_width + (start_y / 2) * src_width / 2 + start_x / 2;
628         for (i = 0; i < crop_dest_height / 2; i++) {
629                 memcpy(dst, _src, crop_dest_width / 2);
630                 _src += src_width / 2;
631                 dst += crop_dest_width / 2;
632         }
633
634         /* V */
635         _src = src + src_height * src_width * 5 / 4 + (start_y / 2) * src_width / 2 + start_x / 2;
636         for (i = 0; i < crop_dest_height / 2; i++) {
637                 memcpy(dst, _src, crop_dest_width / 2);
638                 _src += src_width / 2;
639                 dst += crop_dest_width / 2;
640         }
641
642         return ret;
643 }
644
645 EXPORT_API int
646 mm_util_convert_colorspace(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format, unsigned char *dst, mm_util_img_format dst_format)
647 {
648         int ret = MM_ERROR_NONE;
649
650         if (!src || !dst) {
651                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] invalid mm_util_convert_colorspace\n", __func__, __LINE__);
652                 return MM_ERROR_IMAGE_INVALID_VALUE;
653         }
654
655         if( (src_format < MM_UTIL_IMG_FMT_YUV420) || (src_format > MM_UTIL_IMG_FMT_NUM) || (dst_format < MM_UTIL_IMG_FMT_YUV420) || (dst_format > MM_UTIL_IMG_FMT_NUM) ) {
656                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_format: %d || dst_format:%d  value ", __func__, __LINE__, src_format, dst_format);
657                 return MM_ERROR_IMAGE_INVALID_VALUE;
658         }
659
660         if( (src_width < 0) || (src_height < 0)) {
661                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_width || src_height value ", __func__, __LINE__);
662                 return MM_ERROR_IMAGE_INVALID_VALUE;
663         }
664
665         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] #START#", __func__, __LINE__);
666
667         if (_mm_cannot_convert_format(src_format, dst_format)) {
668                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# Cannot Support Image Format Convert", __func__, __LINE__);
669                 return MM_ERROR_IMAGE_INVALID_VALUE;
670         }
671
672         imgp_info_s* _imgp_info_s=(imgp_info_s*)malloc(sizeof(imgp_info_s));
673         unsigned int dst_size=0;
674         IMGPInfoFunc _mm_util_imgp_func  = NULL;
675         GModule *_module  = NULL;
676         imgp_plugin_type_e _imgp_plugin_type_e=-1;
677
678         /* Initialize */
679         if( _mm_select_convert_plugin(src_format, dst_format)) {
680                 _imgp_plugin_type_e = IMGP_NEON;
681         }else {
682                 _imgp_plugin_type_e = IMGP_GSTCS;
683         }
684         _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
685
686         if(_module == NULL) { //when IMGP_NEON is NULL
687                 _imgp_plugin_type_e = IMGP_GSTCS;
688                 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] You use %s module", __func__, __LINE__, PATH_GSTCS_LIB);
689                 _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
690         }
691         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_util_imgp_func: %p", __func__, __LINE__, _module);
692         ret=_mm_set_imgp_info_s(_imgp_info_s, src, src_format, src_width, src_height, dst_format, src_width, src_height, MM_UTIL_ROTATE_0);
693         if(ret != MM_ERROR_NONE)        {
694                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_set_imgp_info_s failed", __func__, __LINE__);
695                 return MM_ERROR_IMAGE_INVALID_VALUE;
696         }
697         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_set_imgp_info_s", __func__, __LINE__);
698
699         /* image processing */
700         _mm_util_imgp_func = _mm_util_imgp_process(_module);
701         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_util_imgp_process", __func__, __LINE__);
702
703         if(_mm_util_imgp_func) {
704                 ret=_mm_util_imgp_func(_imgp_info_s, IMGP_CSC);
705                 if (ret != MM_ERROR_NONE)
706                 {
707                         mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] image processing failed", __func__, __LINE__);
708                         return MM_ERROR_IMAGE_INVALID_VALUE;
709                 }
710         }else {
711                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] g_module_symbol failed", __func__, __LINE__);
712                 return MM_ERROR_IMAGE_INVALID_VALUE;
713         }
714
715         /* Output result*/
716         mm_util_get_image_size(_imgp_info_s->dst_format, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &dst_size);
717         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] dst_width: %d, dst_height: %d, output_stride: %d, output_elevation: %d",
718                         __func__, __LINE__, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
719
720         memcpy(dst, _imgp_info_s->dst, dst_size);
721
722         /* Finalize */
723         ret = _mm_util_imgp_finalize(_module, _imgp_info_s);
724         if(ret != MM_ERROR_NONE) {
725                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_util_imgp_finalize failed", __func__, __LINE__);
726                 return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
727         }
728         return ret;
729 }
730
731 EXPORT_API int
732 mm_util_resize_image(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format, unsigned char *dst, unsigned int *dst_width, unsigned int *dst_height)
733 {
734         int ret = MM_ERROR_NONE;
735         if (!src || !dst) {
736                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] invalid argument\n", __func__, __LINE__);
737                 return MM_ERROR_IMAGE_INVALID_VALUE;
738         }
739
740         if( (src_format < MM_UTIL_IMG_FMT_YUV420) || (src_format > MM_UTIL_IMG_FMT_NUM) ) {
741                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_format value ", __func__, __LINE__);
742                 return MM_ERROR_IMAGE_INVALID_VALUE;
743         }
744
745         if( !dst_width || !dst_height ) {
746                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# dst width/height buffer is NULL", __func__, __LINE__);
747                 return MM_ERROR_IMAGE_INVALID_VALUE;
748         }
749
750         if( (src_width < 0) || (src_height < 0)) {
751                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_width || src_height value ", __func__, __LINE__);
752                 return MM_ERROR_IMAGE_INVALID_VALUE;
753         }
754
755         imgp_info_s* _imgp_info_s=(imgp_info_s*)malloc(sizeof(imgp_info_s));
756         unsigned int dst_size=0;
757         IMGPInfoFunc _mm_util_imgp_func  = NULL;
758         GModule *_module  = NULL;
759         imgp_plugin_type_e _imgp_plugin_type_e=-1;
760
761         /* Initialize */
762         if( _mm_select_resize_plugin(src_format)) {
763                 _imgp_plugin_type_e = IMGP_NEON;
764         }else {
765                 _imgp_plugin_type_e = IMGP_GSTCS;
766         }
767         _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
768         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_util_imgp_init: %p", __func__, __LINE__, _module);
769         if(_module == NULL)  //when IMGP_NEON is NULL
770         {
771                 _imgp_plugin_type_e = IMGP_GSTCS;
772                 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] You use %s module", __func__, __LINE__, PATH_GSTCS_LIB);
773                 _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
774         }
775         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_set_imgp_info_s", __func__, __LINE__);
776         ret=_mm_set_imgp_info_s(_imgp_info_s, src, src_format, src_width, src_height, src_format, *dst_width, *dst_height, MM_UTIL_ROTATE_0);
777         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_set_imgp_info_s ret: %d", __func__, __LINE__, ret);
778         if(ret != MM_ERROR_NONE) {
779                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_set_imgp_info_s failed", __func__, __LINE__);
780                 return MM_ERROR_IMAGE_INVALID_VALUE;
781         }
782         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_set_imgp_info_s", __func__, __LINE__);
783
784         if(g_strrstr(g_module_name (_module), GST)) {
785                 if(_mm_gst_can_resize_format(_imgp_info_s->input_format_label) == FALSE) {
786                         mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] #RESIZE ERROR# IMAGE_NOT_SUPPORT_FORMAT", __func__, __LINE__);
787                         _mm_util_imgp_finalize(_module, _imgp_info_s);
788                         return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
789                 }
790         }
791
792         /* image processing */
793         _mm_util_imgp_func = _mm_util_imgp_process(_module);
794         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_util_imgp_process", __func__, __LINE__);
795         if(_mm_util_imgp_func) {
796                 ret=_mm_util_imgp_func(_imgp_info_s, IMGP_RSZ);
797                 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_util_imgp_func, ret: %d", __func__, __LINE__, ret);
798                 if (ret != MM_ERROR_NONE)
799                 {
800                         mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] image processing failed", __func__, __LINE__);
801                         return MM_ERROR_IMAGE_INVALID_VALUE;
802                 }
803         }else {
804                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] g_module_symbol failed", __func__, __LINE__);
805                 return MM_ERROR_IMAGE_INVALID_VALUE;
806         }
807
808         /* Output result*/
809         mm_util_get_image_size(_imgp_info_s->dst_format, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &dst_size);
810         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] dst_width: %d, dst_height: %d, output_stride: %d, output_elevation: %d",
811                         __func__, __LINE__, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
812
813         memcpy(dst, _imgp_info_s->dst, dst_size);
814         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] #Success# memcpy");
815
816         *dst_width = _imgp_info_s->dst_width;
817         *dst_height = _imgp_info_s->dst_height;
818
819         /* Finalize */
820         ret = _mm_util_imgp_finalize(_module, _imgp_info_s);
821         if(ret != MM_ERROR_NONE)        {
822                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_util_imgp_finalize failed", __func__, __LINE__);
823                 return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
824         }
825         return ret;
826 }
827
828 EXPORT_API int
829 mm_util_rotate_image(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format, unsigned char *dst, unsigned int *dst_width, unsigned int *dst_height, mm_util_img_rotate_type angle)
830 {
831         int ret = MM_ERROR_NONE;
832
833         if (!src || !dst) {
834                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] invalid argument\n", __func__, __LINE__);
835                 return MM_ERROR_IMAGE_INVALID_VALUE;
836         }
837
838         if( (src_format < MM_UTIL_IMG_FMT_YUV420) || (src_format > MM_UTIL_IMG_FMT_NUM) ) {
839                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_format value", __func__, __LINE__);
840                 return MM_ERROR_IMAGE_INVALID_VALUE;
841         }
842
843         if( !dst_width || !dst_height ) {
844                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# dst width/height buffer is NUL", __func__, __LINE__);
845                 return MM_ERROR_IMAGE_INVALID_VALUE;
846         }
847
848         if( (src_width < 0) || (src_height < 0)) {
849                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_width || src_height value ", __func__, __LINE__);
850                 return MM_ERROR_IMAGE_INVALID_VALUE;
851         }
852
853         if( (angle < MM_UTIL_ROTATE_0) || (angle > MM_UTIL_ROTATE_NUM) ) {
854                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# angle vaule", __func__, __LINE__);
855                 return MM_ERROR_IMAGE_INVALID_VALUE;
856         }
857
858         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d]  #START#", __func__, __LINE__);
859         imgp_info_s* _imgp_info_s=(imgp_info_s*)malloc(sizeof(imgp_info_s));
860         unsigned int dst_size=0;
861         IMGPInfoFunc  _mm_util_imgp_func = NULL;
862         GModule *_module = NULL;
863         imgp_plugin_type_e _imgp_plugin_type_e=-1;
864
865         /* Initialize */
866         if( _mm_select_rotate_plugin(src_format, src_width, src_height, angle)) {
867                 _imgp_plugin_type_e = IMGP_NEON;
868         }else {
869                 _imgp_plugin_type_e = IMGP_GSTCS;
870         }
871         _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
872         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_util_imgp_func: %p", __func__, __LINE__, _module);
873         if(_module == NULL) { //when IMGP_NEON is NULL
874                 _imgp_plugin_type_e = IMGP_GSTCS;
875                 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] You use %s module", __func__, __LINE__, PATH_GSTCS_LIB);
876                 _module = _mm_util_imgp_initialize(_imgp_plugin_type_e);
877         }
878         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_set_imgp_info_s", __func__, __LINE__);
879         ret=_mm_confirm_dst_width_height(src_width, src_height, dst_width, dst_height, angle);
880         if(ret != MM_ERROR_NONE) {
881                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] dst_width || dest_height size Error", __func__, __LINE__);
882                 return MM_ERROR_IMAGE_INVALID_VALUE;
883         }
884
885         ret=_mm_set_imgp_info_s(_imgp_info_s, src, src_format, src_width, src_height, src_format, *dst_width, *dst_height, angle);
886         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] _mm_set_imgp_info_s", __func__, __LINE__);
887         if(ret != MM_ERROR_NONE) {
888                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_set_imgp_info_s failed", __func__, __LINE__);
889                 return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
890         }
891         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_set_imgp_info_s", __func__, __LINE__);
892
893         if(g_strrstr(g_module_name (_module), GST)) {
894                 if(_mm_gst_can_rotate_format(_imgp_info_s->input_format_label) == FALSE) {
895                         mmf_debug(MMF_DEBUG_ERROR,"[%s][%05d] #gstreamer ROTATE ERROR# IMAGE_NOT_SUPPORT_FORMAT", __func__, __LINE__);
896                         _mm_util_imgp_finalize(_module, _imgp_info_s);
897                         return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
898                 }
899         }
900
901         /* image processing */
902         _mm_util_imgp_func = _mm_util_imgp_process(_module);
903         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Sucess _mm_util_imgp_process", __func__, __LINE__);
904         if(_mm_util_imgp_func) {
905                 ret=_mm_util_imgp_func(_imgp_info_s, IMGP_ROT);
906                 if (ret!= MM_ERROR_NONE)        {
907                         mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] image processing failed", __func__, __LINE__);
908                         return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
909                 }
910         }else {
911                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] g_module_symbol failed", __func__, __LINE__);
912                 return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
913         }
914
915         /* Output result*/
916         mm_util_get_image_size(_imgp_info_s->dst_format, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &dst_size);
917         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] dst_width: %d, dst_height: %d, output_stride: %d, output_elevation: %d, dst_size: %d",
918                         __func__, __LINE__, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation,dst_size);
919
920         memcpy(dst, _imgp_info_s->dst, dst_size);
921         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] #Success# memcpy");
922         *dst_width = _imgp_info_s->dst_width;
923         *dst_height = _imgp_info_s->dst_height;
924
925         /* Finalize */
926         ret = _mm_util_imgp_finalize(_module, _imgp_info_s);
927         if(ret != MM_ERROR_NONE) {
928                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] _mm_util_imgp_finalize failed", __func__, __LINE__);
929                 return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
930         }
931         return ret;
932 }
933
934 EXPORT_API int
935 mm_util_crop_image(unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_img_format src_format,
936 unsigned int crop_start_x, unsigned int crop_start_y, unsigned int *crop_dest_width, unsigned int *crop_dest_height, unsigned char *dst)
937 {
938         int ret = MM_ERROR_NONE;
939
940         if (!src || !dst) {
941                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] invalid argument\n", __func__, __LINE__);
942                 return MM_ERROR_IMAGE_INVALID_VALUE;
943         }
944
945         if( (src_format < MM_UTIL_IMG_FMT_YUV420) || (src_format > MM_UTIL_IMG_FMT_NUM) ) {
946                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# src_format value", __func__, __LINE__);
947                 return MM_ERROR_IMAGE_INVALID_VALUE;
948         }
949
950         if( (*crop_dest_width < 0) || (*crop_dest_height < 0) || (crop_start_x +*crop_dest_width > src_width) || (crop_start_y +*crop_dest_height > src_height) ) {
951                 mmf_debug(MMF_DEBUG_ERROR, "[%s][%05d] #ERROR# dest width | height value", __func__, __LINE__);
952                 return MM_ERROR_IMAGE_INVALID_VALUE;
953         }
954
955         switch (src_format) {
956                 case MM_UTIL_IMG_FMT_RGB888: {
957                         ret = _mm_util_crop_rgb888(src, src_width, src_height, src_format, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
958                         break;
959                         }
960                 case MM_UTIL_IMG_FMT_RGB565: {
961                         ret = _mm_util_crop_rgb565(src, src_width, src_height, src_format, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
962                         break;
963                         }
964                 case MM_UTIL_IMG_FMT_ARGB8888:
965                 case MM_UTIL_IMG_FMT_BGRA8888:
966                 case MM_UTIL_IMG_FMT_RGBA8888:
967                 case MM_UTIL_IMG_FMT_BGRX8888: {
968                         ret = _mm_util_crop_rgba32(src, src_width, src_height, src_format, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
969                         break;
970                         }
971                 case MM_UTIL_IMG_FMT_I420:
972                 case MM_UTIL_IMG_FMT_YUV420: {
973                         if( (*crop_dest_width %2) !=0 ) {
974                                 mmf_debug(MMF_DEBUG_WARNING, "[%s][%05d] #YUV Width value(%d) must be even at least# ", __func__, __LINE__, *crop_dest_width);
975                                 *crop_dest_width = ((*crop_dest_width+1)>>1)<<1;
976                                 mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Image isplay is suceeded when YUV crop width value %d ", __func__, __LINE__,*crop_dest_width);
977                         }
978
979                         ret = _mm_util_crop_yuv420(src, src_width, src_height, src_format, crop_start_x, crop_start_y, *crop_dest_width, *crop_dest_height, dst);
980                         break;
981                         }
982                 default:
983                         mmf_debug(MMF_DEBUG_LOG, "[%s][%05d] Not supported format", __func__, __LINE__);
984         }
985
986         return ret;
987 }
988
989 EXPORT_API int
990 mm_util_get_image_size(mm_util_img_format format, unsigned int width, unsigned int height, unsigned int *imgsize)
991 {
992         int ret = MM_ERROR_NONE;
993         unsigned char x_chroma_shift = 0;
994         unsigned char y_chroma_shift = 0;
995         int size, w2, h2, size2;
996         int stride, stride2;
997
998         if (!imgsize) {
999                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] imgsize can't be null\n", __func__, __LINE__);
1000                 return MM_ERROR_IMAGE_FILEOPEN;
1001         }
1002
1003         *imgsize = 0;
1004
1005
1006         if (check_valid_picture_size(width, height) < 0) {
1007                 mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] invalid width and height\n", __func__, __LINE__);
1008                 return MM_ERROR_IMAGE_INVALID_VALUE;
1009         }
1010
1011         switch (format)
1012         {
1013
1014                 case MM_UTIL_IMG_FMT_I420:
1015                 case MM_UTIL_IMG_FMT_YUV420:
1016                         x_chroma_shift = 1;
1017                         y_chroma_shift = 1;
1018                         stride = MM_UTIL_ROUND_UP_4 (width);
1019                         h2 = ROUND_UP_X (height, x_chroma_shift);
1020                         size = stride * h2;
1021                         w2 = DIV_ROUND_UP_X (width, x_chroma_shift);
1022                         stride2 = MM_UTIL_ROUND_UP_4 (w2);
1023                         h2 = DIV_ROUND_UP_X (height, y_chroma_shift);
1024                         size2 = stride2 * h2;
1025                         *imgsize = size + 2 * size2;
1026                         break;
1027                 case MM_UTIL_IMG_FMT_YUV422:
1028                 case MM_UTIL_IMG_FMT_YUYV:
1029                 case MM_UTIL_IMG_FMT_UYVY:
1030                         stride = MM_UTIL_ROUND_UP_4 (width * 2);
1031                         size = stride * height;
1032                         *imgsize = size;
1033                         break;
1034
1035                 case MM_UTIL_IMG_FMT_RGB565:
1036                         stride = MM_UTIL_ROUND_UP_4 (width * 2);
1037                         size = stride * height;
1038                         *imgsize = size;
1039                         break;
1040
1041                 case MM_UTIL_IMG_FMT_RGB888:
1042                         stride = MM_UTIL_ROUND_UP_4 (width * 3);
1043                         size = stride * height;
1044                         *imgsize = size;
1045                         break;
1046
1047                 case MM_UTIL_IMG_FMT_ARGB8888:
1048                 case MM_UTIL_IMG_FMT_BGRA8888:
1049                 case MM_UTIL_IMG_FMT_RGBA8888:
1050                 case MM_UTIL_IMG_FMT_BGRX8888:
1051                         stride = width * 4;
1052                         size = stride * height;
1053                         *imgsize = size;
1054                         break;
1055
1056
1057                 case MM_UTIL_IMG_FMT_NV12:
1058                 case MM_UTIL_IMG_FMT_NV12_TILED:
1059                         x_chroma_shift = 1;
1060                         y_chroma_shift = 1;
1061                         stride = MM_UTIL_ROUND_UP_4 (width);
1062                         h2 = ROUND_UP_X (height, y_chroma_shift);
1063                         size = stride * h2;
1064                         w2 = 2 * DIV_ROUND_UP_X (width, x_chroma_shift);
1065                         stride2 = MM_UTIL_ROUND_UP_4 (w2);
1066                         h2 = DIV_ROUND_UP_X (height, y_chroma_shift);
1067                         size2 = stride2 * h2;
1068                         *imgsize = size + size2;
1069                         break;
1070
1071                 default:
1072                         mmf_debug (MMF_DEBUG_ERROR, "[%s][%05d] Not supported format\n", __func__, __LINE__);
1073                         return MM_ERROR_IMAGE_NOT_SUPPORT_FORMAT;
1074         }
1075         mmf_debug (MMF_DEBUG_LOG, "[%s][%05d] format: %d, *imgsize: %d\n", __func__, __LINE__, format, *imgsize);
1076
1077         return ret;
1078 }