dvpp_jpegd.cpp 5.01 KB
/**
* Copyright 2020 Huawei Technologies Co., Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

* http://www.apache.org/licenses/LICENSE-2.0

* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.

* File dvpp_process.cpp
* Description: handle dvpp process
*/

#include <iostream>
#include "acl/acl.h"
#include "dvpp_jpegd.h"
#include "utils.h"
#include "sy_errorinfo.h"
using namespace std;

DvppJpegD::DvppJpegD(aclrtStream& stream,  acldvppChannelDesc *dvppChannelDesc)
    : stream_(stream), dvppChannelDesc_(dvppChannelDesc),
      decodeOutBufferDev_(nullptr), decodeOutputDesc_(nullptr),
      decodeOutWidthStride_(0), decodeOutHeightStride_(0)
{
}

DvppJpegD::~DvppJpegD()
{
    DestroyDecodeResource();
}


int DvppJpegD::InitDecodeOutputDesc(ImageData& inputImage)
{
    //230316 ascend310p兼容=================================================
    auto socVersion = aclrtGetSocName();
    if (strncmp(socVersion, "Ascend310P3", sizeof("Ascend310P3") - 1) == 0) {
        decodeOutWidth_ = ALIGN_UP2(inputImage.width);
        decodeOutHeight_ = ALIGN_UP2(inputImage.height);
        decodeOutWidthStride_ = ALIGN_UP64(inputImage.width); // 64-byte alignment
        decodeOutHeightStride_ = ALIGN_UP16(inputImage.height); // 16-byte alignment
    } else {
        decodeOutWidth_ = inputImage.width;
        decodeOutHeight_ = inputImage.height;
        decodeOutWidthStride_ = ALIGN_UP128(inputImage.width); // 128-byte alignment
        decodeOutHeightStride_ = ALIGN_UP16(inputImage.height); // 16-byte alignment
    }
    if (decodeOutWidthStride_ == 0 || decodeOutHeightStride_ == 0) {
        ERROR_LOG("InitDecodeOutputDesc AlignmentHelper failed");
        return SY_FAILED;
    }
    //=====================================================================
    
    acldvppJpegPredictDecSize(inputImage.data.get(), inputImage.size,
    PIXEL_FORMAT_YUV_SEMIPLANAR_420, &decodeOutBufferSize_);

    aclError aclRet = acldvppMalloc(&decodeOutBufferDev_, decodeOutBufferSize_);
    //debug========================================================================
    uint32_t size = ALIGN_UP(decodeOutBufferSize_,32) + 32;
    aclRet = aclrtMemset(decodeOutBufferDev_,size, 128, size);
    // aclRet = aclrtMemset(decodeOutBufferDev_,decodeOutBufferSize_, 0, decodeOutBufferSize_);
    //debug end====================================================================
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppMalloc decodeOutBufferDev_ failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

    decodeOutputDesc_ = acldvppCreatePicDesc();
    if (decodeOutputDesc_ == nullptr) {
        ERROR_LOG("acldvppCreatePicDesc decodeOutputDesc_ failed");
        return SY_FAILED;
    }

    acldvppSetPicDescData(decodeOutputDesc_, decodeOutBufferDev_);
    acldvppSetPicDescFormat(decodeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
    acldvppSetPicDescWidth(decodeOutputDesc_, decodeOutWidth_);
    acldvppSetPicDescHeight(decodeOutputDesc_, decodeOutHeight_);
    acldvppSetPicDescWidthStride(decodeOutputDesc_, decodeOutWidthStride_);
    acldvppSetPicDescHeightStride(decodeOutputDesc_, decodeOutHeightStride_);
    acldvppSetPicDescSize(decodeOutputDesc_, decodeOutBufferSize_);
    return SY_SUCCESS;
}


int DvppJpegD::Process(ImageData& dest, ImageData& src)
{
    int ret = InitDecodeOutputDesc(src);
    if (ret != SY_SUCCESS) {
        ERROR_LOG("InitDecodeOutputDesc failed");
        return SY_FAILED;
    }

    ImageData imageDevice;
    Utils::CopyImageDataToDvpp(imageDevice, src);

    //TODO:
    aclError aclRet = acldvppJpegDecodeAsync(dvppChannelDesc_, reinterpret_cast<void *>(imageDevice.data.get()),
    imageDevice.size, decodeOutputDesc_, stream_);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppJpegDecodeAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

    aclRet = aclrtSynchronizeStream(stream_);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("decode aclrtSynchronizeStream failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

    dest.width = decodeOutWidth_;
    dest.height = decodeOutHeight_;
    dest.alignWidth = decodeOutWidthStride_;
    dest.alignHeight = decodeOutHeightStride_;
    dest.size = YUV420SP_SIZE(dest.alignWidth, dest.alignHeight);
    dest.data = SHARED_PRT_DVPP_BUF(decodeOutBufferDev_);
    // cout << "dvpp w:" << dest.width << " h:" << dest.height << " alignw:" << dest.alignWidth <<" alignh:" <<dest.alignHeight << " size: " << dest.size << endl;
    // INFO_LOG("convert image success");

    return SY_SUCCESS;
}

void DvppJpegD::DestroyDecodeResource()
{
    if (decodeOutputDesc_ != nullptr) {
        acldvppDestroyPicDesc(decodeOutputDesc_);
        decodeOutputDesc_ = nullptr;
    }
}