Modify mm_util_convert_colorspace() API to receive allocated buffer and buffer info 68/170668/1
authorhj kim <backto.kim@samsung.com>
Wed, 21 Feb 2018 08:59:01 +0000 (17:59 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 21 Feb 2018 08:59:01 +0000 (17:59 +0900)
Change-Id: I606b2789a900cbb78cfc01d38aef2e54d9667e25

imgp/include/mm_util_imgp.h
imgp/mm_util_imgp.c
imgp/test/mm_util_imgp_testsuite.c
jpeg/mm_util_jpeg.c

index 51f6b42..8d5ea75 100755 (executable)
@@ -189,7 +189,8 @@ int mm_util_destroy(mm_util_imgp_h imgp_handle);
  * @see         mm_util_color_format_e
  * @since       R1, 1.0
  */
-int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format, unsigned char *dst, mm_util_color_format_e dst_format);
+int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format, mm_util_color_format_e dst_format, unsigned char **dst, unsigned int *result_buf_width, unsigned int *result_buf_height, size_t *result_buf_size);
+
 
 /**
  * This function resizes the source image.
index f1daaf6..62daf0f 100755 (executable)
@@ -530,6 +530,8 @@ static int __mm_util_processing(mm_util_s *handle)
        unsigned int res_w = 0;
        unsigned int res_h = 0;
        unsigned char *res_buffer = NULL;
+       unsigned char *res_buffer_conv = NULL;
+       unsigned char *res_buffer_rotate = NULL;
        size_t res_buffer_size = 0;
 
        mm_util_retvm_if(handle == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid handle");
@@ -555,8 +557,8 @@ static int __mm_util_processing(mm_util_s *handle)
                        mm_util_error("mm_util_crop_image failed");
                        return ret;
                }
-               dst_buf[dst_index] = res_buffer;
 
+               dst_buf[dst_index] = res_buffer;
                src_index = dst_index;
                src_width = res_w;
                src_height = res_h;
@@ -579,33 +581,31 @@ static int __mm_util_processing(mm_util_s *handle)
        if (handle->set_convert) {
                dst_index++;
 
-               mm_util_get_image_size(handle->dst_format, src_width, src_height, &res_buffer_size);
-               dst_buf[dst_index] = calloc(1, res_buffer_size);
-               if (dst_buf[dst_index] == NULL) {
-                       mm_util_error("[multi func] memory allocation error");
-                       __mm_destroy_temp_buffer(dst_buf);
-                       return MM_UTIL_ERROR_INVALID_OPERATION;
-               }
-               ret = mm_util_convert_colorspace(dst_buf[src_index], src_width, src_height, src_format, dst_buf[dst_index], handle->dst_format);
+               ret = mm_util_convert_colorspace(dst_buf[src_index], src_width, src_height, src_format, handle->dst_format, &res_buffer_conv, &res_w, &res_h, &res_buffer_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        __mm_destroy_temp_buffer(dst_buf);
                        mm_util_error("mm_util_convert_colorspace failed");
                        return ret;
                }
+
+               dst_buf[dst_index] = res_buffer_conv;
                src_index = dst_index;
                src_format = handle->dst_format;
+               src_width = res_w;
+               src_height = res_h;
        }
 
        if (handle->set_rotate) {
                dst_index++;
-               ret = mm_util_rotate_image(dst_buf[src_index], src_width, src_height, src_format, handle->rotation, &res_buffer, &res_w, &res_h, &res_buffer_size);
+
+               ret = mm_util_rotate_image(dst_buf[src_index], src_width, src_height, src_format, handle->rotation, &res_buffer_rotate, &res_w, &res_h, &res_buffer_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        __mm_destroy_temp_buffer(dst_buf);
                        mm_util_error("mm_util_rotate_image failed");
                        return ret;
                }
 
-               dst_buf[dst_index] = res_buffer;
+               dst_buf[dst_index] = res_buffer_rotate;
                src_index = dst_index;
                src_width = res_w;
                src_height = res_h;
@@ -887,7 +887,7 @@ static bool __mm_util_check_format(mm_util_color_format_e color_format)
        return FALSE;
 }
 
-int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format, unsigned char *dst, mm_util_color_format_e dst_format)
+int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width, unsigned int src_height, mm_util_color_format_e src_format, mm_util_color_format_e dst_format, unsigned char **dst, unsigned int *result_buf_width, unsigned int *result_buf_height, size_t *result_buf_size)
 {
        int ret = MM_UTIL_ERROR_NONE;
        IMGPInfoFunc _mm_util_imgp_func = NULL;
@@ -904,6 +904,10 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
        mm_util_retvm_if((IS_MM_UTIL_COLOR_FORMAT(dst_format) == FALSE), MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_format [%d]", dst_format);
        mm_util_retvm_if((__mm_util_check_format(src_format) == FALSE), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported src_format [%d]", src_format);
        mm_util_retvm_if((__mm_util_check_format(dst_format) == FALSE), MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT, "not supported dst_format [%d]", dst_format);
+       mm_util_retvm_if(dst == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst");
+       mm_util_retvm_if(result_buf_width == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid result_buf_width");
+       mm_util_retvm_if(result_buf_height == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid result_buf_height");
+       mm_util_retvm_if(result_buf_size == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid result_buf_size");
 
        mm_util_debug("src_width [%d] src_height [%d] src_format[%d] dst_format[%d]", src_width, src_height, src_format, dst_format);
 
@@ -920,6 +924,9 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
                goto ERROR;
        }
 
+       res_w = src_width;
+       res_h = src_height;
+
        ret = __mm_set_imgp_info_s(_imgp_info_s, src_format, src_width, src_height, dst_format, src_width, src_height, MM_UTIL_ROTATE_0);
        if (ret != MM_UTIL_ERROR_NONE) {
                mm_util_error("__mm_set_imgp_info_s failed");
@@ -937,25 +944,28 @@ int mm_util_convert_colorspace(const unsigned char *src, unsigned int src_width,
                ret = mm_util_crop_image(output_buffer, _imgp_info_s->output_stride, _imgp_info_s->output_elevation, dst_format, 0, 0, _imgp_info_s->dst_width, _imgp_info_s->dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
                if (ret != MM_UTIL_ERROR_NONE) {
                        mm_util_error("mm_util_crop_image failed");
+                       MMUTIL_SAFE_FREE(output_buffer);
                        ret = MM_UTIL_ERROR_INVALID_OPERATION;
                        goto ERROR;
                }
-               memcpy(dst, res_buffer, res_buffer_size);
+
+               MMUTIL_SAFE_FREE(output_buffer);
+               *dst = res_buffer;
+               *result_buf_size = res_buffer_size;
        } else {
-               memcpy(dst, output_buffer, _imgp_info_s->buffer_size);
+               *dst = output_buffer;
+               *result_buf_size = _imgp_info_s->buffer_size;
        }
 
-       /* Output result*/
-       mm_util_debug("dst[%p] dst_width[%u] dst_height[%u] output_stride[%u] output_elevation[%u]",
-                       dst, _imgp_info_s->dst_width, _imgp_info_s->dst_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
+       *result_buf_width = res_w;
+       *result_buf_height = res_h;
+
+       mm_util_debug("dst[%p] result_buf_w[%u] result_buf_h[%u] output_stride[%u] output_elevation[%u]", dst, *result_buf_width, *result_buf_height, _imgp_info_s->output_stride, _imgp_info_s->output_elevation);
 
 ERROR:
        /* Finalize */
        __mm_util_imgp_finalize(_module, _imgp_info_s);
 
-       MMUTIL_SAFE_FREE(output_buffer);
-       MMUTIL_SAFE_FREE(res_buffer);
-
        mm_util_fleave();
 
        return ret;
@@ -980,6 +990,7 @@ int mm_util_resize_image(const unsigned char *src, unsigned int src_width, unsig
        mm_util_retvm_if(dst == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst");
        mm_util_retvm_if(dst_width == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_width");
        mm_util_retvm_if(dst_height == 0, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid dst_height");
+       mm_util_retvm_if(result_buf_size == NULL, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid result_buf_size");
 
        mm_util_debug("src_width [%d] src_height [%d] src_format[%d]", src_width, src_height, src_format);
 
index c82c96f..e96a172 100755 (executable)
@@ -99,7 +99,7 @@ bool _transform_completed_cb(mm_util_color_image_h image, int error, void *user_
 int main(int argc, char *argv[])
 {
        int ret = 0;
-       void *src;
+       void *src = NULL;
        unsigned char *dst = NULL;
 
        if (argc < 12) {
@@ -110,7 +110,6 @@ int main(int argc, char *argv[])
        }
 
        size_t src_size = 0;
-       size_t dst_size = 0;
        bool sync_mode = (strcmp(argv[1], "sync") == 0) ? TRUE : FALSE;
        char *filename = strdup(argv[2]);
        char *command = strdup(argv[3]);
@@ -126,7 +125,6 @@ int main(int argc, char *argv[])
        char output_file[40] = {};
        unsigned int res_w = 0;
        unsigned int res_h = 0;
-       unsigned char *res_buffer = NULL;
        size_t res_buffer_size = 0;
 
        /* async mode */
@@ -134,19 +132,6 @@ int main(int argc, char *argv[])
 
        fprintf(stderr, "command: %s src_width: %d, src_height: %d, src_format: %d, dst_width: %d, dst_height: %d, dst_format:%d, rotation:%d\n", command, src_width, src_height, src_format, dst_width, dst_height, dst_format, rotation);
 
-       if (strcmp(command, "convert") == 0) {
-               if ((src_width != dst_width) || (src_width != dst_width)) {
-                       fprintf(stderr, "Wrong input. dst w/h should be same as src w/h\n");
-                       return 0;
-               }
-       }
-
-       /* mem allocation for src dst buffer */
-       mm_util_get_image_size(src_format, src_width, src_height, &src_size);
-       mm_util_get_image_size(dst_format, dst_width, dst_height, &dst_size);
-       src = calloc(1, src_size);
-       dst = calloc(1, dst_size);
-
        { /* read input file */
                FILE *fp = fopen(filename, "r");
                if (fp == NULL) {
@@ -154,6 +139,11 @@ int main(int argc, char *argv[])
                        goto TEST_FAIL;
                }
 
+               fseek(fp, 0, SEEK_END);
+               src_size = ftell(fp);
+               fseek(fp, 0, SEEK_SET);
+
+               src = calloc(1, src_size);
                if (src == NULL) {
                        fprintf(stderr, "\tmemory allocation failed\n");
                        goto TEST_FAIL;
@@ -187,21 +177,14 @@ int main(int argc, char *argv[])
 
        if (sync_mode) {
                fprintf(stderr, "SYNC\n");
-               if (strcmp(command, "convert") == 0) {
-                       ret = mm_util_convert_colorspace(src, src_width, src_height, src_format, dst, dst_format);
-               } else if (strcmp(command, "resize") == 0) {
-                       ret = mm_util_resize_image(src, src_width, src_height, src_format, dst_width, dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
-                       IMGP_FREE(dst);
-                       dst = res_buffer;
-               } else if (strcmp(command, "rotate") == 0) {
-                       ret = mm_util_rotate_image(src, src_width, src_height, src_format, rotation, &res_buffer, &res_w, &res_h, &res_buffer_size);
-                       IMGP_FREE(dst);
-                       dst = res_buffer;
-               } else if (strcmp(command, "crop") == 0) {
-                       ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, dst_width, dst_height, &res_buffer, &res_w, &res_h, &res_buffer_size);
-                       IMGP_FREE(dst);
-                       dst = res_buffer;
-               }
+               if (strcmp(command, "convert") == 0)
+                       ret = mm_util_convert_colorspace(src, src_width, src_height, src_format, dst_format, &dst, &res_w, &res_h, &res_buffer_size);
+               else if (strcmp(command, "resize") == 0)
+                       ret = mm_util_resize_image(src, src_width, src_height, src_format, dst_width, dst_height, &dst, &res_w, &res_h, &res_buffer_size);
+               else if (strcmp(command, "rotate") == 0)
+                       ret = mm_util_rotate_image(src, src_width, src_height, src_format, rotation, &dst, &res_w, &res_h, &res_buffer_size);
+               else if (strcmp(command, "crop") == 0)
+                       ret = mm_util_crop_image(src, src_width, src_height, src_format, start_x, start_y, dst_width, dst_height, &dst, &res_w, &res_h, &res_buffer_size);
 
                if (ret == MM_UTIL_ERROR_NONE) {
                        fprintf(stderr, "Success - %s\n", command);
@@ -213,8 +196,8 @@ int main(int argc, char *argv[])
                {  /* write output file */
                        FILE *fpout = fopen(output_file, "w");
                        if (fpout) {
-                               fprintf(stderr, "dst: %p [%zu]\n", dst, dst_size);
-                               fwrite(dst, 1, dst_size, fpout);
+                               fprintf(stderr, "dst: %p [%zu]\n", dst, res_buffer_size);
+                               fwrite(dst, 1, res_buffer_size, fpout);
                                fprintf(stderr, "FREE\n");
                                fclose(fpout);
                        }
index e5b9fda..4ab51ad 100755 (executable)
@@ -1206,17 +1206,16 @@ int mm_util_jpeg_encode_to_file(const char *filename, void* src, int width, int
 #else
        mm_util_debug("#START# LIBJPEG");
        if (fmt == MM_UTIL_COLOR_NV12) {
-               size_t dst_size = 0;
-               ret = mm_util_get_image_size(MM_UTIL_COLOR_NV12, (unsigned int)width, (unsigned int)height, &dst_size);
+               unsigned int res_w = 0;
+               unsigned int res_h = 0;
+               size_t res_buffer_size = 0;
                unsigned char *dst = NULL;
-               dst = calloc(1, dst_size);
-               if (dst) {
-                       ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, dst, MM_UTIL_COLOR_YUV420);
-                       ret = __mm_image_encode_to_jpeg_file_with_libjpeg(filename, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
-                       MMUTIL_SAFE_FREE(dst);
-               } else {
-                       return MM_UTIL_ERROR_OUT_OF_MEMORY;
-               }
+
+               ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
+               ret = __mm_image_encode_to_jpeg_file_with_libjpeg(filename, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
+
+               MMUTIL_SAFE_FREE(dst);
+
        } else if (fmt == MM_UTIL_COLOR_NV21) {
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        } else {
@@ -1248,17 +1247,16 @@ int mm_util_jpeg_encode_to_memory(void **mem, unsigned int *size, void* src, int
 #else /* LIBJPEG_TURBO */
        mm_util_debug("#START# libjpeg");
        if (fmt == MM_UTIL_COLOR_NV12) {
-               size_t dst_size = 0;
-               ret = mm_util_get_image_size(MM_UTIL_COLOR_NV12, (unsigned int)width, (unsigned int)height, &dst_size);
+               unsigned int res_w = 0;
+               unsigned int res_h = 0;
+               size_t res_buffer_size = 0;
                unsigned char *dst = NULL;
-               dst = calloc(1, dst_size);
-               if (dst) {
-                       ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, dst, MM_UTIL_COLOR_YUV420);
-                       ret = __mm_image_encode_to_jpeg_memory_with_libjpeg(mem, size, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
-                       MMUTIL_SAFE_FREE(dst);
-               } else {
-                       return MM_UTIL_ERROR_OUT_OF_MEMORY;
-               }
+
+               ret = mm_util_convert_colorspace(src, width, height, MM_UTIL_COLOR_NV12, MM_UTIL_COLOR_YUV420, &dst, &res_w, &res_h, &res_buffer_size);
+               ret = __mm_image_encode_to_jpeg_memory_with_libjpeg(mem, size, dst, width, height, MM_UTIL_COLOR_YUV420, quality);
+
+               MMUTIL_SAFE_FREE(dst);
+
        } else if (fmt == MM_UTIL_COLOR_NV21) {
                return MM_UTIL_ERROR_NOT_SUPPORTED_FORMAT;
        } else {
@@ -1309,32 +1307,28 @@ int mm_util_decode_from_jpeg_file(mm_util_jpeg_yuv_data *decoded, const char *fi
 #else
                mm_util_debug("#START# libjpeg(fmt:%d)", fmt);
                if (fmt == MM_UTIL_COLOR_NV12) {
-                       size_t dst_size = 0;
                        ret = __mm_image_decode_from_jpeg_file_with_libjpeg(decoded, filename, MM_UTIL_COLOR_YUV420, MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1);
                        if (ret == MM_UTIL_ERROR_NONE) {
-                               int err = MM_UTIL_ERROR_NONE;
-                               err = mm_util_get_image_size(MM_UTIL_COLOR_NV12, decoded->width, decoded->height, &dst_size);
-                               if (err != MM_UTIL_ERROR_NONE)
-                                       mm_util_error("fail mm_util_get_image_size");
-
+                               unsigned int res_w = 0;
+                               unsigned int res_h = 0;
+                               size_t res_buffer_size = 0;
                                unsigned char *dst = NULL;
-                               dst = calloc(1, dst_size);
-                               if (dst) {
-                                       ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, dst, MM_UTIL_COLOR_NV12);
-                                       MMUTIL_SAFE_FREE(decoded->data);
-                                       decoded->data = calloc(1, dst_size);
-                                       if (decoded->data == NULL) {
-                                               mm_util_debug("memory allocation failed");
-                                               MMUTIL_SAFE_FREE(dst);
-                                               return MM_UTIL_ERROR_OUT_OF_MEMORY;
-                                       }
-                                       memcpy(decoded->data, dst, dst_size);
-                                       decoded->size = (unsigned int)dst_size;
-                                       MMUTIL_SAFE_FREE(dst);
-                               } else {
+
+                               ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NV12, &dst, &res_w, &res_h, &res_buffer_size);
+
+                               MMUTIL_SAFE_FREE(decoded->data);
+
+                               decoded->data = calloc(1, res_buffer_size);
+                               if (decoded->data == NULL) {
                                        mm_util_debug("memory allocation failed");
+                                       MMUTIL_SAFE_FREE(dst);
                                        return MM_UTIL_ERROR_OUT_OF_MEMORY;
                                }
+
+                               memcpy(decoded->data, dst, res_buffer_size);
+                               decoded->size = (unsigned int)res_buffer_size;
+
+                               MMUTIL_SAFE_FREE(dst);
                        }
                } else {
                        ret = __mm_image_decode_from_jpeg_file_with_libjpeg(decoded, filename, fmt, MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1);
@@ -1377,33 +1371,28 @@ int mm_util_decode_from_jpeg_memory(mm_util_jpeg_yuv_data *decoded, void *src, u
 #else
        mm_util_debug("#START# libjpeg");
        if (fmt == MM_UTIL_COLOR_NV12) {
-               size_t dst_size = 0;
-               unsigned char *dst = NULL;
-
                ret = __mm_image_decode_from_jpeg_memory_with_libjpeg(decoded, src, size, MM_UTIL_COLOR_YUV420, MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1);
                if (ret == MM_UTIL_ERROR_NONE) {
-                       int err = MM_UTIL_ERROR_NONE;
-                       err = mm_util_get_image_size(MM_UTIL_COLOR_NV12, decoded->width, decoded->height, &dst_size);
-                       if (err != MM_UTIL_ERROR_NONE)
-                                       mm_util_error("fail mm_util_get_image_size");
-
-                       dst = calloc(1, dst_size);
-                       if (dst) {
-                               ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, dst, MM_UTIL_COLOR_NV12);
-                               MMUTIL_SAFE_FREE(decoded->data);
-                               decoded->data = calloc(1, dst_size);
-                               if (decoded->data == NULL) {
-                                       mm_util_debug("memory allocation failed");
-                                       MMUTIL_SAFE_FREE(dst);
-                                       return MM_UTIL_ERROR_OUT_OF_MEMORY;
-                               }
-                               memcpy(decoded->data, dst, dst_size);
-                               decoded->size = (unsigned int)dst_size;
-                               MMUTIL_SAFE_FREE(dst);
-                       } else {
+                       unsigned int res_w = 0;
+                       unsigned int res_h = 0;
+                       size_t res_buffer_size = 0;
+                       unsigned char *dst = NULL;
+
+                       ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NV12, &dst, &res_w, &res_h, &res_buffer_size);
+
+                       MMUTIL_SAFE_FREE(decoded->data);
+
+                       decoded->data = calloc(1, res_buffer_size);
+                       if (decoded->data == NULL) {
                                mm_util_debug("memory allocation failed");
+                               MMUTIL_SAFE_FREE(dst);
                                return MM_UTIL_ERROR_OUT_OF_MEMORY;
                        }
+
+                       memcpy(decoded->data, dst, res_buffer_size);
+                       decoded->size = (unsigned int)res_buffer_size;
+
+                       MMUTIL_SAFE_FREE(dst);
                }
        } else {
                ret = __mm_image_decode_from_jpeg_memory_with_libjpeg(decoded, src, size, fmt, MM_UTIL_JPEG_DECODE_DOWNSCALE_1_1);
@@ -1459,32 +1448,28 @@ int mm_util_decode_from_jpeg_file_with_downscale(mm_util_jpeg_yuv_data *decoded,
 #else
                mm_util_debug("#START# libjpeg");
                if (fmt == MM_UTIL_COLOR_NV12) {
-                       size_t dst_size = 0;
                        ret = __mm_image_decode_from_jpeg_file_with_libjpeg(decoded, filename, MM_UTIL_COLOR_YUV420, downscale);
                        if (ret == MM_UTIL_ERROR_NONE) {
-                               int err = MM_UTIL_ERROR_NONE;
-                               err = mm_util_get_image_size(MM_UTIL_COLOR_NV12, decoded->width, decoded->height, &dst_size);
-                               if (err != MM_UTIL_ERROR_NONE)
-                                       mm_util_error("fail mm_util_get_image_size");
-
+                               unsigned int res_w = 0;
+                               unsigned int res_h = 0;
+                               size_t res_buffer_size = 0;
                                unsigned char *dst = NULL;
-                               dst = calloc(1, dst_size);
-                               if (dst) {
-                                       ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, dst, MM_UTIL_COLOR_NV12);
-                                       MMUTIL_SAFE_FREE(decoded->data);
-                                       decoded->data = calloc(1, dst_size);
-                                       if (decoded->data == NULL) {
-                                               mm_util_debug("memory allocation failed");
-                                               MMUTIL_SAFE_FREE(dst);
-                                               return MM_UTIL_ERROR_OUT_OF_MEMORY;
-                                       }
-                                       memcpy(decoded->data, dst, dst_size);
-                                       decoded->size = (unsigned int)dst_size;
-                                       MMUTIL_SAFE_FREE(dst);
-                               } else {
+
+                               ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NV12, &dst, &res_w, &res_h, &res_buffer_size);
+
+                               MMUTIL_SAFE_FREE(decoded->data);
+
+                               decoded->data = calloc(1, res_buffer_size);
+                               if (decoded->data == NULL) {
                                        mm_util_debug("memory allocation failed");
+                                       MMUTIL_SAFE_FREE(dst);
                                        return MM_UTIL_ERROR_OUT_OF_MEMORY;
                                }
+
+                               memcpy(decoded->data, dst, res_buffer_size);
+                               decoded->size = (unsigned int)res_buffer_size;
+
+                               MMUTIL_SAFE_FREE(dst);
                        }
                } else {
                        ret = __mm_image_decode_from_jpeg_file_with_libjpeg(decoded, filename, fmt, downscale);
@@ -1533,33 +1518,27 @@ int mm_util_decode_from_jpeg_memory_with_downscale(mm_util_jpeg_yuv_data *decode
 #else
        mm_util_debug("#START# libjpeg");
        if (fmt == MM_UTIL_COLOR_NV12) {
-               size_t dst_size = 0;
-               unsigned char *dst = NULL;
-
                ret = __mm_image_decode_from_jpeg_memory_with_libjpeg(decoded, src, size, MM_UTIL_COLOR_YUV420, downscale);
                if (ret == MM_UTIL_ERROR_NONE) {
-                       int err = MM_UTIL_ERROR_NONE;
-                       err = mm_util_get_image_size(MM_UTIL_COLOR_NV12, decoded->width, decoded->height, &dst_size);
-                       if (err != MM_UTIL_ERROR_NONE)
-                                       mm_util_error("fail mm_util_get_image_size");
-
-                       dst = calloc(1, dst_size);
-                       if (dst) {
-                               ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, dst, MM_UTIL_COLOR_NV12);
-                               MMUTIL_SAFE_FREE(decoded->data);
-                               decoded->data = calloc(1, dst_size);
-                               if (decoded->data == NULL) {
-                                       mm_util_debug("memory allocation failed");
-                                       MMUTIL_SAFE_FREE(dst);
-                                       return MM_UTIL_ERROR_OUT_OF_MEMORY;
-                               }
-                               memcpy(decoded->data, dst, dst_size);
-                               decoded->size = (unsigned int)dst_size;
-                               MMUTIL_SAFE_FREE(dst);
-                       } else {
+                       unsigned int res_w = 0;
+                       unsigned int res_h = 0;
+                       size_t res_buffer_size = 0;
+                       unsigned char *dst = NULL;
+
+                       ret = mm_util_convert_colorspace(decoded->data, decoded->width, decoded->height, MM_UTIL_COLOR_YUV420, MM_UTIL_COLOR_NV12, &dst, &res_w, &res_h, &res_buffer_size);
+
+                       MMUTIL_SAFE_FREE(decoded->data);
+
+                       decoded->data = calloc(1, res_buffer_size);
+                       if (decoded->data == NULL) {
                                mm_util_debug("memory allocation failed");
+                               MMUTIL_SAFE_FREE(dst);
                                return MM_UTIL_ERROR_OUT_OF_MEMORY;
                        }
+                       memcpy(decoded->data, dst, res_buffer_size);
+                       decoded->size = (unsigned int)res_buffer_size;
+
+                       MMUTIL_SAFE_FREE(dst);
                }
        } else {
                ret = __mm_image_decode_from_jpeg_memory_with_libjpeg(decoded, src, size, fmt, downscale);