⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pptest.cpp

📁 Microsoft WinCE 6.0 BSP FINAL release source code for use with the i.MX27ADS TO2 WCE600_FINAL_MX27_S
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    } else {
        if (bytesRead == 0) {
            g_pKato->Log(PP_ZONE_ERROR, 
                (TEXT("%s: Nothing to read from input file.\r\n"), 
                __WFUNCTION__));
            return TPR_FAIL;
        }
    }

    // Add worker buffers to queue
    bufs.InYBuf= pInputFramePhysAddr;
    bufs.InUBuf= pInputFramePhysAddr + YUV420_U_OFFSET(iInputBytesPerFrame);
    bufs.InVBuf= pInputFramePhysAddr + YUV420_V_OFFSET(iInputBytesPerFrame);
    bufs.OutBuf = pOutputFramePhysAddr;
    PPAddBuffers(hPP, &bufs);

    iCurrentFrame++;

    // Start PP
    PPStart(hPP);

    bFirstFrame = TRUE;

    // Loop through frames of YUV input file
    // For each, call post-processor to convert & resize
    while (1) {
        // Wait for End of Frame
        WaitForSingleObject(g_hPPEOFEvent, INFINITE);

#if (PP_TEST_OUTPUT_FORMAT == PP_TEST_OUTPUT_RGB888)
        if (bFirstFrame) { // Only save first frame
            generateBMP(pOutputFrameVirtAddr, 
                ppData.outputSize.width, ppData.outputSize.height);
        }
#elif (PP_TEST_OUTPUT_FORMAT == PP_TEST_OUTPUT_RGB565)
        // displayRGB565(pOutputFrameVirtAddr, 
        //     ppData.outputSize.width, ppData.outputSize.height);
        memcpy(FrameBufferVirtualAddr, pOutputFrameVirtAddr, iOutputStride * iOutputHeight);
#elif (PP_TEST_OUTPUT_FORMAT == PP_TEST_OUTPUT_YUV422)
        if (!WriteFile(hPpResultFile, (LPVOID)pOutputFrameVirtAddr, 
            iOutputBytesPerFrame, (LPDWORD) &bytesWritten, NULL)) {
            g_pKato->Log(PP_ZONE_ERROR, 
                (TEXT("%s: Could not write post-processing results.\r\n"), 
                __WFUNCTION__));
        }
#endif

        if (bFirstFrame) {
            startTime = GetTickCount();
            bFirstFrame = FALSE;
        }

        // Read timer
        nextFrameRunTime = startTime + (PP_TEST_TIME_INTERVAL * iCurrentFrame);

        currentTime = GetTickCount();

        // Wait until PP_TEST_TIME_INTERVAL ms have 
        // passed to process next frame.
        waitTimeInMilliSec =  nextFrameRunTime - currentTime; 

        if (waitTimeInMilliSec > 0)
            Sleep(waitTimeInMilliSec);

        // Read input file for new frame
        if (!ReadFile(hPpTestFile, pInputFrameVirtAddr, 
            iInputBytesPerFrame, &bytesRead, NULL)) {
            g_pKato->Log(PP_ZONE_ERROR, 
                (TEXT("%s: Read new frame failed.\r\n"), __WFUNCTION__));
            return TPR_FAIL;
        } else {
            if (bytesRead == 0) {
                // Reached end of file...break out
                // For last frame read frame count and compare
                iFrameCountRead = PPGetFrameCount(hPP);
                if (iCurrentFrame != iFrameCountRead) {
                    g_pKato->Log(PP_ZONE_ERROR,
                        (TEXT("%s: Frame counts do not match up. Running count: %x, read count: %x\r\n")), 
                        __WFUNCTION__, iCurrentFrame - 1, iFrameCountRead);
                } else {
                    g_pKato->Log(PP_ZONE_ERROR, 
                        (TEXT("%s: Last frame processed.\r\n"), __WFUNCTION__));
                }
                break;
            }
        }

        // Add buffer to PP queues.
        bufs.InYBuf= pInputFramePhysAddr;
        bufs.InUBuf= pInputFramePhysAddr + YUV420_U_OFFSET(iInputBytesPerFrame);
        bufs.InVBuf= pInputFramePhysAddr + YUV420_V_OFFSET(iInputBytesPerFrame);
        bufs.OutBuf = pOutputFramePhysAddr;
        PPAddBuffers(hPP, &bufs);

        iCurrentFrame++;
    }

    // Stop PP
    PPStop(hPP);

    CloseHandle(hPpTestFile);

#if (PP_TEST_OUTPUT_FORMAT == PP_TEST_OUTPUT_YUV422)
    CloseHandle(hPpResultFile);
#endif

    // Deinitialization
    PPTestDeinit();

    return TPR_PASS;
}

//------------------------------------------------------------------------------
//
// Function: PPTestInit
//
// This function does initialization for post-processor test.
//
// Parameters:
//      None.
//
// Returns:
//      TRUE if successful; FALSE if failed.
//
//------------------------------------------------------------------------------
static BOOL PPTestInit()
{
    PHYSICAL_ADDRESS phyAddr;

    // Open handle to the post-processor
    hPP = PPOpenHandle();
    if (hPP == NULL) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: PPOpenHandle failed!\r\n"), __WFUNCTION__));
        goto _error;
    }

    // Map peripheral physical address to virtual address
    phyAddr.QuadPart = IMAGE_SHARE_FRAMEBUFFER_RAM_PA_START;
    FrameBufferVirtualAddr = (UINT8 *)MmMapIoSpace(phyAddr, 
        PP_TEST_SCREEN_HEIGHT * PP_TEST_SCREEN_WIDTH * 2, FALSE);
    if (FrameBufferVirtualAddr == NULL) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: MmMap unsuccessful!!!!!@!\r\n"), __WFUNCTION__));
        goto _error;
    }

    // Events to signal pin that frame is ready
    g_hPPEOFEvent = CreateEvent(NULL, FALSE, FALSE, PP_EOF_EVENT_NAME);
    if (g_hPPEOFEvent == NULL) {
        g_pKato->Log(PP_ZONE_ERROR,
            (TEXT("%s: CreateEvent failed for PP EOF\r\n"), 
            __WFUNCTION__));
        goto _error;
    }

    return TRUE;
    
_error:
    
    PPTestDeinit();
    
    return FALSE;
    
}

//------------------------------------------------------------------------------
//
// Function: PPTestDeinit
//
// This function does deinitialization for post-processor test.
//
// Parameters:
//      None.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
static void PPTestDeinit()
{
    if (hPP != NULL) {
        PPCloseHandle(hPP);
        hPP = NULL;
    }

    if (FrameBufferVirtualAddr != NULL) {
        MmUnmapIoSpace(FrameBufferVirtualAddr, 
            PP_TEST_SCREEN_HEIGHT * PP_TEST_SCREEN_WIDTH * 2);
        FrameBufferVirtualAddr = NULL;
    }

    if (g_hPPEOFEvent != NULL) {
        CloseHandle(g_hPPEOFEvent);
        g_hPPEOFEvent = NULL;
    }
}

//------------------------------------------------------------------------------
//
// Function: generateBMP
//
// This function creates a BMP file from the PP output.
//
// Parameters:
//      None.
//
// Returns:
//      TRUE if successful; FALSE if failed.
//
//------------------------------------------------------------------------------
static BOOL generateBMP(UINT8 *pImageBuf, INT32 width, INT32 height)
{
    HANDLE hPPOutputFile;
    static wchar_t *fileName = PP_TEST_OUTPUT_FILENAME;
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    DWORD bytesWritten;
    UINT32 iImageBytes;

    // Create structures for a bitmap of RGB888
    iImageBytes = height * width * PP_TEST_OUTPUT_BPP;

    // Fill bitmap structures
    bmfh.bfType = 0x4D42;                           // standard value
    bmfh.bfSize = sizeof(BITMAPFILEHEADER) 
        + sizeof(BITMAPINFOHEADER) + iImageBytes;   // size of the file in bytes
    bmfh.bfReserved1 = 0;                           // reserved
    bmfh.bfReserved2 = 0;                           // reserved
    bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) 
        + sizeof(BITMAPINFOHEADER);                 // offset from beginning of file to bitmap data
    bmih.biSize = sizeof(BITMAPINFOHEADER);         // size of BITMAPINFOHEADER structure in bytes
    bmih.biWidth = width;                           // bitmap width
    bmih.biHeight = -height;                        // bitmap height
    bmih.biPlanes = 1;                              // bitmap planes
    bmih.biBitCount = PP_TEST_OUTPUT_BPP * 8;       // bits per pixel
    bmih.biCompression = BI_RGB;                    // type of compression
    bmih.biSizeImage = iImageBytes;                 // size of image data (0 if no compression)
    bmih.biXPelsPerMeter = 0;
    bmih.biYPelsPerMeter = 0;
    bmih.biClrUsed = 0;
    bmih.biClrImportant= 0;

    g_pKato->Log(PP_ZONE_ERROR, 
        (TEXT("%s: Output file name: %s.\r\n")), __WFUNCTION__, fileName);

    // Open file to write BMP
    hPPOutputFile = CreateFile(
        fileName,                       // "special" file name
        GENERIC_WRITE | GENERIC_READ,   // desired access
        0,                              // sharing mode
        NULL,                           // security attributes (=NULL)
        CREATE_ALWAYS,                  // creation disposition
        FILE_ATTRIBUTE_NORMAL,          // flags and attributes
        NULL);                          // template file (ignored)
    if (hPPOutputFile == INVALID_HANDLE_VALUE) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: Could not open file to write BMP data.\r\n"), 
            __WFUNCTION__));
        goto _error;
    }

    // Write bitmapfileheader
    if (!WriteFile(hPPOutputFile, (LPVOID) &bmfh, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &bytesWritten, NULL)) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: Could not write bit map file header to BMP.\r\n"), 
            __WFUNCTION__));
        goto _error;
    }

    // Write bitmapinfoheader
    if (!WriteFile(hPPOutputFile, (LPVOID) &bmih, sizeof(BITMAPINFOHEADER), 
        (LPDWORD) &bytesWritten, NULL)) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: Could not write bit map info header to BMP.\r\n"), 
            __WFUNCTION__));
        goto _error;
    }

    // Write bitmap info
    if (!WriteFile(hPPOutputFile, (LPVOID) pImageBuf, iImageBytes, 
        (LPDWORD) &bytesWritten, NULL)) {
        g_pKato->Log(PP_ZONE_ERROR, 
            (TEXT("%s: Could not write bit map info header to BMP.\r\n"), 
            __WFUNCTION__));
        goto _error;
    }

    CloseHandle(hPPOutputFile);
    
    return TRUE;

_error:
    
    if (hPPOutputFile != NULL)
        CloseHandle(hPPOutputFile);
    
    return FALSE;
}

//------------------------------------------------------------------------------
//
// Function: displayRGB565
//
// This function displays the post-processing result on LCD.
//
// Parameters:
//      pImageBuf
//          [in] Image to display.
//
//      width
//          [in] Width of the image.
//
//      height
//          [in] Height of the image.
//
// Returns:
//      None.
//
//------------------------------------------------------------------------------
static void displayRGB565(UINT8 *pImageBuf, UINT32 width, UINT32 height)
{
    UINT16 *pFb = (UINT16 *)FrameBufferVirtualAddr;
    UINT16 *pImg = (UINT16 *)pImageBuf;
    UINT32 x, y;

    for (y = 0; y < height; y++)
        for (x = 0; x < width; x++)
            pFb[x * PP_TEST_SCREEN_WIDTH + (height - y)] = pImg[y * width + x];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -