Add shallow copy for media source 52/290952/1
authorKwanghoon Son <k.son@samsung.com>
Wed, 5 Apr 2023 10:52:12 +0000 (19:52 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 5 Apr 2023 10:52:45 +0000 (19:52 +0900)
- Has media_packet_h for ref count
- zero copy for mv_source_fill_by_media_packet_c

rebase from commit (86c8f1754b53bfd08db519b0a6335aca3126e9ef)

Change-Id: Id443acfc5cfc49c945c3e81d81a54dad63582536
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_common/include/MediaSource.h
mv_common/src/MediaSource.cpp
mv_common/src/mv_common_c.cpp
test/testsuites/common/test_pkt.cpp

index a3455763a3bfd08eadd5afc6eadc9df7d3354912..7998e9f7722844736fe08642171edff9a4ffc443 100644 (file)
@@ -167,12 +167,16 @@ public:
         */
        mv_colorspace_e getColorspace(void) const;
 
+       void addPlane(Plane &plane);
+
+       void setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace, media_packet_h media_packet);
+
 private:
        std::vector<Plane> _plane;
        unsigned int _width { 0 }; /**< The image width */
        unsigned int _height { 0 }; /**< The image height */
        mv_colorspace_e _colorspace { MEDIA_VISION_COLORSPACE_INVALID }; /**< The image colorspace */
-       bool _isRef { false };
+       media_packet_h _packet { nullptr };
 #ifdef WITH_DA_PROFILE
        da_timestamp_s _stamp {};
 #endif
index 78ca9dd829b5c8ddd74ed21ebb66a014b8d839be..c2dab39afc67ad7c295fc9b5cf0d96f9ecfea27c 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <cstring>
 #include <new>
+#include <stdexcept>
 
 namespace MediaVision
 {
@@ -71,7 +72,9 @@ bool MediaSource::alloc(unsigned int bufferSize, unsigned int width, unsigned in
 
 void MediaSource::clear(void)
 {
-       if (!_isRef) {
+       if (_packet) {
+               media_packet_unref(_packet);
+       } else {
                for (const auto &p : _plane) {
                        delete[] p.buffer;
                }
@@ -80,7 +83,7 @@ void MediaSource::clear(void)
        _width = 0;
        _height = 0;
        _colorspace = MEDIA_VISION_COLORSPACE_INVALID;
-       _isRef = false;
+       _packet = nullptr;
 }
 
 bool MediaSource::fill(const unsigned char *buffer, unsigned int bufferSize, unsigned int width, unsigned int height,
@@ -253,5 +256,23 @@ mv_colorspace_e MediaSource::getColorspace(void) const
        return _colorspace;
 }
 
+void MediaSource::addPlane(Plane &plane)
+{
+       _plane.push_back(plane);
+}
+
+void MediaSource::setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace,
+                                                       media_packet_h media_packet)
+{
+       _width = width;
+       _height = height;
+       _colorspace = colorspace;
+       _packet = media_packet;
+       if (media_packet_ref(media_packet) != MEDIA_PACKET_ERROR_NONE) {
+               LOGE("media_packet_ref failed");
+               throw std::runtime_error("media_packet_ref failed");
+       }
+}
+
 } /* Common */
 } /* MediaVision */
index 8ba24aeae0814618e8ccca520000d5a16edcd903..3d6a2fd549598006e21dfeea7e28c2be55fd278a 100644 (file)
@@ -74,20 +74,9 @@ int mv_source_fill_by_media_packet_c(mv_source_h source, media_packet_h media_pa
        int image_width = 0;
        int image_height = 0;
 
-       int plane_width[4] = {
-               0,
-       };
-       int plane_height[4] = {
-               0,
-       };
-       uint64_t plane_size[4] = {
-               0,
-       };
-       size_t offset = 0;
        media_format_h format = NULL;
        media_format_mimetype_e mimetype = MEDIA_FORMAT_I420;
        unsigned char *data_buffer = NULL;
-       uint64_t buffer_size = 0;
        mv_colorspace_e image_colorspace = MEDIA_VISION_COLORSPACE_INVALID;
 
        int ret = media_packet_is_video(media_packet, &is_video);
@@ -165,53 +154,41 @@ int mv_source_fill_by_media_packet_c(mv_source_h source, media_packet_h media_pa
        }
 
        ret = media_packet_get_number_of_video_planes(media_packet, &plane_num);
-       if (plane_num <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+       if (plane_num <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
                LOGE("invalid plane_num [%d] is returned", plane_num);
                return MEDIA_VISION_ERROR_INVALID_PARAMETER;
        }
        LOGI("%d planes with color_space [%d]", plane_num, image_colorspace);
 
+       MediaVision::Common::MediaSource *mediaSource = static_cast<MediaVision::Common::MediaSource *>(source);
+       try {
+               mediaSource->setFormat((unsigned int) (image_width), (unsigned int) (image_height), image_colorspace,
+                                                          media_packet);
+       } catch (const std::exception &e) {
+               return MEDIA_VISION_ERROR_INTERNAL;
+       }
+
        for (ind = 0; ind < plane_num; ++ind) {
                ret = media_packet_get_video_stride_width(media_packet, ind, &image_width);
-               if (image_width <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+               if (image_width <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
                        LOGE("invalid plane width [%d]", image_width);
                        return MEDIA_VISION_ERROR_INVALID_PARAMETER;
                }
 
                ret = media_packet_get_video_stride_height(media_packet, ind, &image_height);
-               if (image_height <= 0 || ret != MEDIA_VISION_ERROR_NONE) {
+               if (image_height <= 0 || ret != MEDIA_PACKET_ERROR_NONE) {
                        LOGE("invalid plane width [%d]", image_height);
                        return MEDIA_VISION_ERROR_INVALID_PARAMETER;
                }
 
-               plane_width[ind] = image_width;
-               plane_height[ind] = image_height;
-               plane_size[ind] = static_cast<uint64_t>(image_width) * static_cast<uint64_t>(image_height);
-               buffer_size += plane_size[ind];
-       }
-
-       if (!(static_cast<MediaVision::Common::MediaSource *>(source))
-                                ->alloc(buffer_size, static_cast<unsigned int>(plane_width[0]),
-                                                static_cast<unsigned int>(plane_height[0]), image_colorspace)) {
-               LOGE("mv_source_h alloc from media_packet_h failed");
-               return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
-       }
-
-       for (ind = 0; ind < plane_num; ++ind) {
                ret = media_packet_get_video_plane_data_ptr(media_packet, ind, (void **) &data_buffer);
                if (ret != MEDIA_PACKET_ERROR_NONE) {
                        LOGE("media_packet_get_video_plane_data_ptr() plane[%d] failed, mv_source_h fill skipped", ind);
                        return MEDIA_VISION_ERROR_INVALID_PARAMETER;
                }
-
-               offset = (ind == 0) ? 0 : plane_size[ind - 1] * sizeof(char);
-               if (!(static_cast<MediaVision::Common::MediaSource *>(source))
-                                        ->fill(data_buffer, plane_size[ind], static_cast<unsigned int>(plane_width[ind]),
-                                                       static_cast<unsigned int>(plane_height[ind]), offset)) {
-                       LOGE("mv_source_h filling from media_packet_h failed");
-                       return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
-               }
-               data_buffer = NULL;
+               MediaVision::Common::Plane plane { data_buffer, (unsigned int) (image_width * image_height),
+                                                                                  (unsigned int) (image_width) };
+               mediaSource->addPlane(plane);
        }
 
        LOGD("Media source has been filled from media packet");
index 7a4a231f764d567a8c86a9348a5264e93da6b030..146d2f82d53a38248b3163bd3f7c4237b06d2cd3 100644 (file)
@@ -40,7 +40,7 @@ TEST(MediaPKT, PacketToSource)
        mv_source_h mv_source;
        ASSERT_EQ(mv_create_source(&mv_source), MEDIA_VISION_ERROR_NONE);
        ASSERT_EQ(mv_source_fill_by_media_packet(mv_source, media_pkt), MEDIA_VISION_ERROR_INVALID_PARAMETER);
-       ASSERT_EQ(mv_destroy_source(mv_source), MEDIA_VISION_ERROR_NONE);
        ASSERT_EQ(media_packet_unref(media_pkt), MEDIA_PACKET_ERROR_NONE);
        ASSERT_EQ(media_format_unref(media_fmt), MEDIA_FORMAT_ERROR_NONE);
+       ASSERT_EQ(mv_destroy_source(mv_source), MEDIA_VISION_ERROR_NONE);
 }
\ No newline at end of file