Help Center/
Atlas 300 Application (Model 3000)/
Matrix API Reference/
Auxiliary APIs/
Data Type Serialization and Deserialization (C++ Language)/
API Usage Example
Updated on 2022-03-13 GMT+08:00
API Usage Example
/*************************************************************************************** * CopyRight (C) HiSilicon Co., Ltd. * * Filename: user_def_datatype.h * Description: User defined data type * * Version: 1.0 * Created: 2018-01-08 10:15:18 * Author: * * Revision: initial draft; **************************************************************************************/ #ifndef USE_DEF_DATA_TYPE_H_ #define USE_DEF_DATA_TYPE_H_ #ifdef __cplusplus #include <vector> #include <map> #endif // User-defined data type (C language) typedef struct UseDefDataType { int data1; float data2; }UseDefDataTypeT; #ifdef __cplusplus // User-defined data type (with template) template<typename T1, typename T2, typename T3> class UseDefTemplateDataType { public: std::vector<T1> data_vec_; std::map<T2, T3> data_map_; }; #endif #endif
/*************************************************************************************** * CopyRight (C) HiSilicon Co., Ltd. * * Filename: use_def_data_type_reg.cpp * Description: User defined Data Type Register * * Version: 1.0 * Created: 2018-01-08 10:15:18 * Author: h00384043 * * Revision: initial draft; **************************************************************************************/ #include "use_def_data_type.h" #include "hiaiengine/data_type_reg.h" // The user-defined data type must be registered with the Matrix before being used as the communication interface between engines. template<class Archive> void serialize(Archive& ar, UseDefDataTypeT& data) { ar(data.data1, data.data2); } // Register UseDefDataTypeT. HIAI_REGISTER_DATA_TYPE("UseDefDataTypeT", UseDefDataTypeT) // Register the data of the template type. // All the required template types must be registered. template<class Archive, typename T1, typename T2, typename T3> void serialize(Archive& ar, UseDefTemplateDataType<T1, T2, T3>& data) { ar(data.data_vec_, data.data_map_); } // Register UseDefTemplateDataType<int, float>. HIAI_REGISTER_TEMPLATE_DATA_TYPE("UseDefTemplateDataType_uint64_t_float_string", UseDefTemplateDataType, uint64_t, float, std::string); //... Register other types. // Register UseDefTemplateDataType<int, int>. HIAI_REGISTER_TEMPLATE_DATA_TYPE("UseDefTemplateDataType_uint64_t_uint64_t_uint64_t", UseDefTemplateDataType, uint64_t, uint64_t, uint64_t);
// Serialization and deserialization of registered structs typedef struct tagST_SC_IMAGE { INT32 iWidth; // image width INT32 iHeight; // image height INT32 iWidthStep; // Size of aligned image row in bytes INT32 iChannel; // channels INT32 iDepth; // depth in bits UINT32 uiTimeStampH; UINT32 uiTimeStampL; UINT32 iSize; UINT64 uiID; EN_SC_IMAGE_TYPE eType; // image type ST_SC_IMAGE_ROI *roi; // Image ROI. If NULL, the whole image is selected UINT8* pucImageData; // Image data UINT64 uiPhyAddr; }ST_SC_IMAGE; std::shared_ptr<ST_SC_IMAGE> st_image = std::make_shared<ST_SC_IMAGE>(); st_image->iWidth = 1080; st_image->iHeight = 1080; st_image->iChannel = 1; st_image->iWidthStep = 1080; st_image->iSize = 1080*1080*3/2 ; st_image->eType = SC_IMAGE_YUV420SP; st_image->roi = (ST_SC_IMAGE_ROI*)malloc(sizeof(ST_SC_IMAGE_ROI)); st_image->pucImageData = nullptr; uint8_t* align_buffer = nullptr; HIAI_StatusT get_ret = HIAIMemory::HIAI_DMalloc(st_image->iSize,(void*&)align_buffer,10000); hiai::EnginePortID engine_id; engine_id.graph_id = 1; engine_id.engine_id = 1; engine_id.port_id = 0; HIAI_SendData(engine_id, "ST_SC_IMAGE", std::static_pointer_cast<void>(st_image),100000); void GetStScImageSearPtr(void* inputPtr, std::string& ctrlStr, uint8_t*& dataPtr, uint32_t& dataLen) { // If the struct body contains multiple pointers in addition to image, the pointers are concateneated. ST_SC_IMAGE* st_image = (ST_SC_IMAGE*)inputPtr; ctrlStr = std::string((char*)inputPtr, sizeof(ST_SC_IMAGE)); if(nullptr != st_image->roi) { std::string image_roi_str = std::string((char*)st_image->roi, sizeof(ST_SC_IMAGE_ROI)); ctrlStr += image_roi_str; } dataPtr = (UINT8*)st_image->pucImageData; dataLen= st_image->iSize; } std::shared_ptr<void> GetStScImageDearPtr(const char* ctrlPtr, const uint32_t& ctrlLen, const uint8_t* dataPtr, const uint32_t& dataLen) { ST_SC_IMAGE* st_sc_image = (ST_SC_IMAGE*)ctrlPtr; std::shared_ptr<hiai::BatchImagePara<uint8_t>> ImageInfo(new hiai::BatchImagePara<uint8_t>); hiai::ImageData<uint8_t> image_data; image_data.width = st_sc_image->iWidth; image_data.height = st_sc_image->iHeight; image_data.channel = st_sc_image->iChannel; image_data.width_step = st_sc_image->iWidthStep; if (st_sc_image->eType == SC_IMAGE_U8C3PLANAR) { image_data.size = st_sc_image->iWidth * st_sc_image->iHeight * 3; image_data.format = hiai::BGR888; } else if (SC_IMAGE_U8C3PACKAGE == st_sc_image->eType) { image_data.size = st_sc_image->iWidth * st_sc_image->iHeight * 3; image_data.format = hiai::BGR888; } else if (st_sc_image->eType == SC_IMAGE_YUV420SP) { image_data.size = st_sc_image->iSize;//st_sc_image->iWidth * st_sc_image->iHeight * 3 / 2; image_data.format = hiai::YUV420SP; } image_data.data.reset(dataPtr, hiai::Graph::ReleaseDataBuffer); ImageInfo->v_img.push_back(image_data); ImageInfo->b_info.frame_ID.push_back(0); ImageInfo->b_info.batch_size = ImageInfo->b_info.frame_ID.size(); return std::static_pointer_cast<void>(ImageInfo); } HIAI_REGISTER_SERIALIZE_FUNC("ST_SC_IMAGE", ST_SC_IMAGE, GetStScImageSearPtr, GetStScImageDearPtr);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot