更新时间:2021-03-18 GMT+08:00
分享

接口使用示例

/***************************************************************************************
 *                      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

// 用户自定义的数据类型 (C 语言风格)
typedef struct UseDefDataType
{
    int data1;
    float data2;
}UseDefDataTypeT;

#ifdef __cplusplus
// 用户自定义的数据类型 (带模板类型) 
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"

// 用户自定义的数据类型必须先注册到Matrix,才能作为Engine之间的通信接口正常使用
template<class Archive>
void serialize(Archive& ar, UseDefDataTypeT& data)
{
    ar(data.data1, data.data2);
}

// 注册UseDefDataTypeT
HIAI_REGISTER_DATA_TYPE("UseDefDataTypeT", UseDefDataTypeT)


// 模板类型数据的注册
// 对于模板类型,对于所有需要使用的类型必须都注册
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_);
}

// 注册 UseDefTemplateDataType<int, float>
HIAI_REGISTER_TEMPLATE_DATA_TYPE("UseDefTemplateDataType_uint64_t_float_string", UseDefTemplateDataType, uint64_t, float, std::string);

//... 注册其他类型

// 注册 UseDefTemplateDataType<int, int>
HIAI_REGISTER_TEMPLATE_DATA_TYPE("UseDefTemplateDataType_uint64_t_uint64_t_uint64_t", UseDefTemplateDataType, uint64_t, uint64_t, uint64_t);
// 注册结构体的序列化和反序列化
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)

{
    // 如果结构体内有除了image之外的多个指针, 则进行拼接
    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);
分享:

    相关文档

    相关产品