DvppDec.cpp 13.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
#include "DvppDec.h"
#include "DvppSourceManager.h"

struct Vdec_CallBack_UserData {
    uint64_t frameId;
    long startTime;
    long sendTime;
    // void* vdecOutputBuf;
	DvppDec* self;
	shared_ptr<MemNode> inBufNode;
    Vdec_CallBack_UserData() {
        frameId = 0;
    }
};

static const int g_pkt_size = 1024 * 1024;

 DvppDec::DvppDec(){
    m_decode_thread = 0;
 }

 DvppDec::~DvppDec(){
    releaseResource();
 }

 bool DvppDec::init_vdpp(DvppDecConfig cfg){

    m_dec_name = cfg.dec_name;

    LOG_INFO("[{}]- Init device start...", m_dec_name);

    m_dvpp_deviceId = atoi(cfg.dev_id.c_str());
    
    if(cfg.codec_id == 0){
        // 66:Baseline,77:Main,>=100:High
        if(cfg.profile == 77){
            enType = H264_MAIN_LEVEL;
        }else if(cfg.profile < 77){
            enType = H264_BASELINE_LEVEL;
        }else{
            enType = H264_HIGH_LEVEL;
        }
    }else if(cfg.codec_id == 1){
        // h265只有main
        enType = H265_MAIN_LEVEL;
    }else {
        LOG_ERROR("[{}]- codec_id is not supported!", m_dec_name);
        return false;
    }

    post_decoded_cbk = cfg.post_decoded_cbk;
    m_pktQueueptr = cfg.pktQueueptr;

	// DvppSourceManager 创建时包含 aclInit,析构时包含 aclFinalize
	DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
	m_context = pSrcMgr->getContext(m_dvpp_deviceId);
	m_dvpp_channel = pSrcMgr->getChannel(m_dvpp_deviceId);
	if(m_dvpp_channel < 0){
        LOG_ERROR("[{}]-该设备channel已经用完了!", m_dec_name);
		return false;
	}

    do
    {
        CHECK_AND_BREAK(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed !");

        int ret = picConverter.init(m_context, m_dec_name);
        if(ret != ACL_ERROR_NONE){
            LOG_ERROR("[{}]- acldvppMalloc failed!, ret:{}", m_dec_name, ret);
            break;
        }

        // queue_size 最小应大于16,否则关键帧之间距离太远的时候会导致回调函数与循环队列卡死
        for (size_t i = 0; i < 20; i++){
            void *vdecInputbuf = nullptr;
            ret = acldvppMalloc((void **)&vdecInputbuf, g_pkt_size);
            if(ret != ACL_ERROR_NONE){
                LOG_ERROR("[{}]- acldvppMalloc failed!, ret:{}", m_dec_name, ret);
                // 析构函数中有对channel 的补救性释放,所以这里可以直接return
                return false;;
            }
            m_vec_vdec.push_back(vdecInputbuf);
        }

        if(!m_vdecQueue.init(m_vec_vdec)){
            break;
        }

        m_vdec_out_size = cfg.width * cfg.height * 3 / 2;

        LOG_INFO("[{}]- init vdpp success! device:{} channel:{}", m_dec_name, m_dvpp_deviceId, m_dvpp_channel);
        return true;

    } while (0);
    
    LOG_INFO("[{}]- init vdpp failed!", m_dec_name);
    // 初始化失败,释放channel
    pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
    return false;
}

bool DvppDec::start(){
	m_bRunning = true;

	pthread_create(&m_decode_thread,0,
        [](void* arg)
        {
            DvppDec* a=(DvppDec*)arg;
            a->decode_thread();
            return (void*)0;
        }
    ,this);

	return true;
}

static void *ReportThd(void *arg)
{
    DvppDec *self = (DvppDec *)arg;
	if(nullptr != self){
		self->doProcessReport();
	}
    return (void *)0;
}

void DvppDec::doProcessReport(){

	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
    // 阻塞等待vdec线程开始

    int ret;
    while (!m_bExitReportThd) {
        aclrtProcessReport(1000);
    }
}

static void VdecCallback(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData)
{
	Vdec_CallBack_UserData *userData = (Vdec_CallBack_UserData *) pUserData;
    if(nullptr != userData){
        DvppDec* self = userData->self;
        if(self != nullptr){

            self->doVdppVdecCallBack(input, output);
        }
        delete userData;
	    userData = nullptr;
    }
}

void DvppDec::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output){

	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");

	void *inputDataDev = acldvppGetStreamDescData(input);
	void *outputDataDev = acldvppGetPicDescData(output);
	uint32_t outputSize = acldvppGetPicDescSize(output);
	uint32_t width = acldvppGetPicDescWidth(output);
	uint32_t height = acldvppGetPicDescHeight(output);

	
    DvppRgbMemory* rgbMem = picConverter.convert2bgr(output, width, height, false);
    if(rgbMem != nullptr){
#ifdef TEST_DECODER
        // D2H
        if(vdecHostAddr == nullptr){
            CHECK_NOT_RETURN(aclrtMallocHost(&vdecHostAddr, width * height * 3), "aclrtMallocHost failed");
        }
        uint32_t data_size = rgbMem->getSize();
        CHECK_AND_RETURN_NOVALUE(aclrtMemcpy(vdecHostAddr, data_size, rgbMem->getMem(), data_size, ACL_MEMCPY_DEVICE_TO_HOST), "D2H aclrtMemcpy failed");

        // 保存vdec结果
        if(count_frame > 45 && count_frame < 50)
        {
            string file_name = "./yuv_pic/vdec_out_"+ m_dec_name +".rgb" ;
            FILE *outputFile = fopen(file_name.c_str(), "a");
            if(outputFile){
                fwrite(vdecHostAddr, data_size, sizeof(char), outputFile);
                fclose(outputFile);
            }
        }
        else if(count_frame > 50 && vdecHostAddr != nullptr){
            CHECK_NOT_RETURN(aclrtFreeHost(vdecHostAddr), "aclrtFreeHost failed");
            vdecHostAddr = nullptr;
        }
        count_frame++;
#endif
        post_decoded_cbk(m_postDecArg, rgbMem);
    }else{
        LOG_ERROR("[{}]- convert2bgr failed !", m_dec_name);
    }

    acldvppFree((uint8_t*)outputDataDev);
    outputDataDev = nullptr;

	m_vdecQueue.addHead();

	CHECK_AND_RETURN_NOVALUE(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
	CHECK_AND_RETURN_NOVALUE(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
}

void DvppDec::close(){
	m_bRunning=false;

	if(m_decode_thread != 0){
		pthread_join(m_decode_thread,0);
	}
}

bool DvppDec::sendVdecEos(aclvdecChannelDesc *vdecChannelDesc){
    // create stream desc
    acldvppStreamDesc *streamInputDesc = acldvppCreateStreamDesc();
    if (streamInputDesc == nullptr) {
        LOG_ERROR("[{}]- fail to create input stream desc", m_dec_name);
        return false;
    }
    aclError ret = acldvppSetStreamDescEos(streamInputDesc, 1);
    if (ret != ACL_SUCCESS) {
        LOG_ERROR("[{}]- fail to set eos for stream desc, errorCode = {}", m_dec_name, static_cast<int32_t>(ret));
        (void)acldvppDestroyStreamDesc(streamInputDesc);
        return false;
    }

    // send vdec eos frame. when all vdec callback are completed, aclvdecSendFrame can be returned.
    LOG_INFO("[{}]- send eos", m_dec_name);
    ret = aclvdecSendFrame(vdecChannelDesc, streamInputDesc, nullptr, nullptr, nullptr);
    if (ret != ACL_SUCCESS) {
        LOG_ERROR("[{}]- fail to send eos frame, ret={}", m_dec_name, ret);
        (void)acldvppDestroyStreamDesc(streamInputDesc);
        return false;
    }
    (void)acldvppDestroyStreamDesc(streamInputDesc);

    return true;
}

void DvppDec::releaseResource(){
	for(int i = 0; i < m_vec_vdec.size(); i++){
		if(m_vec_vdec[i] != nullptr){
			acldvppFree(m_vec_vdec[i]);
			m_vec_vdec[i] = nullptr;
		}
	}
	m_vec_vdec.clear();

	DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
	pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
}

void DvppDec::decode_thread(){

    long startTime = UtilTools::get_cur_time_ms();

	int ret = -1;

    // dvpp解码参数
	CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");

    m_bExitReportThd = false;
	pthread_t report_thread;
	ret = pthread_create(&report_thread, nullptr, ReportThd, (void *)this);
	if(ret != 0){
        LOG_ERROR("[{}]- pthread_create failed", m_dec_name);
		return;
	}

    // 创建aclvdecChannelDesc类型的数据
    aclvdecChannelDesc *vdecChannelDesc = aclvdecCreateChannelDesc();
    if (vdecChannelDesc == nullptr) { 
        LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
		return;
	}
    do{
        // 创建 channel dec结构体
        // 通道ID在dvpp层面为0~31
        CHECK_AND_BREAK(aclvdecSetChannelDescChannelId(vdecChannelDesc, m_dvpp_channel), "aclvdecSetChannelDescChannelId failed");
        CHECK_AND_BREAK(aclvdecSetChannelDescThreadId(vdecChannelDesc, report_thread), "aclvdecSetChannelDescThreadId failed");
        CHECK_AND_BREAK(aclvdecSetChannelDescCallback(vdecChannelDesc, VdecCallback), "aclvdecSetChannelDescCallback failed");
        CHECK_AND_BREAK(aclvdecSetChannelDescEnType(vdecChannelDesc, enType), "aclvdecSetChannelDescEnType failed");
        CHECK_AND_BREAK(aclvdecSetChannelDescOutPicFormat(vdecChannelDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420), "aclvdecSetChannelDescOutPicFormat failed");
        CHECK_AND_BREAK(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed");

        uint64_t frame_count = 0;
        bool bBreak = false;
        while (m_bRunning)
        {
            if (m_bPause){
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                continue;
            }
            int ret = sentFrame(vdecChannelDesc, frame_count);
            if(ret == 2){
                break;
                bBreak = true;
            }else if(ret == 1){
                continue;
            }

            frame_count++;
        }

        // 尽量保证数据全部解码完成
        int sum = 0;
        if(!bBreak){
            while(!m_pktQueueptr->isEmpty()){
                int ret = sentFrame(vdecChannelDesc, frame_count);
                if(ret == 2){
                    break;
                }
                std::this_thread::sleep_for(std::chrono::milliseconds(3));
                sum++;
                if(sum > 40){
                    // 避免卡死
                    break;
                }
            }
        }
        
        sendVdecEos(vdecChannelDesc);

        CHECK_NOT_RETURN(aclvdecDestroyChannel(vdecChannelDesc), "aclvdecDestroyChannel failed");
    }while(0);
    
    CHECK_NOT_RETURN(aclvdecDestroyChannelDesc(vdecChannelDesc), "aclvdecDestroyChannelDesc failed");

	// report_thread 需后于destroy退出
	m_bRunning = false;
    m_bExitReportThd = true;
	CHECK_NOT_RETURN(pthread_join(report_thread, nullptr), "pthread_join failed");

    releaseResource();
    LOG_INFO("[{}]- decode thread exit.", m_dec_name);
}

int DvppDec::sentFrame(aclvdecChannelDesc *vdecChannelDesc, uint64_t frame_count){

    AVPacket * pkt = m_pktQueueptr->getHead();
    if(pkt == nullptr){
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        return 1;
    }
    // 解码
    void *vdecInputbuf = m_vdecQueue.getTail();
    if(vdecInputbuf == nullptr){
        std::this_thread::sleep_for(std::chrono::milliseconds(3));
        return 1;
    }
    
    int ret = aclrtMemcpy(vdecInputbuf, pkt->size, pkt->data, pkt->size, ACL_MEMCPY_HOST_TO_DEVICE);
    if(ACL_ERROR_NONE != ret){
        LOG_ERROR("[{}]- aclrtMemcpy failed", m_dec_name);
        return 2;
    }

    void *vdecOutputBuf = nullptr;
    ret = acldvppMalloc((void **)&vdecOutputBuf, m_vdec_out_size);
    if(ret != ACL_ERROR_NONE){
        LOG_ERROR("[{}]- acldvppMalloc failed", m_dec_name);
        return 2;
    }

    acldvppStreamDesc *input_stream_desc = nullptr;
    acldvppPicDesc *output_pic_desc = nullptr;
    do{
        input_stream_desc = acldvppCreateStreamDesc();
        if (input_stream_desc == nullptr) { 
            LOG_ERROR("[{}]- acldvppCreateStreamDesc failed", m_dec_name);
            break;
        }
        output_pic_desc = acldvppCreatePicDesc();
        if (output_pic_desc == nullptr) { 
            LOG_ERROR("[{}]- acldvppCreatePicDesc failed", m_dec_name);
            break;
        }
        CHECK_AND_BREAK(acldvppSetStreamDescData(input_stream_desc, vdecInputbuf), "acldvppSetStreamDescData failed");
        CHECK_AND_BREAK(acldvppSetStreamDescSize(input_stream_desc, pkt->size), "acldvppSetStreamDescSize failed");
        CHECK_AND_BREAK(acldvppSetPicDescData(output_pic_desc, vdecOutputBuf), "acldvppSetPicDescData failed");
        CHECK_AND_BREAK(acldvppSetPicDescSize(output_pic_desc, m_vdec_out_size), "acldvppSetPicDescSize failed");
        
        Vdec_CallBack_UserData *user_data = NULL;
        user_data = new Vdec_CallBack_UserData;
        user_data->frameId = frame_count;
        // user_data->startTime = startTime;
        user_data->sendTime = UtilTools::get_cur_time_ms();
        user_data->self = this;
        ret = aclvdecSendFrame(vdecChannelDesc, input_stream_desc, output_pic_desc, nullptr, reinterpret_cast<void *>(user_data));
        av_packet_unref(pkt);
        m_pktQueueptr->addHead();
        if(ret != ACL_ERROR_NONE){
            delete user_data;
            user_data = nullptr;
            LOG_ERROR("[{}]- aclvdecSendFrame failed", m_dec_name);
            break;
        }

        m_vdecQueue.addTail();

        return 0;
    }while (0);

    // 报错情形
    if(input_stream_desc){
        CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input_stream_desc), "acldvppDestroyStreamDesc failed");
    }
    if(output_pic_desc){
        CHECK_NOT_RETURN(acldvppDestroyPicDesc(output_pic_desc), "acldvppDestroyPicDesc failed");
    }

    acldvppFree(vdecOutputBuf);
	vdecOutputBuf = nullptr;

    return 1;
}


void DvppDec::setPostDecArg(const void* postDecArg){
	m_postDecArg = postDecArg;
}

void DvppDec::pause(){
    m_bPause = true;
}

void DvppDec::resume(){
    m_bPause = false;
}