Revise media packet creation related functions 64/250964/8 submit/tizen/20210108.020109
authorJeongmo Yang <jm80.yang@samsung.com>
Wed, 6 Jan 2021 06:48:05 +0000 (15:48 +0900)
committerJeongmo Yang <jm80.yang@samsung.com>
Thu, 7 Jan 2021 09:41:50 +0000 (18:41 +0900)
[Version] 0.1.30
[Issue Type] Refactoring

Change-Id: I8f42dbbe1f5c99b3b301ccf35d35edb7dfc45f69
Signed-off-by: Jeongmo Yang <jm80.yang@samsung.com>
packaging/capi-media-tool.spec
src/media_packet.c

index 5633c6feb92a5f6fcc3aa97f699247d85ebc271c..d8e32670c495cfdce1dfde3936491f36523aa7f1 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-tool
 Summary:    A Core API media tool library in Tizen Native API
-Version:    0.1.29
+Version:    0.1.30
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 28f1e32450bc4dae1e708cf9321b05fc16698cc0..0e2f92d3bce78eadc6f0dd6125bc389a9b2b80c7 100644 (file)
 /* NOTE : static internal functions does not check anything on incomming parameters
  * Caller should takecare it
  */
+static int  __pkt_init(media_buffer_type_e type, media_format_h fmt, media_packet_finalize_cb fcb, void *fcb_data, media_packet_s **handle);
+static void __pkt_deinit(media_packet_s *handle);
 static size_t __pkt_calculate_video_buffer_size(media_packet_s *pkt);
 static size_t __pkt_calculate_audio_buffer_size(media_packet_s *pkt);
 static size_t __pkt_calculate_text_buffer_size(media_packet_s *pkt);
 static uint32_t _convert_to_tbm_surface_format(media_format_mimetype_e format_type);
 static void *__aligned_malloc_normal_buffer_type(size_t size, int alignment);
 static void __aligned_free_normal_buffer_type(void *buffer_ptr);
-static int __pkt_alloc_buffer(media_packet_s *pkt);
+static int __pkt_alloc_buffer(media_packet_s *handle);
 static int __pkt_dealloc_buffer(media_packet_s *handle);
 
-int media_packet_create_alloc(media_format_h fmt, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *packet)
+static int __pkt_init(media_buffer_type_e type, media_format_h fmt,
+       media_packet_finalize_cb fcb, void *fcb_data, media_packet_s **handle)
 {
-       media_packet_s *handle = NULL;
-       int ret = MEDIA_PACKET_ERROR_NONE;
+       media_packet_s *new_handle = NULL;
 
        MEDIA_PACKET_INSTANCE_CHECK(fmt);
-       MEDIA_PACKET_NULL_ARG_CHECK(packet);
+       MEDIA_PACKET_NULL_ARG_CHECK(handle);
+
        if (MEDIA_FORMAT_GET_REFCOUNT(fmt) < 1) {
-               LOGE("The media format handle is corrupted or Not set media info");     //LCOV_EXCL_LINE
+               LOGE("The media format is corrupted or Not set media info");    //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
        }
 
-       if (!MEDIA_FORMAT_IS_VIDEO(fmt) && !MEDIA_FORMAT_IS_AUDIO(fmt)
-               && !MEDIA_FORMAT_IS_TEXT(fmt)) {
-               LOGE("The media format handle is not specified. set video info, audio info or text info...");   //LCOV_EXCL_LINE
+       if (!MEDIA_FORMAT_IS_VIDEO(fmt) && !MEDIA_FORMAT_IS_AUDIO(fmt) && !MEDIA_FORMAT_IS_TEXT(fmt)) {
+               LOGE("The media format is not specified. set video info, audio info or text info");
                return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
-       handle = (media_packet_s *)malloc(sizeof(media_packet_s));
-       if (handle != NULL)
-               memset(handle, 0, sizeof(media_packet_s));
-       else {
+       new_handle = calloc(1, sizeof(media_packet_s));
+       if (!new_handle) {
                LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
-               ret = MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
-               goto fail;
+               return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
        }
 
-       /* TODO : need to consider to give application to select buffer type(tbm or heap) */
+       new_handle->type = type;
+       new_handle->format = MEDIA_FORMAT_CAST(fmt);
+       new_handle->finalizecb_func = fcb;
+       new_handle->userdata = fcb_data;
 
-       /* check if tbm surface is needed.
-        * This time, Only raw video format is considered to be allocated in TBM
-        */
-       if (MEDIA_FORMAT_IS_VIDEO(fmt) && MEDIA_FORMAT_IS_RAW(fmt))
-               handle->type = MEDIA_BUFFER_TYPE_TBM_SURFACE;
-       else
-               handle->type = MEDIA_BUFFER_TYPE_NORMAL;
+       media_format_ref((media_format_h)new_handle->format);
 
-       /* take fmt */
-       handle->format = MEDIA_FORMAT_CAST(fmt);
+       *handle = new_handle;
 
-       /* alloc buffer */
-       ret = __pkt_alloc_buffer(handle);
-       if (ret != MEDIA_PACKET_ERROR_NONE) {
-               LOGE("failed __pkt_alloc_buffer(), err = (0x%08x)", ret);       //LCOV_EXCL_LINE
-               goto fail;
-       }
-
-       /* allocated buffer */
-       handle->is_allocated = true;
+       LOGI("new packet[type:%d] %p", type, *handle);
 
-       /* set finalized callback and user data */
-       handle->finalizecb_func = fcb;
-       handle->userdata = fcb_data;
-
-       /* increase format reference count */
-       media_format_ref((media_format_h)handle->format);
+       return MEDIA_PACKET_ERROR_NONE;
+}
 
-       /* take handle */
-       *packet = (media_packet_h)handle;
-       LOGI("new handle : %p", *packet);
-       return ret;
+static void __pkt_deinit(media_packet_s *handle)
+{
+       MEDIA_PACKET_NULL_ARG_CHECK(handle);
 
-fail:
+       media_format_unref(handle->format);
+       handle->format = NULL;
 
-       if (handle) {
-               free(handle);
-               handle = NULL;
-       }
+       memset(handle, 0x0, sizeof(media_packet_s));
 
-       *packet = NULL;
-       return ret;
+       free(handle);
 }
 
+
 int media_packet_create(media_format_h fmt, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *packet)
 {
-       media_packet_s *handle = NULL;
-
        MEDIA_PACKET_INSTANCE_CHECK(fmt);
-       MEDIA_PACKET_NULL_ARG_CHECK(packet);
-       if (MEDIA_FORMAT_GET_REFCOUNT(fmt) < 1) {
-               LOGE("The media format handle is corrupted or Not set media info");     //LCOV_EXCL_LINE
-               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
-       }
-
-       if (!MEDIA_FORMAT_IS_VIDEO(fmt) && !MEDIA_FORMAT_IS_AUDIO(fmt)
-               && !MEDIA_FORMAT_IS_TEXT(fmt)) {
-               LOGE("The media format handle is not specified. set video info, audio info or text info...");
-               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
-       }
-       /* TODO : need more validation on fmt */
-
-       handle = (media_packet_s *)malloc(sizeof(media_packet_s));
-       if (handle != NULL)
-               memset(handle, 0, sizeof(media_packet_s));
-       else {
-               LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
-               return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
-       }
-
-       /* TODO : need to consider to give application to select buffer type(tbm or heap) */
 
-       /* check if tbm surface is needed.
-        * This time, Only raw video format is considered to be allocated in TBM
-        */
        if (MEDIA_FORMAT_IS_VIDEO(fmt) && MEDIA_FORMAT_IS_RAW(fmt))
-               handle->type = MEDIA_BUFFER_TYPE_TBM_SURFACE;
+               return __pkt_init(MEDIA_BUFFER_TYPE_TBM_SURFACE, fmt, fcb, fcb_data, packet);
        else
-               handle->type = MEDIA_BUFFER_TYPE_NORMAL;
+               return __pkt_init(MEDIA_BUFFER_TYPE_NORMAL, fmt, fcb, fcb_data, packet);
+}
 
-       /* take fmt */
-       handle->format = MEDIA_FORMAT_CAST(fmt);
 
-       /* NOT allocated buffer */
-       handle->is_allocated = false;
+int media_packet_create_alloc(media_format_h fmt, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *packet)
+{
+       media_packet_s *handle = NULL;
+       int ret = MEDIA_PACKET_ERROR_NONE;
 
-       /* set finalized callback and user data */
-       handle->finalizecb_func = fcb;
-       handle->userdata = fcb_data;
+       ret = media_packet_create(fmt, fcb, fcb_data, (media_packet_h)&handle);
+       if (ret != MEDIA_PACKET_ERROR_NONE) {
+               LOGE("failed 0x%x", ret);
+               return ret;
+       }
 
-       /* increase format reference count */
-       media_format_ref((media_format_h)handle->format);
+       /* alloc buffer */
+       ret = __pkt_alloc_buffer(handle);
+       if (ret != MEDIA_PACKET_ERROR_NONE) {
+               LOGE("failed __pkt_alloc_buffer(), err = (0x%08x)", ret);       //LCOV_EXCL_LINE
+               __pkt_deinit(handle);   //LCOV_EXCL_LINE
+               return ret;
+       }
 
        /* take handle */
        *packet = (media_packet_h)handle;
+
        LOGI("new handle : %p", *packet);
-       return MEDIA_PACKET_ERROR_NONE;
 
+       return MEDIA_PACKET_ERROR_NONE;
 }
 
+
 int media_packet_copy(media_packet_h org_packet, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *new_packet)
 {
        media_packet_s *handle;
@@ -175,10 +138,8 @@ int media_packet_copy(media_packet_h org_packet, media_packet_finalize_cb fcb, v
 
        org_handle = (media_packet_s *)org_packet;
 
-       handle = (media_packet_s *)malloc(sizeof(media_packet_s));
-       if (handle != NULL)
-               memset(handle, 0, sizeof(media_packet_s));
-       else {
+       handle = calloc(1, sizeof(media_packet_s));
+       if (!handle) {
                LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
        }
@@ -201,130 +162,86 @@ int media_packet_copy(media_packet_h org_packet, media_packet_finalize_cb fcb, v
 
        /* take handle */
        *new_packet = (media_packet_h)handle;
+
        LOGI("new handle : %p", *new_packet);
-       return MEDIA_PACKET_ERROR_NONE;
 
+       return MEDIA_PACKET_ERROR_NONE;
 }
 
 int media_packet_alloc(media_packet_h packet)
 {
        LOGI("start");
-       media_packet_s *handle;
-       int ret = MEDIA_PACKET_ERROR_NONE;
-
-       MEDIA_PACKET_INSTANCE_CHECK(packet);
-
-       handle = (media_packet_s *)packet;
-
-       /* alloc buffer */
-       int err = __pkt_alloc_buffer(handle);
-       if (err != MEDIA_PACKET_ERROR_NONE) {
-               LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
-               ret = err;
-               goto fail;
-       }
-
-       /* allocated buffer */
-       handle->is_allocated = true;
-
-       LOGI("end");
-       return ret;
-
-fail:
-       if (handle) {
-               free(handle);
-               handle = NULL;
-       }
-       return ret;
 
+       return __pkt_alloc_buffer((media_packet_s *)packet);
 }
 
-static int __pkt_alloc_buffer(media_packet_s *pkt)
+static int __pkt_alloc_buffer(media_packet_s *handle)
 {
-       /* skip validating pkt */
+       int tbm_ret = TBM_SURFACE_ERROR_NONE;
        size_t buffersize = 0;
+       tbm_surface_info_s surface_info;
+
+       MEDIA_PACKET_INSTANCE_CHECK(handle);
+
+       if (handle->is_allocated) {
+               LOGE("already allocated");
+               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
+       }
 
        /* initialize packet */
-       pkt->pts = CLOCK_TIME_NONE;
-       pkt->dts = CLOCK_TIME_NONE;
-       pkt->duration = CLOCK_TIME_NONE;
-       pkt->method = MEDIA_PACKET_ROTATE_IDENTITY;
+       handle->pts = CLOCK_TIME_NONE;
+       handle->dts = CLOCK_TIME_NONE;
+       handle->duration = CLOCK_TIME_NONE;
+       handle->method = MEDIA_PACKET_ROTATE_IDENTITY;
 
-       if (pkt->type == MEDIA_BUFFER_TYPE_NORMAL) {
+       if (handle->type == MEDIA_BUFFER_TYPE_NORMAL) {
                /* need to use format,width,height to get buffer size */
-               if (MEDIA_FORMAT_IS_VIDEO(pkt->format))
-                       buffersize = __pkt_calculate_video_buffer_size(pkt);
-               else if (MEDIA_FORMAT_IS_AUDIO(pkt->format))
-                       buffersize = __pkt_calculate_audio_buffer_size(pkt);
+               if (MEDIA_FORMAT_IS_VIDEO(handle->format))
+                       buffersize = __pkt_calculate_video_buffer_size(handle);
+               else if (MEDIA_FORMAT_IS_AUDIO(handle->format))
+                       buffersize = __pkt_calculate_audio_buffer_size(handle);
                else
-                       buffersize = __pkt_calculate_text_buffer_size(pkt);
+                       buffersize = __pkt_calculate_text_buffer_size(handle);
 
                if (buffersize == 0)
                        return MEDIA_PACKET_ERROR_INVALID_OPERATION;
 
                /* 16bytes aligned malloc */
-               pkt->data = __aligned_malloc_normal_buffer_type(buffersize, 16);
-               if (!pkt->data)
+               handle->data = __aligned_malloc_normal_buffer_type(buffersize, 16);
+               if (!handle->data) {
+                       LOGE("alloc normal buffer failed, size %zu", buffersize);
                        return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
-               pkt->size = buffersize;
-       } else if (pkt->type == MEDIA_BUFFER_TYPE_TBM_SURFACE) {
-
-               /* convert to tbm_format */
-               uint32_t tbm_format;
-               tbm_format = _convert_to_tbm_surface_format(pkt->format->mimetype);
-
-#if 0
-               /*check whether given tbm_format is supported or not */
-               uint32_t *supported_formats;
-               uint32_t supported_format_num;
-               bool is_supported_format;
-               int i;
-
-               if (tbm_surface_query_formats(&supported_formats, &supported_format_num)) {
-                       is_supported_format = false;
-                       for (i = 0; i < supported_format_num; i++) {
-                               if (supported_formats[i] == tbm_format) {
-                                       is_supported_format = true;
-                                       break;
-                               }
-                       }
-
-                       free(supported_formats);
-               } else {
-                       /* tbm_surface_query_format returns error */
-                       LOGE("tbm_surface_query_format is failed..");
-                       return MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
 
-               /* if not supported tbm_format by backend, don't need to do tbm_surface_create() */
-               if (!is_supported_format) {
-                       LOGE("the backend doesn't support 0x%x mimetype", pkt->format->mimetype);
+               handle->size = buffersize;
+       } else if (handle->type == MEDIA_BUFFER_TYPE_TBM_SURFACE) {
+               /*create tbm_surface */
+               handle->surface_data = (void *)tbm_surface_create(handle->format->detail.video.width,
+                       handle->format->detail.video.height, _convert_to_tbm_surface_format(handle->format->mimetype));
+               if (handle->surface_data == NULL) {
+                       LOGE("tbm_surface_create() is failed!!");       //LCOV_EXCL_LINE
                        return MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
-#endif
-               /*create tbm_surface */
-               pkt->surface_data = (void *)tbm_surface_create(pkt->format->detail.video.width, pkt->format->detail.video.height, tbm_format);
-               if (pkt->surface_data != NULL) {
-
-                       /* get tbm_surface_info */
-                       tbm_surface_info_s surface_info;
-                       int err = tbm_surface_get_info((tbm_surface_h)pkt->surface_data, &surface_info);
-                       if (err == TBM_SURFACE_ERROR_NONE) {
-                               pkt->data = surface_info.planes[0].ptr;
-                               pkt->size = (size_t)surface_info.size;
-                               LOGD("tbm_surface_created, pkt->size = %d\n", pkt->size);
-                       } else {
-                               LOGE("tbm_surface_get_info() is failed.. err = 0x%08x \n", err);        //LCOV_EXCL_LINE
-                               tbm_surface_destroy((tbm_surface_h)pkt->surface_data);
-                               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
-                       }
-               } else {
-                       LOGE("\n tbm_surface_create() is failed!! \n"); //LCOV_EXCL_LINE
+
+               /* get tbm_surface_info */
+               tbm_ret = tbm_surface_get_info((tbm_surface_h)handle->surface_data, &surface_info);
+               if (tbm_ret != TBM_SURFACE_ERROR_NONE) {
+                       LOGE("tbm_surface_get_info() is failed 0x%x", tbm_ret); //LCOV_EXCL_LINE
+                       tbm_surface_destroy((tbm_surface_h)handle->surface_data);
                        return MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
 
+               handle->data = surface_info.planes[0].ptr;
+               handle->size = (size_t)surface_info.size;
+
+               LOGD("tbm_surface_created, size = %d", handle->size);
+       } else {
+               LOGE("should not be reached here, type %d", handle->type);
+               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
+       handle->is_allocated = true;
+
        return MEDIA_PACKET_ERROR_NONE;
 }
 
@@ -352,6 +269,8 @@ static int __pkt_dealloc_buffer(media_packet_s *handle)
                handle->codec_data_size = 0;
        }
 
+       handle->is_allocated = false;
+
        return MEDIA_PACKET_ERROR_NONE;
 }
 
@@ -423,12 +342,12 @@ static size_t __pkt_calculate_video_buffer_size(media_packet_s *pkt)
                buffersize = (stride + (_ROUND_UP_16(width) / 2)) * height;
                break;
        default:
-               LOGE("Not supported format\n"); //LCOV_EXCL_LINE
+               LOGE("Not supported format");   //LCOV_EXCL_LINE
                return 0;
        }
 
        buffersize += BUFFER_PADDING_SIZE;
-       LOGD("format 0x%x, buffersize %d\n", pkt->format->mimetype, buffersize);
+       LOGD("format 0x%x, buffersize %d", pkt->format->mimetype, buffersize);
 
        return buffersize;
 }
@@ -534,12 +453,12 @@ static size_t __pkt_calculate_audio_buffer_size(media_packet_s *pkt)
                buffersize = (OPUS_MAX_FRM_SIZE * OPUS_MAX_NCH) * 2;    /* 2 = (16bit/8) */
                break;
        default:
-               LOGE("Not supported format\n"); //LCOV_EXCL_LINE
+               LOGE("Not supported format");   //LCOV_EXCL_LINE
                return 0;
        }
 
        buffersize += BUFFER_PADDING_SIZE;
-       LOGD("format 0x%x, buffersize %d\n", pkt->format->mimetype, buffersize);
+       LOGD("format 0x%x, buffersize %d", pkt->format->mimetype, buffersize);
 
        return buffersize;
 }
@@ -553,7 +472,7 @@ static size_t __pkt_calculate_text_buffer_size(media_packet_s *pkt)
                buffersize = TXT_MAX_FRM_SIZE;
                break;
        default:
-               LOGE("Not supported text format\n");    //LCOV_EXCL_LINE
+               LOGE("Not supported text format");      //LCOV_EXCL_LINE
                return 0;
        }
        return buffersize;
@@ -561,126 +480,65 @@ static size_t __pkt_calculate_text_buffer_size(media_packet_s *pkt)
 
 int media_packet_create_from_tbm_surface(media_format_h fmt, tbm_surface_h surface, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *packet)
 {
-       media_packet_s *handle;
+       media_packet_s *handle = NULL;
+       tbm_surface_info_s surface_info;
        int ret = MEDIA_PACKET_ERROR_NONE;
+       int tbm_ret = TBM_SURFACE_ERROR_NONE;
 
-       if (surface == NULL)
-               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
-
-       MEDIA_PACKET_INSTANCE_CHECK(fmt);
        MEDIA_PACKET_INSTANCE_CHECK(surface);
-       MEDIA_PACKET_NULL_ARG_CHECK(packet);
-       if (!MEDIA_FORMAT_IS_VIDEO(fmt) && !MEDIA_FORMAT_IS_AUDIO(fmt)) {
-               LOGE("The media format handle is not specified. set video info or audio info...");      //LCOV_EXCL_LINE
+
+       tbm_ret = tbm_surface_get_info(surface, &surface_info);
+       if (tbm_ret != TBM_SURFACE_ERROR_NONE) {
+               LOGE("tbm surface[%p] info failed 0x%x", surface, tbm_ret);
                return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
-       handle = (media_packet_s *)malloc(sizeof(media_packet_s));
-       if (handle != NULL) {
-               memset(handle, 0, sizeof(media_packet_s));
-       } else {
-               LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
-               return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
+       ret = __pkt_init(MEDIA_BUFFER_TYPE_EXTERNAL_TBM_SURFACE, fmt, fcb, fcb_data, &handle);
+       if (ret != MEDIA_PACKET_ERROR_NONE) {
+               LOGE("handle init failed 0x%x", ret);
+               return ret;
        }
 
-       handle->type = MEDIA_BUFFER_TYPE_EXTERNAL_TBM_SURFACE;
        handle->surface_data = (void *)surface;
-
-       /* alloc handle->format */
-       handle->format = MEDIA_FORMAT_CAST(fmt);
-
-       /* get tbm_surface_info */
-       tbm_surface_info_s surface_info;
-       int err = tbm_surface_get_info((tbm_surface_h)handle->surface_data, &surface_info);
-       if (err == TBM_SURFACE_ERROR_NONE) {
-               handle->data = surface_info.planes[0].ptr;
-               handle->size = (uint64_t)surface_info.size;
-       } else {
-               LOGE("tbm_surface_get_info() is failed.. err =0x%08x", err);
-               ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
-               goto fail;
-       }
-
-       /* allocated buffer */
+       handle->data = surface_info.planes[0].ptr;
+       handle->size = (uint64_t)surface_info.size;
        handle->is_allocated = true;
 
-       /* set finalized callback and user data */
-       handle->finalizecb_func = fcb;
-       handle->userdata = fcb_data;
-
-       /* increase format reference count */
-       ret = media_format_ref((media_format_h)handle->format);
-
-       /* take handle */
        *packet = (media_packet_h)handle;
-       LOGI("new handle : %p", *packet);
-       return ret;
-
-fail:
 
-       if (handle) {
-               free(handle);
-               handle = NULL;
-       }
+       LOGI("surface %p, data %p, size %zu", handle->surface_data, handle->data, handle->size);
 
-       *packet = NULL;
        return ret;
 }
 
 int media_packet_create_from_external_memory(media_format_h fmt, void *mem_ptr, uint64_t size, media_packet_finalize_cb fcb, void *fcb_data, media_packet_h *packet)
 {
-       media_packet_s *handle;
+       media_packet_s *handle = NULL;
        int ret = MEDIA_PACKET_ERROR_NONE;
 
-       if (mem_ptr == NULL)
-               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
-
-       if (!(size > 0))
-               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
-
        MEDIA_PACKET_INSTANCE_CHECK(fmt);
+       MEDIA_PACKET_NULL_ARG_CHECK(mem_ptr);
        MEDIA_PACKET_NULL_ARG_CHECK(packet);
-
-       if (!MEDIA_FORMAT_IS_VIDEO(fmt) && !MEDIA_FORMAT_IS_AUDIO(fmt)) {
-               LOGE("The media format handle is not specified. set video info or audio info...");
-               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
-       }
+       MEDIA_PACKET_CHECK_CONDITION(size > 0, "invalid size", MEDIA_PACKET_ERROR_INVALID_PARAMETER);
 
        if (MEDIA_FORMAT_IS_RAW(fmt) && MEDIA_FORMAT_IS_VIDEO(fmt)) {
                LOGE("failed!. it supports only 'MEDIA_FORMAT_ENCODED' type."); //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
        }
 
-       handle = (media_packet_s *)malloc(sizeof(media_packet_s));
-       if (handle != NULL) {
-               memset(handle, 0, sizeof(media_packet_s));
-       } else {
-               LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);     //LCOV_EXCL_LINE
-               return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
+       ret = __pkt_init(MEDIA_BUFFER_TYPE_EXTERNAL_MEMORY, fmt, fcb, fcb_data, &handle);
+       if (ret != MEDIA_PACKET_ERROR_NONE) {
+               LOGE("handle init failed 0x%x", ret);
+               return ret;
        }
 
-       handle->type = MEDIA_BUFFER_TYPE_EXTERNAL_MEMORY;
-
-       /* alloc handle->format */
-       handle->format = MEDIA_FORMAT_CAST(fmt);
-
-       /* set external allocated memory & external memory size */
        handle->size = size;
        handle->data = mem_ptr;
-
-       /* allocated buffer */
        handle->is_allocated = true;
 
-       /* set finalized callback and user data */
-       handle->finalizecb_func = fcb;
-       handle->userdata = fcb_data;
-
-       /* increase format reference count */
-       ret = media_format_ref((media_format_h)handle->format);
-
-       /* take handle */
        *packet = (media_packet_h)handle;
-       LOGI("new handle : %p", *packet);
+
+       LOGI("data %p, size %zu", handle->data, handle->size);
 
        return ret;
 }
@@ -741,7 +599,7 @@ int media_packet_set_format(media_packet_h packet, media_format_h fmt)
 
        /* if trying to set same format,  return */
        if (handle->format == MEDIA_FORMAT_CAST(fmt)) {
-               LOGE("The packet handle already refers the format handle..\n"); //LCOV_EXCL_LINE
+               LOGE("The packet handle already refers the format handle");     //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
@@ -1104,15 +962,15 @@ int media_packet_get_number_of_video_planes(media_packet_h packet, uint32_t *num
                int err = tbm_surface_get_info((tbm_surface_h)handle->surface_data, &surface_info);
                if (err == TBM_SURFACE_ERROR_NONE) {
                        *num = surface_info.num_planes;
-                       LOGD(" surface_info the number of planes = %d\n", (int)surface_info.num_planes);
+                       LOGD(" surface_info the number of planes = %d", (int)surface_info.num_planes);
                } else {
                        *num = 0;
-                       LOGE("tbm_surface_get_info() is failed.. 0x%08x \n", err);      //LCOV_EXCL_LINE
+                       LOGE("tbm_surface_get_info() is failed.. 0x%08x", err); //LCOV_EXCL_LINE
                        ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
        } else {
                *num = 0;
-               LOGE("The packet handle doesn't have tbm_surface buffer type...\n");
+               LOGE("The packet handle doesn't have tbm_surface buffer type");
                ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
 
        }
@@ -1129,7 +987,7 @@ int media_packet_get_video_stride_width(media_packet_h packet, int plane_idx, in
        MEDIA_PACKET_INSTANCE_CHECK(packet);
        MEDIA_PACKET_NULL_ARG_CHECK(stride_width);
        if (plane_idx < 0) {
-               LOGE("Invalid plane_idx : %d, must not be negative number\n", plane_idx);
+               LOGE("Invalid plane_idx : %d, must not be negative number", plane_idx);
                return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
        }
 
@@ -1145,17 +1003,17 @@ int media_packet_get_video_stride_width(media_packet_h packet, int plane_idx, in
                        if (surface_info.num_planes > plane_idx) {
                                *stride_width = surface_info.planes[plane_idx].stride;
                        } else {
-                               LOGE("the plane_idx is invalid, The number of planes = %lu\n", surface_info.num_planes);
+                               LOGE("the plane_idx is invalid, The number of planes = %lu", surface_info.num_planes);
                                *stride_width = 0;
                                ret = MEDIA_PACKET_ERROR_INVALID_PARAMETER;
                        }
                } else {
                        *stride_width = 0;
-                       LOGE("tbm_surface_get_info() is failed.. 0x%08x \n", err);      //LCOV_EXCL_LINE
+                       LOGE("tbm_surface_get_info() is failed.. 0x%08x", err); //LCOV_EXCL_LINE
                        ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
        } else {
-               LOGE("The packet handle doesn't have tbm_surface buffer type...\n");
+               LOGE("The packet handle doesn't have tbm_surface buffer type");
                *stride_width = 0;
                ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
@@ -1172,7 +1030,7 @@ int media_packet_get_video_stride_height(media_packet_h packet, int plane_idx, i
        MEDIA_PACKET_INSTANCE_CHECK(packet);
        MEDIA_PACKET_NULL_ARG_CHECK(stride_height);
        if (plane_idx < 0) {
-               LOGE("Invalid plane_idx : %d, must not be negative number\n", plane_idx);       //LCOV_EXCL_LINE
+               LOGE("Invalid plane_idx : %d, must not be negative number", plane_idx); //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
        }
 
@@ -1187,17 +1045,17 @@ int media_packet_get_video_stride_height(media_packet_h packet, int plane_idx, i
                        if (surface_info.num_planes > plane_idx) {
                                *stride_height = surface_info.planes[plane_idx].size / surface_info.planes[plane_idx].stride;
                        } else {
-                               LOGE("the plane_idx is invalid, The number of planes = %lu\n", surface_info.num_planes);        //LCOV_EXCL_LINE
+                               LOGE("the plane_idx is invalid, The number of planes = %lu", surface_info.num_planes);  //LCOV_EXCL_LINE
                                *stride_height = 0;
                                ret = MEDIA_PACKET_ERROR_INVALID_PARAMETER;
                        }
                } else {
                        *stride_height = 0;
-                       LOGE("tbm_surface_get_info() is failed.. 0x%08x \n", err);      //LCOV_EXCL_LINE
+                       LOGE("tbm_surface_get_info() is failed.. 0x%08x", err); //LCOV_EXCL_LINE
                        ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
        } else {
-               LOGE("The packet handle doesn't have tbm_surface buffer type...\n");
+               LOGE("The packet handle doesn't have tbm_surface buffer type");
                *stride_height = 0;
                ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
@@ -1215,7 +1073,7 @@ int media_packet_get_video_plane_data_ptr(media_packet_h packet, int plane_idx,
        MEDIA_PACKET_INSTANCE_CHECK(packet);
        MEDIA_PACKET_NULL_ARG_CHECK(plane_data_ptr);
        if (plane_idx < 0) {
-               LOGE("Invalid plane_idx : %d, must not be negative number\n", plane_idx);       //LCOV_EXCL_LINE
+               LOGE("Invalid plane_idx : %d, must not be negative number", plane_idx); //LCOV_EXCL_LINE
                return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
        }
 
@@ -1229,19 +1087,19 @@ int media_packet_get_video_plane_data_ptr(media_packet_h packet, int plane_idx,
                if (err == TBM_SURFACE_ERROR_NONE) {
                        if (surface_info.num_planes > plane_idx) {
                                *plane_data_ptr = surface_info.planes[plane_idx].ptr;
-                               LOGD("the tbm_surface_info.planes[%d].ptr = %p\n", plane_idx, surface_info.planes[plane_idx].ptr);
+                               LOGD("the tbm_surface_info.planes[%d].ptr = %p", plane_idx, surface_info.planes[plane_idx].ptr);
                        } else {
-                               LOGE("the plane_idx is invalid, The number of planes = %lu\n", surface_info.num_planes);        //LCOV_EXCL_LINE
+                               LOGE("the plane_idx is invalid, The number of planes = %lu", surface_info.num_planes);  //LCOV_EXCL_LINE
                                *plane_data_ptr = NULL;
                                ret = MEDIA_PACKET_ERROR_INVALID_PARAMETER;
                        }
                } else {
-                       LOGE("tbm_surface_get_info() is failed.. 0x%08x \n", err);      //LCOV_EXCL_LINE
+                       LOGE("tbm_surface_get_info() is failed.. 0x%08x", err); //LCOV_EXCL_LINE
                        *plane_data_ptr = NULL;
                        ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
                }
        } else {
-               LOGI("The packet handle doesn't have tbm_surface buffer type...\n");
+               LOGI("The packet handle doesn't have tbm_surface buffer type");
                *plane_data_ptr = NULL;
                ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
@@ -1260,7 +1118,7 @@ int media_packet_get_codec_data(media_packet_h packet, void **codec_data, unsign
 
        handle = (media_packet_s *)packet;
 
-       LOGI("Get: codec data = %p, codec_data_size = %u\n", handle->codec_data, handle->codec_data_size);      //LCOV_EXCL_LINE
+       LOGI("Get: codec data = %p, codec_data_size = %u", handle->codec_data, handle->codec_data_size);        //LCOV_EXCL_LINE
 
        if (handle->codec_data) {
                *codec_data_size = handle->codec_data_size;
@@ -1269,7 +1127,7 @@ int media_packet_get_codec_data(media_packet_h packet, void **codec_data, unsign
                *codec_data = NULL;
                *codec_data_size = 0;
 
-               LOGE("There is no codec data..\n");
+               LOGE("There is no codec data");
                ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
@@ -1302,13 +1160,10 @@ int media_packet_destroy(media_packet_h packet)
                LOGE("failed __pkt_dealloc_buffer(), err = (0x%08x)", ret);     //LCOV_EXCL_LINE
                return ret;
        }
-       /* unreference media_format */
-       media_format_unref(handle->format);
 
-       LOGI("The packet handle(%p) will be destroyed..\n", handle);    //LCOV_EXCL_LINE
+       LOGI("The packet handle(%p) will be destroyed", handle);        //LCOV_EXCL_LINE
 
-       free(handle);
-       handle = NULL;
+       __pkt_deinit(handle);
 
        return ret;
 }