# uyvy\_remap/app.cpp /******************************************************************************* Copyright (c) 2021-2022 Qualcomm Technologies, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Qualcomm Technologies, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. @brief Program to run FastADAS UYVY remap pipeline(s). *******************************************************************************/ #include #include #include #include #include "util.h" #ifdef USE_OPENCV #include #include #include #include #endif #define NORMALIZE 1 #define ST // single- or multi-threaded versions int main( int argc, char** argv ) { int retVal = 0; FadasError_e status = FADAS_ERROR_NONE; status = FadasInit( nullptr ); if( FADAS_ERROR_NONE != status ) // initialize all FADAS features { UTIL_ERROR( "FadasInit failed (status = %u)", status ); return -1; } // Input UYVY image const size_t balign = 128; const char* uyvy_path = "img4.uyvy"; uint32_t uyvy_w = 1824; uint32_t uyvy_h = 940; uint32_t uyvy_s = 2 * uyvy_w; FadasImage_t uyvyImg = { 0 }; status = FadasCreateImage( FADAS_BUF_TYPE_IN, uyvy_w, uyvy_h, FADAS_IMAGE_FORMAT_UYVY, &uyvyImg, balign ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasCreateImage failed (status = %u)", status ); FadasDeInit(); return -1; } UTIL_ReadRAW( uyvy_path, uyvy_s, uyvyImg.props.height, uyvyImg.props.stride[0], uyvyImg.plane[0] ); // Maps uint32_t map_w = 994; uint32_t map_h = 512; uint32_t map_s = map_w * sizeof( float32_t ); float32_t* mapX = (float32_t*)FadasMemAlloc( map_s * map_h, balign, nullptr ); float32_t* mapY = (float32_t*)FadasMemAlloc( map_s * map_h, balign, nullptr ); if( (nullptr == mapX) || (nullptr == mapY) ) { UTIL_ERROR( "Failed to allocate MAP buffer." ); if(nullptr != mapX) { FadasMemFree(mapX); } if(nullptr != mapY) { FadasMemFree(mapY); } FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } memset( mapX, 0, map_s * map_h ); // optional memset( mapY, 0, map_s * map_h ); // optional UTIL_ReadRAW( "mapX.data", map_s, map_h, map_s, mapX ); UTIL_ReadRAW( "mapY.data", map_s, map_h, map_s, mapY ); FadasRemapMap_t* map = FadasRemap_CreateMapFromMap( uyvy_w, uyvy_h, map_w, map_h, map_s, mapX, mapY, FADAS_REMAP_PIPELINE_UYVY_TO_RGB888, 0 ); if( nullptr == map ) { UTIL_ERROR( "Failed to create map!\n" ); FadasMemFree(mapX); FadasMemFree(mapY); FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } /**************************************************************************/ // Ex. #1: Output image scaled to map output size and renormalized FadasImage_t outImg = { 0 }; status = FadasCreateImage( FADAS_BUF_TYPE_INOUT, map_w, map_h, FADAS_IMAGE_FORMAT_RGB888, &outImg, balign ); if( FADAS_ERROR_NONE != status) { UTIL_ERROR( "FadasCreateImage failed (status = %u)", status ); FadasRemap_DestroyMap(map); FadasMemFree(mapX); FadasMemFree(mapY); FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } // Let feature set ROI to entire image unless we only want to focus in on one ROI (e.g., bounding box) FadasROI_t roi = { 0 }; #ifdef ST status = FadasRemap_Run( map, &uyvyImg, &outImg, &roi ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_Run failed (status = %u)", status ); retVal = -1; } #else int32_t n_threads_affinity[] = { 0,1,2,3 }; void* wrkrs = FadasRemap_CreateWorkers( 4, n_threads_affinity, FADAS_REMAP_PIPELINE_UYVY_TO_RGB888 ); if( nullptr == wrkrs ) { UTIL_ERROR( "creating wrkrs 0" ); retVal = -1; } else { status = FadasRemap_RunMT( wrkrs, map, &uyvyImg, &outImg, &roi ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_Run MT failed (status = %u)", status ); retVal = -1; } status = FadasRemap_DestroyWorkers( wrkrs ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_DestroyWorkers failed (status = %u)", status ); retVal = -1; } } #endif #if NORMALIZE if( 0 == retVal ) { FadasImage_t nrmImg = { 0 }; status = FadasCreateImage( FADAS_BUF_TYPE_OUT, map_w, map_h, FADAS_IMAGE_FORMAT_RGB888, &nrmImg, balign ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasCreateImage failed (status = %u)", status ); retVal = -1; } else { uint8_t* outBuf = (uint8_t*)nrmImg.plane[0]; uint8_t* inBuf = (uint8_t*)outImg.plane[0]; FadasNormlzParams_t normlzR = {30.999f, 1.111f, 30.321f}; FadasNormlzParams_t normlzG = {40.888f, 1.222f, 40.432f}; FadasNormlzParams_t normlzB = {50.777f, 1.333f, 50.654f}; status = FadasCvtYUV_Renormalize888( inBuf, outImg.props, normlzR, normlzG, normlzB, outBuf, nrmImg.props.stride[0] ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasCvtYUV_Renormalize888 failed (status = %u)", status ); retVal = -1; } else { char outfile[128] = {0}; snprintf( outfile, 128, "img4_remap_nrm.ppm" ); UTIL_WritePPM( outfile, map_w, map_h, 255, outBuf ,nrmImg.props.stride[0]); } status = FadasDestroyImage( &nrmImg ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasDestroyImage failed (status = %u)", status ); retVal = -1; } } #else uint8_t* image = (uint8_t*)outImg.plane[0]; char outfile[128] = {0}; snprintf( outfile, 128, "img4_remap.ppm" ); UTIL_WritePPM( outfile, map_w, map_h, 255, image ,outImg.props.stride[0]); #endif } #ifdef USE_OPENCV cv::Mat img_ocv0( outImg.props.height, outImg.props.width, CV_8UC3, outImg.plane[0], outImg.props.stride[0] ); cv::Mat img_display0( outImg.props.height, outImg.props.width, CV_8UC3 ); cv::cvtColor( img_ocv0, img_display0, cv::COLOR_RGB2BGR ); cv::namedWindow( "outImg", cv::WINDOW_AUTOSIZE ); cv::imshow( "outImg", img_display0 ); #endif /**************************************************************************/ // Ex. #2: ROI scaling plus inherent map scaling for constant sized output // into a fixed-size DL network (e.g., QAIC) uint32_t dl_sz = 128; FadasImage_t roiImg = { 0 }; float32_t* mapXRoiScl = (float32_t*)FadasMemAlloc( map_s * map_h, balign, nullptr ); float32_t* mapYRoiScl = (float32_t*)FadasMemAlloc( map_s * map_h, balign, nullptr ); if( (nullptr == mapXRoiScl) || (nullptr == mapYRoiScl) ) { UTIL_ERROR( "Failed to allocate MAP buffer." ); FadasDestroyImage(&outImg); FadasRemap_DestroyMap(map); FadasMemFree(mapX); FadasMemFree(mapY); FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } memset( mapXRoiScl, 0, map_s * map_h ); // optional memset( mapYRoiScl, 0, map_s * map_h ); // optional UTIL_ReadRAW( "mapX.data", map_s, map_h, map_s, mapXRoiScl ); UTIL_ReadRAW( "mapY.data", map_s, map_h, map_s, mapYRoiScl ); status = FadasCreateImage( FADAS_BUF_TYPE_INOUT, dl_sz, dl_sz, FADAS_IMAGE_FORMAT_RGB888, &roiImg, balign ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasCreateImage failed (status = %u)", status ); FadasDestroyImage(&outImg); FadasRemap_DestroyMap(map); FadasMemFree(mapX); FadasMemFree(mapY); FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } // Build a special map that includes the notion of ROI scaling if using the optional roiScale // parameter in Run and RunMT calls FadasRemapMap_t* mapRoiScl = FadasRemap_CreateMapFromMap( uyvy_w, uyvy_h, map_w, map_h, map_s, mapXRoiScl, mapYRoiScl, FADAS_REMAP_PIPELINE_UYVY_TO_RGB888_ROISCALE, 0 ); if( nullptr == mapRoiScl ) { UTIL_ERROR( "Failed to create map!\n" ); FadasDestroyImage(&roiImg); FadasDestroyImage(&outImg); FadasRemap_DestroyMap(map); FadasMemFree(mapX); FadasMemFree(mapY); FadasDestroyImage(&uyvyImg); FadasDeInit(); return -1; } // Virtual output image size (994, 512) due to inherent map scaling. ROI is relative to those sizes. float32_t roi_scl = 4.0; // Must be <= (map_h / dl_sz) roi = { ( map_w - uint32_t(roi_scl * dl_sz) ) - 1, 0, dl_sz, dl_sz }; // {ULx, ULy, w, h} #ifdef ST status = FadasRemap_Run( mapRoiScl, &uyvyImg, &roiImg, &roi, roi_scl ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_Run failed (status = %u)", status ); retVal = -1; } #else wrkrs = FadasRemap_CreateWorkers( 4, n_threads_affinity, FADAS_REMAP_PIPELINE_UYVY_TO_RGB888_ROISCALE ); if( nullptr == wrkrs ) { UTIL_ERROR( "creating wrkrs 0" ); retVal = -1; } else { status = FadasRemap_RunMT( wrkrs, mapRoiScl, &uyvyImg, &roiImg, &roi, roi_scl ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_Run MT failed (status = %u)", status ); retVal = -1; } status = FadasRemap_DestroyWorkers( wrkrs ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_DestroyWorkers failed (status = %u)", status ); retVal = -1; } } #endif if( 0 == retVal ) { char outfile[128] = {0}; snprintf( outfile, 128, "img4_remap_nrm_roiscl.ppm" ); UTIL_WritePPM( outfile, roiImg.props.width, roiImg.props.height, 255, (uint8_t *)roiImg.plane[0], roiImg.props.stride[0]); } #ifdef USE_OPENCV cv::Mat img_ocv1( roiImg.props.height, roiImg.props.width, CV_8UC3, roiImg.plane[0], roiImg.props.stride[0] ); cv::Mat img_display1( roiImg.props.height, roiImg.props.width, CV_8UC3 ); cv::cvtColor( img_ocv1, img_display1, cv::COLOR_RGB2BGR ); cv::namedWindow( "roiImg", cv::WINDOW_AUTOSIZE ); cv::imshow( "roiImg", img_display1 ); cv::waitKey( 0 ); #endif status = FadasRemap_DestroyMap( mapRoiScl ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_DestroyMap failed with error %u", status ); retVal = -1; } status = FadasRemap_DestroyMap( map ); if( FADAS_ERROR_NONE != status ) { UTIL_ERROR( "FadasRemap_DestroyMap failed with error %u", status ); retVal = -1; } FadasMemFree( mapX ); FadasMemFree( mapY ); FadasMemFree( mapXRoiScl ); FadasMemFree( mapYRoiScl ); FadasDestroyImage( &uyvyImg ); FadasDestroyImage( &outImg ); FadasDestroyImage( &roiImg ); FadasDeInit(); return retVal; } Copy to clipboard Last Published: Sep 30, 2024 [Previous Topic fpt\_trcks/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/fpt-trcks.md) [Next Topic vyuy\_remap/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/vyuy-remap.md)