From 54f4a423431206237206bfdf92bcd4a4a00f00ed Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Thu, 4 Jan 2024 19:23:19 +0800 Subject: [PATCH] 添加resize到1920,1080设置 --- src/ai_platform/MultiSourceProcess.cpp | 57 +++++++++++++++++++++++++++++---------------------------- src/decoder/dvpp/DvppDataMemory.hpp | 11 +++++++---- src/decoder/dvpp/DvppDecoder.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- src/decoder/dvpp/DvppDecoder.h | 10 ++++++++++ src/decoder/dvpp/DvppDecoderApi.cpp | 7 +++++++ src/decoder/dvpp/DvppDecoderApi.h | 1 + src/decoder/dvpp/VpcPicConverter.cpp | 152 -------------------------------------------------------------------------------------------------------------------------------------------------------- src/decoder/dvpp/VpcPicConverter.h | 21 --------------------- src/decoder/dvpp/VpcUtils.cpp | 213 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/decoder/dvpp/VpcUtils.h | 23 +++++++++++++++++++++++ src/decoder/dvpp/dvpp_headers.h | 2 +- src/decoder/interface/AbstractDecoder.h | 3 ++- src/decoder/interface/DecoderManager.cpp | 21 +++++++++++++++++++++ src/decoder/interface/DecoderManager.h | 2 ++ src/decoder/interface/DeviceMemory.hpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/decoder/interface/interface_headers.h | 1 + src/demo/demo.cpp | 44 ++++++++++++++++++++++++++------------------ 17 files changed, 464 insertions(+), 233 deletions(-) delete mode 100644 src/decoder/dvpp/VpcPicConverter.cpp delete mode 100644 src/decoder/dvpp/VpcPicConverter.h create mode 100644 src/decoder/dvpp/VpcUtils.cpp create mode 100644 src/decoder/dvpp/VpcUtils.h diff --git a/src/ai_platform/MultiSourceProcess.cpp b/src/ai_platform/MultiSourceProcess.cpp index 2229f33..bc7c94c 100755 --- a/src/ai_platform/MultiSourceProcess.cpp +++ b/src/ai_platform/MultiSourceProcess.cpp @@ -84,17 +84,17 @@ CMultiSourceProcess::~CMultiSourceProcess(){ } int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ -#ifdef USE_VILLAGE - if (!CheckTime()) { //时间限制 - std::cout << "sy_licence_check failed." << std::endl; - return AUTHOR_ERROR; - } -#else - if (!CheckLabel(vptParam.gpuid)) { //机器授权 - std::cout << "sy_licence_check failed." << std::endl; - return AUTHOR_ERROR; - } -#endif +// #ifdef USE_VILLAGE +// if (!CheckTime()) { //时间限制 +// std::cout << "sy_licence_check failed." << std::endl; +// return AUTHOR_ERROR; +// } +// #else +// if (!CheckLabel(vptParam.gpuid)) { //机器授权 +// std::cout << "sy_licence_check failed." << std::endl; +// return AUTHOR_ERROR; +// } +// #endif set_default_logger(LogLevel(vptParam.log_level), "multi_source_process", vptParam.log_path, vptParam.log_mem, vptParam.log_mem); LOG_INFO("编译时间:{} {}", __DATE__, __TIME__); @@ -324,7 +324,8 @@ bool CMultiSourceProcess::AddTask(task_param _cur_task_param){ config.cfg.force_tcp = true; // rtsp用tcp config.cfg.gpuid = to_string(m_devId); config.cfg.skip_frame = skip_frame_; - + config.cfg.resize = true; + if (1 == _cur_task_param.dec_type){ config.cfg.port = _cur_task_param.port; config.dec_type = DECODER_TYPE_GB28181; @@ -360,7 +361,7 @@ bool CMultiSourceProcess::AddTask(task_param _cur_task_param){ int input_image_width = 0; int input_image_height = 0; - pDecManager->getResolution(config.name, input_image_width, input_image_height); + pDecManager->getOutResolution(config.name, input_image_width, input_image_height); LOG_INFO("task_id: {} width: {} height:{}", task_id, input_image_width, input_image_height); // 所有参数都准备好之后再启动解码 @@ -761,21 +762,21 @@ int CMultiSourceProcess::algorthim_process_thread(){ ACL_CALL(aclrtCreateContext(&ctx, m_devId), ACL_SUCCESS, 1); while (true){ -#ifdef USE_VILLAGE - if (!CheckTime()) { //时间限制 - LOG_FATAL("authority failed!"); - break; - } -#else - if (check_label == -1) { //机器授权 - LOG_FATAL("authority failed!"); - break; - } - if (!UpdateLabel()) { - LOG_FATAL("authority failed!"); - break; - } -#endif +// #ifdef USE_VILLAGE +// if (!CheckTime()) { //时间限制 +// LOG_FATAL("authority failed!"); +// break; +// } +// #else +// if (check_label == -1) { //机器授权 +// LOG_FATAL("authority failed!"); +// break; +// } +// if (!UpdateLabel()) { +// LOG_FATAL("authority failed!"); +// break; +// } +// #endif if(m_bfinish){ break; } diff --git a/src/decoder/dvpp/DvppDataMemory.hpp b/src/decoder/dvpp/DvppDataMemory.hpp index 257fd9e..5542e71 100644 --- a/src/decoder/dvpp/DvppDataMemory.hpp +++ b/src/decoder/dvpp/DvppDataMemory.hpp @@ -1,3 +1,6 @@ +#ifndef __DVPP_DATA_MEMORY_HPP__ +#define __DVPP_DATA_MEMORY_HPP__ + #include #include "depend_headers.h" @@ -66,11 +69,11 @@ public: } pHwRgb = nullptr; } - - - } public: int data_type; // 0: rgb , 1: NV12 -}; \ No newline at end of file +}; + + +#endif // __DVPP_DATA_MEMORY_HPP__ \ No newline at end of file diff --git a/src/decoder/dvpp/DvppDecoder.cpp b/src/decoder/dvpp/DvppDecoder.cpp index 8631819..55f1255 100644 --- a/src/decoder/dvpp/DvppDecoder.cpp +++ b/src/decoder/dvpp/DvppDecoder.cpp @@ -184,6 +184,9 @@ AVCodecContext* DvppDecoder::init_FFmpeg(FFDecConfig config){ frame_height = codecpar->height; pix_fmt = (AVPixelFormat)codecpar->format; + m_bResize = config.resize; + calcOutResolution(frame_width, frame_height); + if (stream->avg_frame_rate.den) { m_fps = av_q2d(stream ->avg_frame_rate); } else { @@ -197,7 +200,7 @@ AVCodecContext* DvppDecoder::init_FFmpeg(FFDecConfig config){ } #endif - LOG_INFO("[{}]- init ffmpeg success! input:{} frame_width:{} frame_height:{} fps:{} ", m_dec_name, input_file, frame_width, frame_height, m_fps); + LOG_INFO("[{}]- init ffmpeg success! input:{} src:({}, {}) out:({}, {}) fps:{} ", m_dec_name, input_file, frame_width, frame_height, out_frame_width, out_frame_height, m_fps); return avctx; }while(0); @@ -209,6 +212,33 @@ AVCodecContext* DvppDecoder::init_FFmpeg(FFDecConfig config){ return nullptr; } +void DvppDecoder::calcOutResolution(int width, int height) { + if(m_bResize) { + float srcRatio = width / (float)height; + float stdRatio = 1920.0 / 1080.0f ; + int outWidth = 1920; + int outHeight = 1080; + if (srcRatio > stdRatio) { + outHeight = static_cast(outWidth * (float)height / width) ; + if (outHeight % 2 == 1) { + outHeight += 1; + } + } else if (srcRatio < stdRatio) { + outWidth = static_cast(outHeight * (float)width / height) ; + if (outWidth % 2 == 1) { + outWidth += 1; + } + } + + out_frame_width = outWidth; + out_frame_height = outHeight; + } else { + out_frame_width = width; + out_frame_height = height; + } + +} + int DvppDecoder::getVdecType(int videoType, int profile) { int streamFormat = H264_MAIN_LEVEL; @@ -277,6 +307,9 @@ int DvppDecoder::getVdecType(int videoType, int profile) LOG_ERROR("[{}]-该设备channel已经用完了!", m_dec_name); return false; } + + m_vpcUtils.init(m_dvpp_deviceId); + m_vdec_out_size = frame_width * frame_height * 3 / 2; LOG_INFO("[{}]- init vdpp success! device:{} channel:{}", m_dec_name, m_dvpp_deviceId, m_dvpp_channel); @@ -353,6 +386,12 @@ bool DvppDecoder::getResolution(int &width, int &height){ return true; } +bool DvppDecoder::getOutResolution( int &width, int &height ) { + width = out_frame_width; + height = out_frame_height; + return true; +} + float DvppDecoder::fps(){ return m_fps; } @@ -532,11 +571,6 @@ void DvppDecoder::read_thread() { #endif nSended = sendPkt(vdecChannelDesc, pkt, frame_nb); - if(!m_bReal && frame_nb > 20 && m_avg_decode_time > 0) - { - // 针对文件,根据解码时间做延时,避免占用过多显存 - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - } } if(nSended < 0) { @@ -767,12 +801,43 @@ void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *o // last_ts = get_cur_time_ms(); // 换成解码后数据, 这里这样做的是为了保证解码一直持续进行,避免后续操作阻碍文件读取和解码从而导致花屏 - DvppDataMemory* mem = new DvppDataMemory(width, width_stride, height, height_stride, outputSize, m_dec_name, to_string(m_dvpp_deviceId), false, frame_nb, (unsigned char *)outputDataDev); + DvppDataMemory* mem = nullptr; + if (m_bResize && (width > 1920 || height > 1080)) { + float srcRatio = width / (float)height; + float stdRatio = 1920.0 / 1080.0f ; + int outWidth = 1920; + int outHeight = 1080; + if (srcRatio > stdRatio) { + outHeight = static_cast(outWidth * (float)height / width) ; + if (outHeight % 2 == 1) + { + outHeight += 1; + } + } else if (srcRatio < stdRatio) { + outWidth = static_cast(outHeight * (float)width / height) ; + if (outWidth % 2 == 1) + { + outWidth += 1; + } + } + + mem = m_vpcUtils.resize(output, outWidth, outHeight); + if (mem) { + acldvppFree(outputDataDev); + outputDataDev = nullptr; + + mem->setDeviceId(to_string(m_dvpp_deviceId)); + mem->setId(m_dec_name); + mem->setFrameNb(frame_nb); + } + } else { + mem = new DvppDataMemory(width, width_stride, height, height_stride, outputSize, m_dec_name, to_string(m_dvpp_deviceId), false, frame_nb, (unsigned char *)outputDataDev); + } + if(mem){ m_decoded_data_queue.push(mem); bCached = true; } - } if(!bCached) { diff --git a/src/decoder/dvpp/DvppDecoder.h b/src/decoder/dvpp/DvppDecoder.h index c05eea3..50b3ccc 100644 --- a/src/decoder/dvpp/DvppDecoder.h +++ b/src/decoder/dvpp/DvppDecoder.h @@ -12,6 +12,8 @@ #include "FFRecoderTaskManager.h" +#include "VpcUtils.h" + using namespace std; typedef void(*RECEIVER_FINISHED_CALLBACK)(const void* userPtr); @@ -35,6 +37,7 @@ public: bool isFinished(); bool isPausing(); bool getResolution( int &width, int &height ); + bool getOutResolution( int &width, int &height ); bool isSurport(FFDecConfig& cfg); @@ -77,6 +80,8 @@ private: int getVdecType(int videoType, int profile); + void calcOutResolution(int w, int h); + private: FFDecConfig m_cfg; string m_dec_name; @@ -100,12 +105,15 @@ private: int frame_width{0}; int frame_height{0}; + int out_frame_width{0}; + int out_frame_height{0}; bool m_bReal {false}; // 是否实时流 float m_fps{0.0}; std::thread* m_read_thread{nullptr}; bool m_dec_keyframe {false}; + bool m_bResize {false}; // 解码 int m_dvpp_deviceId {-1}; @@ -129,4 +137,6 @@ private: uint64_t m_out_count {0}; std::atomic m_DvppCacheCounter{0}; + + VpcUtils m_vpcUtils; }; \ No newline at end of file diff --git a/src/decoder/dvpp/DvppDecoderApi.cpp b/src/decoder/dvpp/DvppDecoderApi.cpp index f9b5ff5..a2e3d81 100644 --- a/src/decoder/dvpp/DvppDecoderApi.cpp +++ b/src/decoder/dvpp/DvppDecoderApi.cpp @@ -79,6 +79,13 @@ bool DvppDecoderApi::getResolution(int &width, int &height){ return false; } +bool DvppDecoderApi::getOutResolution(int &width, int &height){ + if(m_pDecoder != nullptr){ + return m_pDecoder->getOutResolution(width, height); + } + return false; +} + bool DvppDecoderApi::isSurport(FFDecConfig& cfg){ if(m_pDecoder != nullptr){ return m_pDecoder->isSurport(cfg); diff --git a/src/decoder/dvpp/DvppDecoderApi.h b/src/decoder/dvpp/DvppDecoderApi.h index 496d394..445ddba 100644 --- a/src/decoder/dvpp/DvppDecoderApi.h +++ b/src/decoder/dvpp/DvppDecoderApi.h @@ -24,6 +24,7 @@ public: bool isFinished(); bool isPausing(); bool getResolution( int &width, int &height ); + bool getOutResolution( int &width, int &height ); bool isSurport(FFDecConfig& cfg); diff --git a/src/decoder/dvpp/VpcPicConverter.cpp b/src/decoder/dvpp/VpcPicConverter.cpp deleted file mode 100644 index 9913e65..0000000 --- a/src/decoder/dvpp/VpcPicConverter.cpp +++ /dev/null @@ -1,152 +0,0 @@ -#include "VpcPicConverter.h" -#include "depend_headers.h" - -#define ALIGN_UP(val, align) (((val) % (align) == 0) ? (val) : (((val) / (align) + 1) * (align))) - -#define CHECK_AND_RETURN(ret, message) \ - if(ret != 0) {LOG_ERROR("{}", message); return ret;} -#define CHECK_NOT_RETURN(ret, message) \ - if(ret != 0) {LOG_ERROR("{}", message);} -#define CHECK_AND_RETURN_NOVALUE(ret, message) \ - if(ret != 0) {LOG_ERROR("{}", message); return;} -#define CHECK_AND_BREAK(ret, message) \ - if(ret != 0) {LOG_ERROR("{}", message); break;} - -VpcPicConverter::VpcPicConverter(){ - -} - -VpcPicConverter::~VpcPicConverter(){ - if(nullptr == stream_){ - aclrtDestroyStream(stream_); - } -} - -int VpcPicConverter::init(){ - - aclrtSetDevice(m_devId); - aclrtCreateContext(&context_, m_devId); - - CHECK_AND_RETURN(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed"); - CHECK_AND_RETURN(aclrtCreateStream(&stream_), "aclrtCreateStream failed! "); - - dvppChannelDesc_ = acldvppCreateChannelDesc(); - - int ret = ACL_ERROR_NONE; - do - { - ret = acldvppCreateChannel(dvppChannelDesc_); - CHECK_AND_BREAK(ret, "acldvppCreateChannel failed !"); - - ret = acldvppSetChannelDescMode(dvppChannelDesc_, DVPP_CHNMODE_VPC); - CHECK_AND_BREAK(ret, "acldvppSetChannelDescMode failed !"); - } while (0); - - return ret; -} - -DvppDataMemory* VpcPicConverter::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, int out_height, bool key_frame){ - - aclrtSetDevice(m_devId); - aclrtSetCurrentContext(context_); - - int out_buf_width = ALIGN_UP(out_width, 16) * 3; - int out_buf_height = ALIGN_UP(out_height, 2); - int out_buf_size = out_buf_width * out_buf_height; - - DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, "", to_string(m_devId), key_frame, 0); - void *outBufferDev_ = (void*)rgbMem->getMem(); - - acldvppPicDesc *outputDesc_= acldvppCreatePicDesc(); - acldvppSetPicDescData(outputDesc_, outBufferDev_); - acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); - acldvppSetPicDescWidth(outputDesc_, out_width); - acldvppSetPicDescHeight(outputDesc_, out_height); - acldvppSetPicDescWidthStride(outputDesc_, out_buf_width); - acldvppSetPicDescHeightStride(outputDesc_, out_buf_height); - acldvppSetPicDescSize(outputDesc_, out_buf_size); - - aclError ret = ACL_ERROR_NONE; - do{ - // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 - ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); - if(ret != ACL_ERROR_NONE){ - LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); - break; - } - ret = aclrtSynchronizeStream(stream_); - if(ret != ACL_ERROR_NONE){ - LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); - break; - } - }while(0); - - acldvppDestroyPicDesc(outputDesc_); - - if(ret != ACL_ERROR_NONE){ - delete rgbMem; - rgbMem = nullptr; - } - - return rgbMem; -} - -DvppDataMemory* VpcPicConverter::convert2bgr(DvppDataMemory* inMem){ - - aclrtSetDevice(m_devId); - aclrtSetCurrentContext(context_); - - int out_width = inMem->getWidth(); - int out_height = inMem->getHeight(); - - - acldvppPicDesc *inputDesc_= acldvppCreatePicDesc(); - acldvppSetPicDescData(inputDesc_, inMem->getMem()); - acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); - acldvppSetPicDescWidth(inputDesc_, out_width); - acldvppSetPicDescHeight(inputDesc_, out_height); - acldvppSetPicDescWidthStride(inputDesc_, inMem->getWidthStride()); - acldvppSetPicDescHeightStride(inputDesc_, inMem->getHeightStride()); - acldvppSetPicDescSize(inputDesc_, inMem->getSize()); - - int out_buf_width = ALIGN_UP(out_width, 16) * 3; - int out_buf_height = ALIGN_UP(out_height, 2); - int out_buf_size = out_buf_width * out_buf_height; - - DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, inMem->getId(), inMem->getDeviceId(), false, inMem->getFrameNb()); - void *outBufferDev_ = (void*)rgbMem->getMem(); - - acldvppPicDesc *outputDesc_= acldvppCreatePicDesc(); - acldvppSetPicDescData(outputDesc_, outBufferDev_); - acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); - acldvppSetPicDescWidth(outputDesc_, out_width); - acldvppSetPicDescHeight(outputDesc_, out_height); - acldvppSetPicDescWidthStride(outputDesc_, out_buf_width); - acldvppSetPicDescHeightStride(outputDesc_, out_buf_height); - acldvppSetPicDescSize(outputDesc_, out_buf_size); - - aclError ret = ACL_ERROR_NONE; - do{ - // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 - ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); - if(ret != ACL_ERROR_NONE){ - LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); - break; - } - ret = aclrtSynchronizeStream(stream_); - if(ret != ACL_ERROR_NONE){ - LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); - break; - } - }while(0); - - acldvppDestroyPicDesc(outputDesc_); - acldvppDestroyPicDesc(inputDesc_); - - if(ret != ACL_ERROR_NONE){ - delete rgbMem; - rgbMem = nullptr; - } - - return rgbMem; -} \ No newline at end of file diff --git a/src/decoder/dvpp/VpcPicConverter.h b/src/decoder/dvpp/VpcPicConverter.h deleted file mode 100644 index b9f4f72..0000000 --- a/src/decoder/dvpp/VpcPicConverter.h +++ /dev/null @@ -1,21 +0,0 @@ -#include "dvpp_headers.h" -#include "depend_headers.h" -#include "DvppDataMemory.hpp" - - -class VpcPicConverter{ -public: - VpcPicConverter(); - ~VpcPicConverter(); - int init(); - - DvppDataMemory* convert2bgr(acldvppPicDesc *input, int out_width, int out_height, bool key_frame); - DvppDataMemory* convert2bgr(DvppDataMemory* inMem); - -private: - aclrtContext context_; - aclrtStream stream_; - int m_devId; - acldvppChannelDesc *dvppChannelDesc_ ; - string m_dec_name; -}; \ No newline at end of file diff --git a/src/decoder/dvpp/VpcUtils.cpp b/src/decoder/dvpp/VpcUtils.cpp new file mode 100644 index 0000000..aec7c52 --- /dev/null +++ b/src/decoder/dvpp/VpcUtils.cpp @@ -0,0 +1,213 @@ +#include "VpcUtils.h" +#include "depend_headers.h" + +#define ALIGN_UP(val, align) (((val) % (align) == 0) ? (val) : (((val) / (align) + 1) * (align))) + +#define CHECK_AND_RETURN(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message); return ret;} +#define CHECK_NOT_RETURN(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message);} +#define CHECK_AND_RETURN_NOVALUE(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message); return;} +#define CHECK_AND_BREAK(ret, message) \ + if(ret != 0) {LOG_ERROR("{}", message); break;} + +VpcUtils::VpcUtils(){ + +} + +VpcUtils::~VpcUtils(){ + if(nullptr != stream_){ + aclrtDestroyStream(stream_); + } + + if(context_){ + aclrtDestroyContext(context_); + } + + if (dvppChannelDesc_) { + (void)acldvppDestroyChannel(dvppChannelDesc_); + (void)acldvppDestroyChannelDesc(dvppChannelDesc_); + dvppChannelDesc_ = nullptr; + } +} + +int VpcUtils::init(int devId){ + + m_devId = devId; + + aclrtSetDevice(m_devId); + aclrtCreateContext(&context_, m_devId); + + CHECK_AND_RETURN(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed"); + CHECK_AND_RETURN(aclrtCreateStream(&stream_), "aclrtCreateStream failed! "); + + dvppChannelDesc_ = acldvppCreateChannelDesc(); + + int ret = ACL_ERROR_NONE; + do + { + ret = acldvppCreateChannel(dvppChannelDesc_); + CHECK_AND_BREAK(ret, "acldvppCreateChannel failed !"); + + ret = acldvppSetChannelDescMode(dvppChannelDesc_, DVPP_CHNMODE_VPC); + CHECK_AND_BREAK(ret, "acldvppSetChannelDescMode failed !"); + } while (0); + + return ret; +} + +DvppDataMemory* VpcUtils::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, int out_height, bool key_frame){ + + aclrtSetDevice(m_devId); + aclrtSetCurrentContext(context_); + + int out_buf_width = ALIGN_UP(out_width, 16) * 3; + int out_buf_height = ALIGN_UP(out_height, 2); + int out_buf_size = out_buf_width * out_buf_height; + + DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, "", to_string(m_devId), key_frame, 0); + void *outBufferDev_ = (void*)rgbMem->getMem(); + + acldvppPicDesc *outputDesc_= acldvppCreatePicDesc(); + acldvppSetPicDescData(outputDesc_, outBufferDev_); + acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); + acldvppSetPicDescWidth(outputDesc_, out_width); + acldvppSetPicDescHeight(outputDesc_, out_height); + acldvppSetPicDescWidthStride(outputDesc_, out_buf_width); + acldvppSetPicDescHeightStride(outputDesc_, out_buf_height); + acldvppSetPicDescSize(outputDesc_, out_buf_size); + + aclError ret = ACL_ERROR_NONE; + do{ + // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 + ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + ret = aclrtSynchronizeStream(stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + }while(0); + + acldvppDestroyPicDesc(outputDesc_); + + if(ret != ACL_ERROR_NONE){ + delete rgbMem; + rgbMem = nullptr; + } + + return rgbMem; +} + +DvppDataMemory* VpcUtils::convert2bgr(DvppDataMemory* inMem){ + + aclrtSetDevice(m_devId); + aclrtSetCurrentContext(context_); + + int out_width = inMem->getWidth(); + int out_height = inMem->getHeight(); + + + acldvppPicDesc *inputDesc_= acldvppCreatePicDesc(); + acldvppSetPicDescData(inputDesc_, inMem->getMem()); + acldvppSetPicDescFormat(inputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420); + acldvppSetPicDescWidth(inputDesc_, out_width); + acldvppSetPicDescHeight(inputDesc_, out_height); + acldvppSetPicDescWidthStride(inputDesc_, inMem->getWidthStride()); + acldvppSetPicDescHeightStride(inputDesc_, inMem->getHeightStride()); + acldvppSetPicDescSize(inputDesc_, inMem->getSize()); + + int out_buf_width = ALIGN_UP(out_width, 16) * 3; + int out_buf_height = ALIGN_UP(out_height, 2); + int out_buf_size = out_buf_width * out_buf_height; + + DvppDataMemory* rgbMem = new DvppDataMemory(3, out_buf_width, out_buf_width, out_buf_height, out_buf_height, out_buf_size, inMem->getId(), inMem->getDeviceId(), false, inMem->getFrameNb()); + void *outBufferDev_ = (void*)rgbMem->getMem(); + + acldvppPicDesc *outputDesc_= acldvppCreatePicDesc(); + acldvppSetPicDescData(outputDesc_, outBufferDev_); + acldvppSetPicDescFormat(outputDesc_, PIXEL_FORMAT_BGR_888); + acldvppSetPicDescWidth(outputDesc_, out_width); + acldvppSetPicDescHeight(outputDesc_, out_height); + acldvppSetPicDescWidthStride(outputDesc_, out_buf_width); + acldvppSetPicDescHeightStride(outputDesc_, out_buf_height); + acldvppSetPicDescSize(outputDesc_, out_buf_size); + + aclError ret = ACL_ERROR_NONE; + do{ + // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 + ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("acldvppVpcConvertColorAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + ret = aclrtSynchronizeStream(stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + }while(0); + + acldvppDestroyPicDesc(outputDesc_); + acldvppDestroyPicDesc(inputDesc_); + + if(ret != ACL_ERROR_NONE){ + delete rgbMem; + rgbMem = nullptr; + } + + return rgbMem; +} + +DvppDataMemory* VpcUtils::resize(acldvppPicDesc *inputDesc_, int out_width, int out_height){ + + aclrtSetDevice(m_devId); + aclrtSetCurrentContext(context_); + + int out_buf_width = ALIGN_UP(out_width, 16); + int out_buf_height = ALIGN_UP(out_height, 2); + int out_buf_size = out_buf_width * out_buf_height * 3 / 2; + + DvppDataMemory* rgbMem = new DvppDataMemory(1, out_width, out_buf_width, out_height, out_buf_height, out_buf_size, "", "", false, 0); + void *outBufferDev_ = (void*)rgbMem->getMem(); + + acldvppPicDesc *outputDesc_= acldvppCreatePicDesc(); + acldvppSetPicDescData(outputDesc_, outBufferDev_); + acldvppSetPicDescFormat(outputDesc_, acldvppGetPicDescFormat(inputDesc_)); + acldvppSetPicDescWidth(outputDesc_, out_width); + acldvppSetPicDescHeight(outputDesc_, out_height); + acldvppSetPicDescWidthStride(outputDesc_, out_buf_width); + acldvppSetPicDescHeightStride(outputDesc_, out_buf_height); + acldvppSetPicDescSize(outputDesc_, out_buf_size); + + acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig(); + + aclError ret = ACL_ERROR_NONE; + do{ + // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 + ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc_, outputDesc_, resizeConfig_, stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("acldvppVpcResizeAsync failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + ret = aclrtSynchronizeStream(stream_); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("aclrtSynchronizeStream failed - out_width:{} out_height:{} out_buf_width:{} out_buf_height:{} out_buf_size:{}", out_width, out_height, out_buf_width, out_buf_height, out_buf_size); + break; + } + }while(0); + + acldvppDestroyResizeConfig(resizeConfig_); + acldvppDestroyPicDesc(outputDesc_); + + if(ret != ACL_ERROR_NONE){ + delete rgbMem; + rgbMem = nullptr; + } + + return rgbMem; +} \ No newline at end of file diff --git a/src/decoder/dvpp/VpcUtils.h b/src/decoder/dvpp/VpcUtils.h new file mode 100644 index 0000000..19e0649 --- /dev/null +++ b/src/decoder/dvpp/VpcUtils.h @@ -0,0 +1,23 @@ +#include "dvpp_headers.h" +#include "depend_headers.h" +#include "DvppDataMemory.hpp" + + +class VpcUtils{ +public: + VpcUtils(); + ~VpcUtils(); + int init(int); + + DvppDataMemory* convert2bgr(acldvppPicDesc *input, int out_width, int out_height, bool key_frame); + DvppDataMemory* convert2bgr(DvppDataMemory* inMem); + + DvppDataMemory* resize(acldvppPicDesc *inputDesc_, int out_width, int out_height); + +private: + aclrtContext context_; + aclrtStream stream_; + int m_devId; + acldvppChannelDesc *dvppChannelDesc_ ; + string m_dec_name; +}; \ No newline at end of file diff --git a/src/decoder/dvpp/dvpp_headers.h b/src/decoder/dvpp/dvpp_headers.h index 85a80c5..936d2a1 100644 --- a/src/decoder/dvpp/dvpp_headers.h +++ b/src/decoder/dvpp/dvpp_headers.h @@ -25,7 +25,7 @@ #include "acl/acl_rt.h" #include "acl/acl.h" #include "acl/ops/acl_dvpp.h" - +#include "acl/dvpp/hi_dvpp.h" #endif diff --git a/src/decoder/interface/AbstractDecoder.h b/src/decoder/interface/AbstractDecoder.h index 38d9841..c2c5615 100644 --- a/src/decoder/interface/AbstractDecoder.h +++ b/src/decoder/interface/AbstractDecoder.h @@ -18,12 +18,13 @@ public: virtual void pause() = 0; virtual void resume() = 0; - virtual void setDecKeyframe(bool bKeyframe) = 0; + virtual void setDecKeyframe(bool ) = 0; virtual bool isRunning() = 0; virtual bool isFinished() = 0; virtual bool isPausing() = 0; virtual bool getResolution( int &width, int &height ) = 0; + virtual bool getOutResolution( int &width, int &height ) = 0; virtual bool isSurport(FFDecConfig& cfg) = 0; diff --git a/src/decoder/interface/DecoderManager.cpp b/src/decoder/interface/DecoderManager.cpp index 489720c..b1ecb6d 100644 --- a/src/decoder/interface/DecoderManager.cpp +++ b/src/decoder/interface/DecoderManager.cpp @@ -429,6 +429,27 @@ bool DecoderManager::getResolution(const string name, int &width, int &height) return false; } +bool DecoderManager::getOutResolution(const string name, int &width, int &height) +{ + if (name.empty()) + { + LOG_ERROR("name 为空!"); + return false; + } + + std::lock_guard l(m_mutex); + + auto dec = decoderMap.find(name); + if (dec != decoderMap.end()) + { + dec->second->getOutResolution(width, height); + return true; + } + + LOG_ERROR("没有找到name为{}的解码器",name); + return false; +} + vector DecoderManager::getAllDecodeName(){ closeAllFinishedDecoder(); diff --git a/src/decoder/interface/DecoderManager.h b/src/decoder/interface/DecoderManager.h index d3efbea..cd10e16 100644 --- a/src/decoder/interface/DecoderManager.h +++ b/src/decoder/interface/DecoderManager.h @@ -218,6 +218,8 @@ public: **************************************************/ bool getResolution(const string name, int &width, int &height); + bool getOutResolution(const string name, int &width, int &height); + /************************************************** * 接口:getAllDecodeName * 功能:获取全部解码器名称 diff --git a/src/decoder/interface/DeviceMemory.hpp b/src/decoder/interface/DeviceMemory.hpp index a7e3863..a5b913c 100644 --- a/src/decoder/interface/DeviceMemory.hpp +++ b/src/decoder/interface/DeviceMemory.hpp @@ -30,6 +30,10 @@ public: int getSize() { return data_size; } + + void setSize(int size) { + data_size = size; + } bool isIsused() { return isused; @@ -45,46 +49,90 @@ public: return id; } + void setId(string _id) { + id = _id; + } + string getDeviceId() { return device_id; } + void setDeviceId(string _device_id) { + device_id = _device_id; + } + unsigned char* getMem(){ return pHwRgb; } + void setMem(unsigned char* _pHwRgb) { + pHwRgb = _pHwRgb; + } + long long getTimesstamp(){ return timestamp; } + void setTimesstamp(long long _timestamp) { + timestamp = _timestamp; + } + int getWidth(){ return width; } + void setWidth(int _width) { + width = _width; + } + int getWidthStride(){ return width_stride; } + void setWidthStride(int _width_stride) { + width_stride = _width_stride; + } + int getHeight(){ return height; } + void setHeight(int _height) { + height = _height; + } + int getHeightStride(){ return height_stride; } + void setHeightStride(int _height_stride) { + height_stride = _height_stride; + } + int getChannel(){ return channel; } + void setChannel(int _channel) { + channel = _channel; + } + bool isKeyFrame(){ return key_frame; } + void setKeyFrame(bool _key_frame) { + key_frame = _key_frame; + } + unsigned long long getFrameNb() { return frame_nb; } + void setFrameNb(unsigned long long _frame_nb) { + frame_nb = _frame_nb; + } + public: int data_size; bool isused; diff --git a/src/decoder/interface/interface_headers.h b/src/decoder/interface/interface_headers.h index 7c01669..da6346f 100644 --- a/src/decoder/interface/interface_headers.h +++ b/src/decoder/interface/interface_headers.h @@ -35,6 +35,7 @@ struct FFDecConfig{ bool force_tcp{true}; // 是否指定使用tcp连接 int skip_frame{1}; // 跳帧数 string dec_name; + bool resize{false}; // 是否resize到1920,1080 int port; // gb28181接收数据的端口号 DECODE_REQUEST_STREAM_CALLBACK request_stream_cbk; // gb28181请求流 diff --git a/src/demo/demo.cpp b/src/demo/demo.cpp index 9bd58a8..481e446 100755 --- a/src/demo/demo.cpp +++ b/src/demo/demo.cpp @@ -998,6 +998,9 @@ string createTask(void *handle, std::vector algor_vec, int gi, // FFmpeg生成的解码错误数据流 tparam.ipc_url = "rtsp://192.168.10.4:8554/street"; break; + case 27: + tparam.ipc_url = "/data/share/data/Street_4k_265.mp4"; + break; default: tparam.ipc_url = "/opt/share/data/Street.uvf"; break; @@ -1123,26 +1126,31 @@ void test_gpu(int gpuID){ algorithm_type_t::VEHICLE_SOLIDLINETURNAROUND}; - int repeat_num = 1000; - createTask(handle, algor_vec2, 0, false); - createTask(handle, algor_vec2, 2, false); - while(repeat_num--) { - printf("============================:%d\n",repeat_num); - vector task_ids; - for (int i = 4; i < 10; i ++) { - string task_id = createTask(handle, algor_vec2, i, false); - task_ids.push_back(task_id); - } + // int repeat_num = 1000; + // createTask(handle, algor_vec2, 0, false); + // createTask(handle, algor_vec2, 2, false); + // while(repeat_num--) { + // printf("============================:%d\n",repeat_num); + // vector task_ids; + // for (int i = 4; i < 10; i ++) { + // string task_id = createTask(handle, algor_vec2, i, false); + // task_ids.push_back(task_id); + // } - // test_snapshot(handle); - sleep(60); //60s - close_all_task(handle); - for (auto& task_id : task_ids) { - finish_task(handle, (char*)task_id.data(), 0); - sleep(5); - } + // // test_snapshot(handle); + // sleep(60); //60s + // close_all_task(handle); + // for (auto& task_id : task_ids) { + // finish_task(handle, (char*)task_id.data(), 0); + // sleep(5); + // } - } + // } + + createTask(handle, algor_vec2, 8, false); + createTask(handle, algor_vec2, 27, false); + + while (getchar() != 'q'); /* char ch = 'a'; -- libgit2 0.21.4