YUV422转BGR888(海思3516平台)


#include "hi_comm_ive.h"
#include "mpi_ive.h"

#pragma pack(1)

typedef struct{
    short type;
    int size;
    short reserved1;
    short reserved2;
    int offset;
} BMPHeader;

typedef struct{
    int size;
    int width;
    int height;
    short planes;
    short bitsPerPixel;
    unsigned compression;
    unsigned imageSize;
    int xPelsPerMeter;
    int yPelsPerMeter;
    int clrUsed;
    int clrImportant;
} BMPInfoHeader;

#pragma pack()


int saveBMPFile(unsigned char* src, int width, int height, const char* name){
	BMPHeader hdr;
	BMPInfoHeader infoHdr;
    int ret = 0;

	FILE* fp = NULL;
    if(NULL == src) 
    {
		return (-1);
    }

    fp = fopen(name,"wb");
	if(NULL == fp)
    {
		printf("saveBMPFile: Err: Open!\n");
		return (-1);
	}

	infoHdr.size	= sizeof(BMPInfoHeader);
	infoHdr.width	= width;
	infoHdr.height	= 0 - height;
	infoHdr.planes	= 1;
	infoHdr.bitsPerPixel	= 24;
	infoHdr.compression		= 0;
	infoHdr.imageSize		= width*height;
	infoHdr.xPelsPerMeter	= 0;
	infoHdr.yPelsPerMeter	= 0;
	infoHdr.clrUsed			= 0;
	infoHdr.clrImportant	= 0;

	hdr.type	= 0x4D42;
	hdr.size	= 54 + infoHdr.imageSize*3;
	hdr.reserved1	= 0;
	hdr.reserved2	= 0;
	hdr.offset	= 54;


    //BGR2RGB
    int i;
    int temp;
    for(i = 0;i < width*height;i++)
    {
        temp = src[i*3];
        src[i*3] = src[i*3 + 2];
        src[i*3 + 2] = temp;
    }

	fwrite(&hdr, sizeof(hdr), 1, fp);
  	fwrite(&infoHdr, sizeof(infoHdr), 1, fp);
 	fwrite(src, sizeof(unsigned char), width*height*3, fp); //TODO:
    fflush(fp);

	if(ferror(fp)){
		printf("saveBMPFile: Err: Unknown!***\n");
	}

	fclose(fp);
    fp = NULL;

	return 0;
}

int main()
{
    HI_VOID *pVirtSrc = NULL;
	HI_VOID *pVirtDst = NULL;
	IVE_SRC_INFO_S stSrc;
	IVE_MEM_INFO_S stDst;
    unsigned int * pImage = NULL;

    stSrc.enSrcFmt = IVE_SRC_FMT_SP422;
    stSrc.stSrcMem.u32Stride = nWidth;
    stSrc.stSrcMem.u32PhyAddr = frameInfo->phyAddr;
    stSrc.u32Width = nWidth;
    stSrc.u32Height = nHeight;

    stDst.u32Stride = nWidth;

    IVE_HANDLE IveHandle;
    HI_BOOL bInstant = HI_TRUE;
    IVE_CSC_CTRL_S stCscCtrl;
    stCscCtrl.enOutFmt = IVE_CSC_OUT_FMT_PACKAGE;
    stCscCtrl.enCscMode = IVE_CSC_MODE_VIDEO_BT601_AND_BT656;

    //s32Ret = HI_MPI_SYS_MmzAlloc_Cached (&pstSrc->stSrcMem.u32PhyAddr,&pVirtSrc,"User", HI_NULL, pstSrc->u32Height * pstSrc->stSrcMem.u32Stride*2);
    //memset(pVirtSrc,1,pstSrc->u32Height * pstSrc->stSrcMem.u32Stride);
    ret = HI_MPI_SYS_MmzAlloc_Cached (&stDst.u32PhyAddr, &pVirtDst,"User", HI_NULL, stSrc.u32Height * stDst.u32Stride);
    //s32Ret = HI_MPI_IVE_DMA(&IveHandle, &stSrc, &stDst, bInstant);
    //HI_MPI_SYS_MmzFree(stSrc.stSrcMem.u32PhyAddr, pVirtSrc);
    HI_MPI_SYS_MmzFree(stDst.u32PhyAddr, pVirtDst);
    ret=HI_MPI_IVE_CSC(&IveHandle,&stSrc,&stDst,&stCscCtrl,bInstant);
    if(ret==HI_FAILURE)
    {
        printf("YUV Convert to RGB failed!\n");
        return HI_FAILURE;
    }

    pImage = HI_MPI_SYS_Mmap(stDst.u32PhyAddr, nLengthImage/2*3);
    if(NULL == pImage)
    {
        msg_print( "HI_MPI_SYS_Mmap err!\n");
        return NVD_FALSE;
    }

    ret = saveBMPFile((Byte*)pImage, nWidth, nHeight, "/mnt/rgb.dib");
    if(0 != ret)
    {
        printf( "saveBMPFile error\n");
        return;
    }
    printf("saveBMPFile done!\n");

    //uninit();
}