dvpp_cropandpaste.cpp 21.4 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
/**
* 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 "utils.h"
#include "dvpp_cropandpaste.h"
#include "sy_errorinfo.h"
using namespace std;

DvppCropAndPaste::DvppCropAndPaste(aclrtStream& stream, acldvppChannelDesc *dvppChannelDesc,
                       uint32_t width, uint32_t height)
: stream_(stream), dvppChannelDesc_(dvppChannelDesc),
vpcInputDesc_(nullptr), vpcOutputDesc_(nullptr),
vpcOutBufferDev_(nullptr),vpcOutBufferSize_(0){
    size_.width = width;
    size_.height = height;
}

DvppCropAndPaste::~DvppCropAndPaste()
{
    DestroyCropAndPasteResource();
}

int DvppCropAndPaste::InitCropAndPasteInputDesc(ImageData& inputImage)
{
    originalImageWidth_ = inputImage.width;
    originalImageHeight_ = inputImage.height;
    uint32_t alignWidth = inputImage.alignWidth;
    uint32_t alignHeight = inputImage.alignHeight;
    // printf("image w %d, h %d, align w%d, h%d",inputImage.width, inputImage.height, alignWidth, alignHeight);
    if (alignWidth == 0 || alignHeight == 0) {
        ERROR_LOG("InitResizeInputDesc AlignmentHelper failed. image w %d, h %d, align w%d, h%d",
         inputImage.width, inputImage.height, alignWidth, alignHeight);
        return SY_FAILED;
    }
    uint32_t inputBufferSize = YUV420SP_SIZE(alignWidth, alignHeight);
    vpcInputDesc_ = acldvppCreatePicDesc();
    if (vpcInputDesc_ == nullptr) {
        ERROR_LOG("acldvppCreatePicDesc vpcInputDesc_ failed");
        return SY_FAILED;
    }

    acldvppSetPicDescData(vpcInputDesc_, inputImage.data.get()); //  JpegD . vpcResize
    acldvppSetPicDescFormat(vpcInputDesc_, format_);
    acldvppSetPicDescWidth(vpcInputDesc_, inputImage.width);
    acldvppSetPicDescHeight(vpcInputDesc_, inputImage.height);
    acldvppSetPicDescWidthStride(vpcInputDesc_, alignWidth);
    acldvppSetPicDescHeightStride(vpcInputDesc_, alignHeight);
    acldvppSetPicDescSize(vpcInputDesc_, inputBufferSize);
    return SY_SUCCESS;
}

int DvppCropAndPaste::InitCropAndPasteOutputDesc()
{
    int resizeOutWidth = size_.width;
    int resizeOutHeight = size_.height;
    int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
    int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
   
    if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) {
        ERROR_LOG("InitResizeOutputDesc AlignmentHelper failed");
        return SY_FAILED;
    }
    
    vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
    aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_);
    //debug========================================================================
    uint32_t size = ALIGN_UP(vpcOutBufferSize_,32) + 32;
    aclRet = aclrtMemset(vpcOutBufferDev_,size, 128, size);
    // aclRet = aclrtMemset(vpcOutBufferDev_,vpcOutBufferSize_, 0, vpcOutBufferSize_);
    //debug end====================================================================
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

    vpcOutputDesc_ = acldvppCreatePicDesc();
    if (vpcOutputDesc_ == nullptr) {
        ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed");
        return SY_FAILED;
    }
    acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_);
    acldvppSetPicDescFormat(vpcOutputDesc_, format_);
    acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth);
    acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight);
    acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride);
    acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride);
    acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);

    return SY_SUCCESS;
}

int DvppCropAndPaste::InitRightCropAndPasteOutputDesc()
{
    int resizeOutWidth = size_.width;
    int resizeOutHeight = size_.height;
    int resizeOutWidthStride = ALIGN_UP16(resizeOutWidth);
    int resizeOutHeightStride = ALIGN_UP2(resizeOutHeight);
    if (resizeOutWidthStride == 0 || resizeOutHeightStride == 0) {
        ERROR_LOG("InitResizeOutputDesc AlignmentHelper failed");
        return SY_FAILED;
    }

    vpcOutBufferSize_ = YUV420SP_SIZE(resizeOutWidthStride, resizeOutHeightStride);
    //aclError aclRet = acldvppMalloc(&vpcOutBufferDev_, vpcOutBufferSize_);
    // if (aclRet != ACL_SUCCESS) {
    //     ERROR_LOG("acldvppMalloc vpcOutBufferDev_ failed, aclRet = %d", aclRet);
    //     return SY_FAILED;
    // }
    vpcOutputDesc_ = acldvppCreatePicDesc();
    if (vpcOutputDesc_ == nullptr) {
        ERROR_LOG("acldvppCreatePicDesc vpcOutputDesc_ failed");
        return SY_FAILED;
    }
    acldvppSetPicDescData(vpcOutputDesc_, vpcOutBufferDev_ + vpcOutBufferSize_);
    acldvppSetPicDescFormat(vpcOutputDesc_, format_);
    acldvppSetPicDescWidth(vpcOutputDesc_, resizeOutWidth);
    acldvppSetPicDescHeight(vpcOutputDesc_, resizeOutHeight);
    acldvppSetPicDescWidthStride(vpcOutputDesc_, resizeOutWidthStride);
    acldvppSetPicDescHeightStride(vpcOutputDesc_, resizeOutHeightStride);
    acldvppSetPicDescSize(vpcOutputDesc_, vpcOutBufferSize_);

    return SY_SUCCESS;
}


// IN/OUT Desc
int DvppCropAndPaste::InitCropAndPasteResource(ImageData& inputImage) {
    format_ = static_cast<acldvppPixelFormat>(PIXEL_FORMAT_YUV_SEMIPLANAR_420);
    if (SY_SUCCESS != InitCropAndPasteInputDesc(inputImage)) {
        ERROR_LOG("InitCropAndPasteInputDesc failed");
        return SY_FAILED;
    } 

    if (SY_SUCCESS != InitCropAndPasteOutputDesc()) {
        ERROR_LOG("InitCropAndPasteOutputDesc failed");
        return SY_FAILED;
    } 

    return SY_SUCCESS;  
}

int DvppCropAndPaste::InitRightCropAndPasteResource(ImageData& inputImage) {
    format_ = static_cast<acldvppPixelFormat>(PIXEL_FORMAT_YUV_SEMIPLANAR_420);
    if (SY_SUCCESS != InitCropAndPasteInputDesc(inputImage)) {
        ERROR_LOG("InitCropAndPasteInputDesc failed");
        return SY_FAILED;
    } 
    
    if (SY_SUCCESS != InitRightCropAndPasteOutputDesc()) {
        ERROR_LOG("InitCropAndPasteOutputDesc failed");
        return SY_FAILED;
    } 

    return SY_SUCCESS;  
}

int DvppCropAndPaste::ResizeWithPadding(ImageData& resizedImage, ImageData& srcImage)
{
    if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
        ERROR_LOG("Dvpp cropandpaste failed for init error");
        return SY_FAILED;
    }

    uint32_t cropLeftOffset = 0;  // must even
    uint32_t cropTopOffset = 0;   // must even
    uint32_t cropRightOffset = (((cropLeftOffset + originalImageWidth_) >> 1) << 1) -1;   // must odd
    uint32_t cropBottomOffset = (((cropTopOffset + originalImageHeight_) >> 1) << 1) -1;  // must odd

    cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
    cropTopOffset, cropBottomOffset);
    if (cropArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
        return SY_FAILED;
    }
    // cout << "====================================================" << endl;
    // printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);
     
    bool widthRatioSmaller = true;
    // The scaling ratio is based on the smaller ratio to ensure the smallest edge to fill the targe edge
    float resizeRatio = static_cast<float>(size_.width) / srcImage.width;
    if (resizeRatio > (static_cast<float>(size_.height) / srcImage.height)) {
        resizeRatio = static_cast<float>(size_.height) / srcImage.height;
        widthRatioSmaller = false;
    }

    const int halfValue = 2;
    uint32_t pasteLeftOffset = 0;
    uint32_t pasteRightOffset = 0;
    uint32_t pasteTopOffset = 0;
    uint32_t pasteBottomOffset = 0;
    // The left and up must be even, right and down must be odd which is required by acl
    if (widthRatioSmaller) { //宽较长
        pasteLeftOffset = 0;  // must even
        pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1;   // must odd
        // pasteTopOffset = ((static_cast<uint32_t>((size_.height - srcImage.height * resizeRatio) / halfValue) >> 1) << 1); // must even
        // pasteBottomOffset = (((size_.height - pasteTopOffset) >> 1) << 1) -1;  // must odd
        //debug===============================================================================================
        float pady = ((size_.height - srcImage.height * resizeRatio) / halfValue);
        pasteTopOffset = ((static_cast<uint32_t>(pady) >> 1) << 1); // must even
        pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady) >> 1) << 1) -1;  // must odd
        // if((int)(size_.height - srcImage.height * resizeRatio) % 2 ==0){
        //     pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady) >> 1) << 1) -1;  // must odd
        // }else{
        //     pasteBottomOffset = ((static_cast<uint32_t>(size_.height - pady + 1) >> 1) << 1) -1;  // must odd
        // }
        //debug end============================================================================================
    }else{  //高较长
        uint32_t pad = (static_cast<uint32_t>((size_.width - srcImage.width * resizeRatio) / halfValue));
        // printf("debug pad:%d\n",pad);
        pasteLeftOffset = (pad + 8) / 16 * 16;  // must even,作贴图区域时,需16对齐
        // pasteLeftOffset = ALIGN_UP16(pad);  // must even,作贴图区域时,需16对齐
        pasteRightOffset = (((size_.width - pad) >> 1) << 1) -1;  // must odd
        pasteTopOffset = 0;  // must even
        pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; // must odd

    }

    pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
    pasteTopOffset, pasteBottomOffset);
    if (pasteArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
        return SY_FAILED;
    }
    // printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);
    // crop and patse pic
    aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
    vpcOutputDesc_, cropArea_, pasteArea_, stream_);
    //printf("debug crop line:%d\n",__LINE__);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }
    aclRet = aclrtSynchronizeStream(stream_);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("crop and paste aclrtSynchronizeStream failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

    resizedImage.width = size_.width;
    resizedImage.height = size_.height;
    resizedImage.alignWidth = ALIGN_UP16(size_.width);
    resizedImage.alignHeight = ALIGN_UP2(size_.height);
    resizedImage.size = vpcOutBufferSize_;
    resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);

    DestroyCropAndPasteResource();

    return SY_SUCCESS;
}



int DvppCropAndPaste::PatchProcess(ImageData& resizedImage, ImageData& srcImage, uint32_t xmin, uint32_t ymin,
    uint32_t xmax, uint32_t ymax)
{
    if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
        ERROR_LOG("Dvpp cropandpaste failed for init error");
        return SY_FAILED;
    }

    uint32_t cropLeftOffset = ((xmin >> 1) << 1); // must even
    uint32_t cropTopOffset = ((ymin >> 1) << 1);  // must even
    uint32_t cropRightOffset = ((xmax >> 1) << 1) -1;   // must odd
    uint32_t cropBottomOffset = ((ymax >> 1) << 1) -1;  // must odd

    cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
    cropTopOffset, cropBottomOffset);
    if (cropArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
        return SY_FAILED;
    }
    //printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);

    uint32_t pasteLeftOffset = 0;  // must even
    uint32_t pasteTopOffset = 0;   // must even
    uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1;  // must odd
    uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1; // must odd

    pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
    pasteTopOffset, pasteBottomOffset);
    if (pasteArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
        return SY_FAILED;
    }
    //printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);

    // crop and patse pic
    aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
    vpcOutputDesc_, cropArea_, pasteArea_, stream_);
    //printf("debug crop line:%d\n",__LINE__);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

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

    resizedImage.width = size_.width;
    resizedImage.height = size_.height;
    resizedImage.alignWidth = ALIGN_UP16(size_.width);
    resizedImage.alignHeight = ALIGN_UP2(size_.height);
    resizedImage.size = vpcOutBufferSize_;
    resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);

    DestroyCropAndPasteResource();

    return SY_SUCCESS;
}

int DvppCropAndPaste::Crop2Process(ImageData& resizedImage, ImageData& leftImage, ImageData& rightImage, ImageData& srcImage)
{
    //left
    if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
        ERROR_LOG("Dvpp cropandpaste failed for init error");
        return SY_FAILED;
    }
    uint32_t lcropLeftOffset = 0; // must even
    uint32_t lcropTopOffset = 0;  // must even
    uint32_t lcropRightOffset = ((srcImage.width/2 >> 1) << 1) -1; // must odd
    uint32_t lcropBottomOffset = ((srcImage.height >> 1) << 1) -1; // must odd
    cropArea_ = acldvppCreateRoiConfig(lcropLeftOffset, lcropRightOffset,lcropTopOffset, lcropBottomOffset);
    if (cropArea_ == nullptr) {
        ERROR_LOG("left acldvppCreateRoiConfig cropArea_ failed");
        return SY_FAILED;
    }
    //printf("debug left crop area: %d %d %d %d\n", lcropLeftOffset, lcropTopOffset, lcropRightOffset, lcropBottomOffset);

    uint32_t lpasteLeftOffset = 0; // must even
    uint32_t lpasteTopOffset = 0;  // must even
    uint32_t lpasteRightOffset = (((lpasteLeftOffset + size_.width) >> 1) << 1) -1;  // must odd
    uint32_t lpasteBottomOffset = (((lpasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
    pasteArea_ = acldvppCreateRoiConfig(lpasteLeftOffset, lpasteRightOffset,lpasteTopOffset, lpasteBottomOffset);
    if (pasteArea_ == nullptr) {
        ERROR_LOG("left acldvppCreateRoiConfig pasteArea_ failed");
        return SY_FAILED;
    }
    //printf("debug left paste area: %d %d %d %d\n", lpasteLeftOffset, lpasteTopOffset, lpasteRightOffset, lpasteBottomOffset);

    // crop and patse pic
    aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
    vpcOutputDesc_, cropArea_, pasteArea_, stream_);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("left acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

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

    leftImage.width = size_.width;
    leftImage.height = size_.height;
    leftImage.alignWidth = ALIGN_UP16(size_.width);
    leftImage.alignHeight = ALIGN_UP2(size_.height);
    leftImage.size = vpcOutBufferSize_;
    leftImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);

    DestroyCropAndPasteResource();

    //right
    if (SY_SUCCESS != InitRightCropAndPasteResource(srcImage)) {
        ERROR_LOG("Dvpp cropandpaste failed for init error");
        return SY_FAILED;
    }

    uint32_t rcropLeftOffset = ((srcImage.width/2 >> 1) << 1); // must even
    uint32_t rcropTopOffset = 0;  // must even
    uint32_t rcropRightOffset = ((srcImage.width >> 1) << 1) -1; // must odd
    uint32_t rcropBottomOffset = ((srcImage.height >> 1) << 1) -1; // must odd
    cropArea_ = acldvppCreateRoiConfig(rcropLeftOffset, rcropRightOffset, rcropTopOffset, rcropBottomOffset);
    if (cropArea_ == nullptr) {
        ERROR_LOG("right acldvppCreateRoiConfig cropArea_ failed");
        return SY_FAILED;
    }
    //printf("debug right crop area: %d %d %d %d\n", rcropLeftOffset, rcropTopOffset, rcropRightOffset, rcropBottomOffset);

    uint32_t rpasteLeftOffset = 0; // must even
    uint32_t rpasteTopOffset = 0;  // must even
    uint32_t rpasteRightOffset = (((rpasteLeftOffset + size_.width) >> 1) << 1) -1;  // must odd
    uint32_t rpasteBottomOffset = (((rpasteTopOffset + size_.height) >> 1) << 1) -1; // must odd
    pasteArea_ = acldvppCreateRoiConfig(rpasteLeftOffset, rpasteRightOffset, rpasteTopOffset, rpasteBottomOffset);
    if (pasteArea_ == nullptr) {
        ERROR_LOG("right acldvppCreateRoiConfig pasteArea_ failed");
        return SY_FAILED;
    }
    //printf("debug right paste area: %d %d %d %d\n", rpasteLeftOffset, rpasteTopOffset, rpasteRightOffset, rpasteBottomOffset);

    // crop and patse pic
    aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
    vpcOutputDesc_, cropArea_, pasteArea_, stream_);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("right acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

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

    rightImage.width = size_.width;
    rightImage.height = size_.height;
    rightImage.alignWidth = ALIGN_UP16(size_.width);
    rightImage.alignHeight = ALIGN_UP2(size_.height);
    rightImage.size = vpcOutBufferSize_;
    rightImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);
    // rightImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_ + vpcOutBufferSize_);

    DestroyCropAndPasteResource();

    resizedImage.size = leftImage.size + rightImage.size;
    resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);

    return SY_SUCCESS;
}


int DvppCropAndPaste::Process(ImageData& resizedImage, ImageData& srcImage)
{
    if (SY_SUCCESS != InitCropAndPasteResource(srcImage)) {
        ERROR_LOG("Dvpp cropandpaste failed for init error");
        return SY_FAILED;
    }

    uint32_t cropLeftOffset = 0;  // must even
    uint32_t cropTopOffset = 0;   // must even
    uint32_t cropRightOffset = (((cropLeftOffset + originalImageWidth_) >> 1) << 1) -1;  // must odd
    uint32_t cropBottomOffset = (((cropTopOffset + originalImageHeight_) >> 1) << 1) -1; // must odd

    cropArea_ = acldvppCreateRoiConfig(cropLeftOffset, cropRightOffset,
    cropTopOffset, cropBottomOffset);
    if (cropArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig cropArea_ failed");
        return SY_FAILED;
    }
    //printf("debug crop area: %d %d %d %d\n", cropLeftOffset, cropTopOffset, cropRightOffset, cropBottomOffset);

    uint32_t pasteLeftOffset = 0; // must even
    uint32_t pasteTopOffset = 0;  // must even
    uint32_t pasteRightOffset = (((pasteLeftOffset + size_.width) >> 1) << 1) -1;   // must odd
    uint32_t pasteBottomOffset = (((pasteTopOffset + size_.height) >> 1) << 1) -1;  // must odd

    pasteArea_ = acldvppCreateRoiConfig(pasteLeftOffset, pasteRightOffset,
    pasteTopOffset, pasteBottomOffset);
    if (pasteArea_ == nullptr) {
        ERROR_LOG("acldvppCreateRoiConfig pasteArea_ failed");
        return SY_FAILED;
    }
    //printf("debug paste area: %d %d %d %d\n", pasteLeftOffset, pasteTopOffset, pasteRightOffset, pasteBottomOffset);

    // crop and patse pic
    aclError aclRet = acldvppVpcCropAndPasteAsync(dvppChannelDesc_, vpcInputDesc_,
    vpcOutputDesc_, cropArea_, pasteArea_, stream_);
    //printf("debug crop line:%d\n",__LINE__);
    if (aclRet != ACL_SUCCESS) {
        ERROR_LOG("acldvppVpcCropAndPasteAsync failed, aclRet = %d", aclRet);
        return SY_FAILED;
    }

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

    resizedImage.width = size_.width;
    resizedImage.height = size_.height;
    resizedImage.alignWidth = ALIGN_UP16(size_.width);
    resizedImage.alignHeight = ALIGN_UP2(size_.height);
    resizedImage.size = vpcOutBufferSize_;
    resizedImage.data = SHARED_PRT_DVPP_BUF(vpcOutBufferDev_);

    DestroyCropAndPasteResource();

    return SY_SUCCESS;
}


void DvppCropAndPaste::DestroyCropAndPasteResource()
{
    if (cropArea_ != nullptr) {
        (void)acldvppDestroyRoiConfig(cropArea_);
        cropArea_ = nullptr;
    }

    if (pasteArea_ != nullptr) {
        (void)acldvppDestroyRoiConfig(pasteArea_);
        pasteArea_ = nullptr;
    }

    if (vpcInputDesc_ != nullptr) {
        (void)acldvppDestroyPicDesc(vpcInputDesc_);
        vpcInputDesc_ = nullptr;
    }

    if (vpcOutputDesc_ != nullptr) {
        (void)acldvppDestroyPicDesc(vpcOutputDesc_);
        vpcOutputDesc_ = nullptr;
    }
}