media_format: Add new internal APIs for codec data 13/291013/4
authorJeongmo Yang <jm80.yang@samsung.com>
Thu, 6 Apr 2023 10:36:50 +0000 (19:36 +0900)
committerJeongmo Yang <jm80.yang@samsung.com>
Mon, 10 Apr 2023 22:37:24 +0000 (07:37 +0900)
- int media_format_set_codec_data(media_format_h fmt, void *codec_data, unsigned int size);
- int media_format_get_codec_data(media_format_h fmt, void **codec_data, unsigned int *size);

[Version] 0.1.56
[Issue Type] New feature

Change-Id: I43d8ac2b6f47a9723fa2e4150432ca2f44bd9384
Signed-off-by: Jeongmo Yang <jm80.yang@samsung.com>
include/media_format_internal.h
include/media_format_private.h
include/media_packet_private.h
packaging/capi-media-tool.spec
src/media_format.c
src/media_format_internal.c
src/media_packet.c
src/media_packet_internal.c
test/media_packet_test.c

index 9f9fdf5631383f17cc7152525944920808059611..a7f3e0cda4159f144346a01670d37af1fc3e815a 100644 (file)
@@ -37,7 +37,7 @@ extern "C" {
  * @internal
  * @brief Get reference count of #media_format_h object.
  * @since_tizen 7.0
- * @param[in] fmt The #media_format_h to get
+ * @param[in] fmt The media format handle
  * @param[out] ref_count Reference count of #media_format_h
  *
  * @return @c 0 on success,
@@ -49,6 +49,40 @@ extern "C" {
  */
 int media_format_get_refcount(media_format_h fmt, int *ref_count);
 
+/**
+ * @internal
+ * @brief Sets codec data and the codec data size.
+ * @since_tizen 7.5
+ * @param[in] fmt        The media format handle
+ * @param[in] codec_data The codec data to set
+ * @param[in] size       The size of codec data
+ *
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #MEDIA_FORMAT_ERROR_NONE Successful
+ * @retval #MEDIA_FORMAT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MEDIA_FORMAT_ERROR_INVALID_OPERATION Invalid operation
+ * @retval #MEDIA_FORMAT_ERROR_OUT_OF_MEMORY Out of memory
+ */
+int media_format_set_codec_data(media_format_h fmt, void *codec_data, unsigned int size);
+
+/**
+ * @internal
+ * @brief Gets codec data and the size.
+ * @since_tizen 7.5
+ * @remarks The @a codec_data should be released using free().
+ * @param[in]  fmt        The media format handle
+ * @param[out] codec_data The codec data to get
+ * @param[out] size       The size of codec data
+ *
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #MEDIA_FORMAT_ERROR_NONE Successful
+ * @retval #MEDIA_FORMAT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MEDIA_FORMAT_ERROR_INVALID_OPERATION Invalid operation
+ */
+int media_format_get_codec_data(media_format_h fmt, void **codec_data, unsigned int *size);
+
 /**
  * @}
  */
index b76ce478f87e5204ee2742de5bedd135fc65c337..281d2134a682a8d32e0f636a402ccf5ec4797f25 100644 (file)
@@ -178,6 +178,15 @@ typedef struct _media_fomat_text_spec_s {
        media_format_text_type_e type;                   /**< media format text (or subtile) codec type */
 } media_format_text_spec_s;
 
+/**
+ * @brief Media format for codec data.
+ * @since_tizen 7.5
+ */
+typedef struct _media_fomat_codec_data_s {
+       void *data;             /**< codec data */
+       unsigned int size;      /**< codec data size */
+} media_format_codec_data_s;
+
 /**
  * @brief Structure of media format.
  * @since_tizen 2.3
@@ -193,6 +202,7 @@ typedef struct _media_format_s {
                media_format_text_spec_s text;                  /**< media format struct video of media_format_text_spec_s (Since 3.0) */
        } detail;
 
+       media_format_codec_data_s codec_data;   /**< media format codec data */
 } media_format_s;
 
 #ifdef __cplusplus
index 489b7c5fd7b8dec825b5879dc373d0e9266c2d47..f722d0d83ea03fe5463d682270bc3678438088f8 100644 (file)
@@ -146,8 +146,6 @@ typedef struct _media_packet_s {
        bool is_allocated;
        bool using_pool;
        void *extradata;
-       void *codec_data;
-       unsigned int codec_data_size;
 
        media_format_s *format;
        media_buffer_type_e type;
index 418bfdfc1b6d76bb765641ae67164c077b989259..5a47ac2f9a7e682ea4744074a81a65c0b0de47ca 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-tool
 Summary:    A Core API media tool library in Tizen Native API
-Version:    0.1.55
+Version:    0.1.56
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 842c5a7e47b187e2327c3154039c164bc7608151..d989b991c027439836412fb54ed048aa219bec7c 100644 (file)
@@ -114,8 +114,10 @@ static void __media_format_destroy(media_format_s *fmt)
 {
        MEDIA_FORMAT_INSTANCE_CHEC_VOID(fmt);
 
+       if (fmt->codec_data.data)
+               free(fmt->codec_data.data);
+
        free(fmt);
-       fmt = NULL;
 }
 
 int media_format_get_type(media_format_h fmt, media_format_type_e *formattype)
index c0c1fb0a7dff2d01ff5f89957f2001ba6435fac3..cce3db6c681b574debd7b9610297535e8e1e4f93 100644 (file)
@@ -29,3 +29,70 @@ int media_format_get_refcount(media_format_h fmt, int *ref_count)
 
        return MEDIA_FORMAT_ERROR_NONE;
 }
+
+
+int media_format_set_codec_data(media_format_h fmt, void *codec_data, unsigned int size)
+{
+       media_format_s *fmt_handle = (media_format_s *)fmt;
+       void *tmp_data = NULL;
+
+       MEDIA_FORMAT_INSTANCE_CHECK(fmt_handle);
+       MEDIA_FORMAT_NULL_ARG_CHECK(codec_data);
+       MEDIA_FORMAT_NULL_ARG_CHECK(size > 0);
+
+       if (!MEDIA_FORMAT_IS_WRITABLE(fmt)) {
+               LOGE("The format can not be changed");
+               return MEDIA_FORMAT_ERROR_INVALID_OPERATION;
+       }
+
+       tmp_data = malloc(size);
+       if (!tmp_data) {
+               LOGE("alloc[%u] failed", size);
+               return MEDIA_FORMAT_ERROR_OUT_OF_MEMORY;
+       }
+
+       if (fmt_handle->codec_data.data) {
+               LOGW("release current codec data[%p], size[%u]",
+                       fmt_handle->codec_data.data, fmt_handle->codec_data.size);
+               free(fmt_handle->codec_data.data);
+       }
+
+       memcpy(tmp_data, codec_data, size);
+
+       fmt_handle->codec_data.data = tmp_data;
+       fmt_handle->codec_data.size = size;
+
+       LOGI("set new codec data[%p], size[%u]",
+               fmt_handle->codec_data.data, fmt_handle->codec_data.size);
+
+       return MEDIA_FORMAT_ERROR_NONE;
+}
+
+
+int media_format_get_codec_data(media_format_h fmt, void **codec_data, unsigned int *size)
+{
+       int ret = MEDIA_FORMAT_ERROR_NONE;
+       media_format_s *fmt_handle = (media_format_s *)fmt;
+       void *new_data = NULL;
+
+       MEDIA_FORMAT_INSTANCE_CHECK(fmt_handle);
+       MEDIA_FORMAT_NULL_ARG_CHECK(codec_data);
+       MEDIA_FORMAT_NULL_ARG_CHECK(size);
+
+       if (fmt_handle->codec_data.data && fmt_handle->codec_data.size > 0) {
+               new_data = malloc(fmt_handle->codec_data.size);
+               if (!new_data) {
+                       LOGE("alloc[%u] failed", fmt_handle->codec_data.size);
+                       return MEDIA_FORMAT_ERROR_OUT_OF_MEMORY;
+               }
+
+               memcpy(new_data, fmt_handle->codec_data.data, fmt_handle->codec_data.size);
+       }
+
+       *codec_data = new_data;
+       *size = fmt_handle->codec_data.size;
+
+       LOGI("get codec data[%p], size[%u]", *codec_data, *size);
+
+       return MEDIA_FORMAT_ERROR_NONE;
+}
index 7e2be690123be129ac7d37c2d6b8b1d6a1e74056..8a1f8b9251d182a48107b98c4ad803ce4158562f 100644 (file)
@@ -497,13 +497,6 @@ static int __pkt_dealloc_buffer(media_packet_s *handle)
                return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
 
-       /* free codec_data if it is allocated */
-       if (handle->codec_data) {
-               free(handle->codec_data);
-               handle->codec_data = NULL;
-               handle->codec_data_size = 0;
-       }
-
        handle->is_allocated = false;
 
        return MEDIA_PACKET_ERROR_NONE;
@@ -1289,29 +1282,19 @@ int media_packet_get_video_plane_data_ptr(media_packet_h packet, int plane_idx,
 
 int media_packet_get_codec_data(media_packet_h packet, void **codec_data, unsigned int *codec_data_size)
 {
-       media_packet_s *handle;
-       int ret = MEDIA_PACKET_ERROR_NONE;
-
-       MEDIA_PACKET_INSTANCE_CHECK(packet);
-       MEDIA_PACKET_NULL_ARG_CHECK(codec_data);
-       MEDIA_PACKET_NULL_ARG_CHECK(codec_data_size);
-
-       handle = (media_packet_s *)packet;
-
-       LOGI("Get: codec data = %p, codec_data_size = %u", handle->codec_data, handle->codec_data_size);        //LCOV_EXCL_LINE
+       media_packet_s *handle = (media_packet_s *)packet;
 
-       if (handle->codec_data) {
-               *codec_data_size = handle->codec_data_size;
-               *codec_data = handle->codec_data;
-       } else {
-               *codec_data = NULL;
-               *codec_data_size = 0;
+       MEDIA_PACKET_INSTANCE_CHECK(handle);
 
-               LOGE("There is no codec data");
-               ret = MEDIA_PACKET_ERROR_INVALID_OPERATION;
+       switch (media_format_get_codec_data(handle->format, codec_data, codec_data_size)) {
+       case MEDIA_FORMAT_ERROR_NONE:
+               return MEDIA_PACKET_ERROR_NONE;
+       case MEDIA_FORMAT_ERROR_INVALID_PARAMETER:
+               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
+       case MEDIA_FORMAT_ERROR_INVALID_OPERATION: /* fall through */
+       default:
+               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
-
-       return ret;
 }
 
 int media_packet_destroy(media_packet_h packet)
index 364a7830ecfff570af3019b2f6ed5dc8609e0fb6..8f0a8b20b898d0e71f73e42b1b24b314946027f0 100644 (file)
@@ -35,16 +35,15 @@ int media_packet_set_codec_data(media_packet_h packet, void *codec_data, unsigne
 
        LOGI("Set: codec data = %p, codec_data_size = %u\n", codec_data, codec_data_size);
 
-       handle->codec_data = (void *)malloc(codec_data_size);
-       if (handle->codec_data == NULL) {
-               LOGE("MEDIA_PACKET_ERROR_OUT_OF_MEMORY(0x%08x)", MEDIA_PACKET_ERROR_OUT_OF_MEMORY);
-               return MEDIA_PACKET_ERROR_OUT_OF_MEMORY;
+       switch (media_format_set_codec_data(handle->format, codec_data, codec_data_size)) {
+       case MEDIA_FORMAT_ERROR_NONE:
+               return MEDIA_PACKET_ERROR_NONE;
+       case MEDIA_FORMAT_ERROR_INVALID_PARAMETER:
+               return MEDIA_PACKET_ERROR_INVALID_PARAMETER;
+       case MEDIA_FORMAT_ERROR_INVALID_OPERATION:  /* fall through */
+       default:
+               return MEDIA_PACKET_ERROR_INVALID_OPERATION;
        }
-
-       memcpy(handle->codec_data, codec_data, codec_data_size);
-       handle->codec_data_size = codec_data_size;
-
-       return MEDIA_PACKET_ERROR_NONE;
 }
 
 int media_packet_reset_flags(media_packet_h packet)
index cfca7deb321fc1cd8fd6fbc5abfc97d3a98c5bf1..a1d3497bd34fa57c460bb97cc3206fd63b115f60 100644 (file)
@@ -666,7 +666,7 @@ static void _media_packet_get_codec_data()
                g_print("media_packet_get_codec_data is sucess ... !\n");
                g_print("codec_data_size = %u\n", get_codec_data_size);
 
-               if (get_codec_data_size == 0)
+               if (!get_codec_data || get_codec_data_size == 0)
                        return;
 
                int i;
@@ -674,6 +674,8 @@ static void _media_packet_get_codec_data()
                        g_print("codec_data[%d] ", i);
                        g_print(" = 0x%x\n", get_codec_data[i]);
                }
+
+               free(get_codec_data);
        } else {
                g_print("media_packet_get_codec_data is failed...\n");
        }