# uyuv\_nv12\_y/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 camera conversion helper function to convert from
    UYVY to NV12 and then downscale the Y channel for monochromatic algorithms
    like feature detection and tracking to use.  NV12 is used as an intermediate
    format in this example for cases feeding into the hardware video encoder.
    *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <fadas.h>
    #include "util.h"

    int main( int argc, char** argv )
    {
        // one-time initializations
    
        if( FADAS_ERROR_NONE != FadasInit( nullptr ) )
            UTIL_ERROR( "FAILED:  FadasInit" );
    
        // initialize UYVY image memory
        FadasImage_t uyvyImg = { 0 };
        uyvyImg.props.format = FADAS_IMAGE_FORMAT_UYVY;
        uyvyImg.props.width = 1920;
        uyvyImg.props.height = 1020;
        uyvyImg.props.stride[0] = FADAS_MODULO128( 2 * uyvyImg.props.width );
        uyvyImg.plane[0] = FadasMemAlloc( uyvyImg.props.stride[0] * uyvyImg.props.height, 128 );
        uyvyImg.bAllocated = true;  // WARNING: false if memory allocated elsewhere and not the owner
        if( FadasRegBuf( FADAS_BUF_TYPE_IN, uyvyImg.plane[0], uyvyImg.props.stride[0] * uyvyImg.props.height ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "Error registering buffer (uyvyImg)\n" );
    
        // initialize NV12 image memory
        FadasImage_t nv12Img = { 0 };
        nv12Img.props.format = FADAS_IMAGE_FORMAT_Y8UV8;
        nv12Img.props.width = uyvyImg.props.width;
        nv12Img.props.height = uyvyImg.props.height;
        nv12Img.props.stride[0] = FADAS_MODULO128( nv12Img.props.width );
        nv12Img.props.stride[1] = FADAS_MODULO128( nv12Img.props.width );
        nv12Img.plane[0] = FadasMemAlloc( nv12Img.props.stride[0] * nv12Img.props.height, 128 );
        nv12Img.plane[1] = FadasMemAlloc( nv12Img.props.stride[1] * nv12Img.props.height, 128 );
        nv12Img.bAllocated = true;  // WARNING: false if memory allocated elsewhere and not the owner
        if( FadasRegBuf( FADAS_BUF_TYPE_INOUT, nv12Img.plane[0], nv12Img.props.stride[0] * nv12Img.props.height ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "Error registering buffer (nv12Img.0)\n" );
    
        if( FadasRegBuf( FADAS_BUF_TYPE_INOUT, nv12Img.plane[1], nv12Img.props.stride[1] * nv12Img.props.height ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "Error registering buffer (nv12Img.1)\n" );
    
     // initialize Y image source memory
        FadasImage_t yImgSrc = { 0 };
        yImgSrc.props.format = FADAS_IMAGE_FORMAT_Y;
        yImgSrc.props.width = nv12Img.props.width / 2;
        yImgSrc.props.height = nv12Img.props.height / 2;
        yImgSrc.props.stride[0] = FADAS_MODULO128( nv12Img.props.width );
        yImgSrc.plane[0] = FadasMemAlloc( yImgSrc.props.stride[0] * yImgSrc.props.height, 128 );
        yImgSrc.bAllocated = true;  // WARNING: false if memory allocated elsewhere and not the owner
        if( FadasRegBuf( FADAS_BUF_TYPE_OUT, yImgSrc.plane[0], yImgSrc.props.stride[0] * yImgSrc.props.height ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "Error registering buffer (yImgSrc)\n" );
    
        // initialize Y image output memory
        FadasImage_t yImgOut = { 0 };
        yImgOut.props.format = FADAS_IMAGE_FORMAT_Y;
        yImgOut.props.width = nv12Img.props.width / 2;
        yImgOut.props.height = nv12Img.props.height / 2;
        yImgOut.props.stride[0] = FADAS_MODULO128( nv12Img.props.width );
        yImgOut.plane[0] = FadasMemAlloc( yImgOut.props.stride[0] * yImgOut.props.height, 128 );
        yImgOut.bAllocated = true;  // WARNING: false if memory allocated elsewhere and not the owner
        if( FadasRegBuf( FADAS_BUF_TYPE_OUT, yImgOut.plane[0], yImgOut.props.stride[0] * yImgOut.props.height ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "Error registering buffer (yImgOut)\n" );

        // fake camera loop
        {
            UTIL_ReadRAW( "img1.uyvy", (2 * uyvyImg.props.width), uyvyImg.props.height, uyvyImg.props.stride[0], (uint8_t*)uyvyImg.plane[0] );

            if( FadasCvtYUV_UYVYtoNV12( (uint8_t*)uyvyImg.plane[0], uyvyImg.props,
                                        (uint8_t*)nv12Img.plane[0], nv12Img.props.stride[0],
                                        (uint8_t*)nv12Img.plane[1], nv12Img.props.stride[1] ) != FADAS_ERROR_NONE )
                UTIL_ERROR( "FadasCvtYUV_UYVYtoNV12 failed" );

            if( FadasCvtYUV_DownscaleBy2( (uint8_t*)nv12Img.plane[0], yImgSrc.props, (uint8_t*)yImgOut.plane[0], &yImgOut.props ) != FADAS_ERROR_NONE )
                UTIL_ERROR( "FadasCvtYUV_DownscaleBy2 failed" );

            UTIL_WritePGM( "img1.pgm", yImgOut.props.width, yImgOut.props.height, 255, (uint8_t*)yImgOut.plane[0], yImgOut.props.stride[0] );
        }

        // cleanup
        FadasDeregBuf( uyvyImg.plane[0] );
        FadasDeregBuf( nv12Img.plane[0] );
        FadasDeregBuf( nv12Img.plane[1] );
        FadasDeregBuf( yImgSrc.plane[0] );
        FadasDeregBuf( yImgOut.plane[0] );
    
        FadasMemFree( uyvyImg.plane[0] );
        FadasMemFree( nv12Img.plane[0] );
        FadasMemFree( nv12Img.plane[1] );
        FadasMemFree( yImgSrc.plane[0] );
        FadasMemFree( yImgOut.plane[0] );
    
        FadasDeInit();
    
        return 0;
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
uyvy/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/uyvy.md) [Next Topic
fpe/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/fpe.md)