Add getWidthStride 68/290968/1
authorKwanghoon Son <k.son@samsung.com>
Thu, 6 Apr 2023 00:14:13 +0000 (09:14 +0900)
committerKwanghoon Son <k.son@samsung.com>
Thu, 6 Apr 2023 00:14:13 +0000 (09:14 +0900)
add function and convert memcpy method in order for mv3d to process images with stride

Change-Id: Ieb8c6082fa83327ee0a318462c8d8e0eef274085
Based-on: d9cfd4dbe6bcef7f5585bb26cd67a75e559671a4
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_3d/3d/src/Mv3d.cpp
mv_common/include/MediaSource.h
mv_common/src/MediaSource.cpp

index fbf6b3f5696e17686a741675b0be1001109c541d..6a029c6f1a629c62331019c4484a82e3e05a36a0 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "mv_private.h"
 #include <mv_common.h>
+#include <MediaSource.h>
 #include <memory>
 #include <unistd.h>
 #include <opencv2/core.hpp>
@@ -144,7 +145,7 @@ namespace mv3d
                                LOGE("Failed to create dfs adaptation : %s", e.what());
                                return MEDIA_VISION_ERROR_OUT_OF_MEMORY;
                        } catch (const std::runtime_error &e) {
-                               LOGE("Failed to bind %s adpator", e.what());
+                               LOGE("Failed to bind %s adaptor", e.what());
                                return MEDIA_VISION_ERROR_INVALID_OPERATION;
                        }
                }
@@ -184,7 +185,7 @@ namespace mv3d
                }
 
                if (!mDfsAdaptor) {
-                       LOGE("Invalid Opertation. Do Configure first.");
+                       LOGE("Invalid Operation. Do Configure first.");
                        return MEDIA_VISION_ERROR_INVALID_OPERATION;
                }
 
@@ -199,7 +200,11 @@ namespace mv3d
                return MEDIA_VISION_ERROR_NONE;
        }
 
-
+/*
+* Get buffer data from mv_source.
+* For now it only support no-stride image, or stride with nv12 format
+* TODO: zero-copy, multi-planar image handle
+*/
        void Mv3d::GetBufferFromSource(mv_source_h source,
                                                                unsigned char*& buffer,
                                                                unsigned int& width,
@@ -207,36 +212,36 @@ namespace mv3d
                                                                int& type,
                                                                size_t& stride)
        {
-               unsigned char* _buffer = nullptr;
-               unsigned int _bufferSize = 0;
-               unsigned int _width = 0;
-               unsigned int _height = 0;
-               mv_colorspace_e _colorSpace = MEDIA_VISION_COLORSPACE_INVALID;
-
-               int ret = mv_source_get_buffer(source, &_buffer, &_bufferSize);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw std::runtime_error("invalid buffer pointer");
-
-               ret = mv_source_get_width(source, &_width);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw std::runtime_error("invalid width");
-
-               ret = mv_source_get_height(source, &_height);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw std::runtime_error("invalid height");
-
-               ret = mv_source_get_colorspace(source, &_colorSpace);
-               if (ret != MEDIA_VISION_ERROR_NONE)
+               MediaVision::Common::MediaSource *mediaSource = static_cast<MediaVision::Common::MediaSource *>(source);
+               auto *_buffer = mediaSource->getBuffer();
+               auto _bufferSize = mediaSource->getBufferSize();
+               width = mediaSource->getWidth();
+               height = mediaSource->getHeight();
+               stride = mediaSource->getWidthStride();
+               switch (mediaSource->getColorspace()) {
+               case MEDIA_VISION_COLORSPACE_RGB888:
+                       type = DFS_DATA_TYPE_UINT8C3;
+                       break;
+               case MEDIA_VISION_COLORSPACE_I420:
+               case MEDIA_VISION_COLORSPACE_NV12:
+               case MEDIA_VISION_COLORSPACE_NV21:
+                       type = DFS_DATA_TYPE_UINT8C1;
+                       break;
+               default: //NOT tested
                        throw std::runtime_error("invalid color space");
+               }
 
-               buffer = new unsigned char [_bufferSize];
-               memcpy(buffer, _buffer, _bufferSize);
-               width = _width;
-               height = _height;
-               type = _colorSpace == MEDIA_VISION_COLORSPACE_RGB888 ?
-                                                                        DFS_DATA_TYPE_UINT8C3 :
-                                                                        DFS_DATA_TYPE_UINT8C1;
-               stride = _bufferSize / _height;
+               if (width != stride && type == DFS_DATA_TYPE_UINT8C1) {
+                       //copy only Y channel
+                       _bufferSize = width * height;
+                       buffer = new unsigned char[_bufferSize];
+                       for (unsigned int i = 0; i < height; i++) {
+                               memcpy(buffer + i * width, _buffer + i * stride, width);
+                       }
+               } else {
+                       buffer = new unsigned char[_bufferSize];
+                       memcpy(buffer, _buffer, _bufferSize);
+               }
        }
 
        void Mv3d::GetDfsDataFromSources(mv_source_h baseSource,
index 7998e9f7722844736fe08642171edff9a4ffc443..b802d8f9af6fbf3d9f55ef72a6567ba5f7e3bd54 100644 (file)
@@ -171,6 +171,8 @@ public:
 
        void setFormat(unsigned int width, unsigned int height, mv_colorspace_e colorspace, media_packet_h media_packet);
 
+       unsigned int getWidthStride();
+
 private:
        std::vector<Plane> _plane;
        unsigned int _width { 0 }; /**< The image width */
index c2dab39afc67ad7c295fc9b5cf0d96f9ecfea27c..b902cd847ea2e9e0ae0005cfef7cc5cea164f66e 100644 (file)
@@ -274,5 +274,12 @@ void MediaSource::setFormat(unsigned int width, unsigned int height, mv_colorspa
        }
 }
 
+unsigned int MediaSource::getWidthStride()
+{
+       if (_plane.empty())
+               return _width;
+       return _plane[0].bytePerLine;
+}
+
 } /* Common */
 } /* MediaVision */