void CudaVideoRender::setVideoFile(LPCWSTR a_pVideoFilename)
	{
		m_sFileName=a_pVideoFilename;

		//char* video_file=new char[MAX_PATH];
		//WideCharToMultiByte( CP_ACP, 0, a_pVideoFilename, -1, video_file, MAX_PATH, NULL, NULL );

		// Find out the video size 
		m_bIsProgressive = loadVideoSource(a_pVideoFilename, 
			m_nVideoWidth, m_nVideoHeight, 
			m_nWindowWidth, m_nWindowHeight );
		//delete video_file;

		// Initialize CUDA
		//TODO: dont init cuda twice??
		cuInit(0);

		int bTCC = 0;
		// If we are using TCC driver, then always turn off interop
		if (bTCC) m_bInterop = false;

		// Initialize CUDA/D3D9 context and other video memory resources
		initCudaResources(m_bInterop, bTCC);

		m_pVideoSource->start();
	}
Esempio n. 2
0
int main(int argc, char *argv[])
{
    pArgc = &argc;
    pArgv = argv;

    sdkCreateTimer(&frame_timer);
    sdkResetTimer(&frame_timer);

    sdkCreateTimer(&global_timer);
    sdkResetTimer(&global_timer);

    // parse the command line arguments
    parseCommandLineArguments(argc, argv);

    // create window (after we know the size of the input file size)
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      sAppName, NULL
                    };
    RegisterClassEx(&wc);

    // Find out the video size
    g_bIsProgressive = loadVideoSource(sFileName.c_str(),
                                       g_nVideoWidth, g_nVideoHeight,
                                       g_nWindowWidth, g_nWindowHeight);

    // figure out the window size we must create to get a *client* area
    // that is of the size requested by m_dimensions.
    RECT adjustedWindowSize;
    DWORD dwWindowStyle;
    HWND hWnd = NULL;

    {
        dwWindowStyle = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
        SetRect(&adjustedWindowSize, 0, 0, g_nVideoWidth  , g_nVideoHeight);
        AdjustWindowRect(&adjustedWindowSize, dwWindowStyle, false);

        g_nWindowWidth  = adjustedWindowSize.right  - adjustedWindowSize.left;
        g_nWindowHeight = adjustedWindowSize.bottom - adjustedWindowSize.top;

        // Create the application's window
        hWnd = CreateWindow(wc.lpszClassName, sAppName,
                            dwWindowStyle,
                            0, 0,
                            g_nWindowWidth,
                            g_nWindowHeight,
                            NULL, NULL, wc.hInstance, NULL);
    }

    // Initialize CUDA
    cuInit(0);

    int bTCC = 0;

    // If we are using TCC driver, then always turn off interop
    if (bTCC)
    {
        g_bUseInterop = false;
    }

    if (g_bUseInterop)
    {
        // Initialize Direct3D
        if (initD3D9(hWnd, argc, argv, &bTCC) == false)
        {
            g_bAutoQuit = true;
            g_bWaived   = true;
            goto ExitApp;
        }
    }

    // Initialize CUDA/D3D9 context and other video memory resources
    if (initCudaResources(argc, argv, g_bUseInterop, bTCC) == E_FAIL)
    {
        g_bAutoQuit  = true;
        g_bException = true;
        g_bWaived    = true;
        goto ExitApp;
    }

    g_pVideoSource->start();
    g_bRunning = true;

    if (!g_bQAReadback && !bTCC)
    {
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);
    }

    // the main loop
    sdkStartTimer(&frame_timer);
    sdkStartTimer(&global_timer);
    sdkResetTimer(&global_timer);

    if (!g_bUseInterop)
    {
        // On this case we drive the display with a while loop (no openGL calls)
        while (g_pVideoSource->isStarted() && !g_pFrameQueue->isEndOfDecode())
        {
            renderVideoFrame(hWnd, g_bUseInterop);
        }
    }
    else
    {
        // Standard windows loop
        while (!g_bDone)
        {
            MSG msg;
            ZeroMemory(&msg, sizeof(msg));

            while (msg.message!=WM_QUIT)
            {
                if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
                else
                {
                    renderVideoFrame(hWnd, g_bUseInterop);
                }

                if (g_bAutoQuit && g_bDone)
                {
                    break;
                }
            }
        } // while loop
    }

    g_pFrameQueue->endDecode();
    g_pVideoSource->stop();

    printStatistics();

    // clean up CUDA and D3D resources
ExitApp:
    cleanup(g_bWaived ? false : true);

    if (!g_bQAReadback)
    {
        // Unregister windows class
        UnregisterClass(wc.lpszClassName, wc.hInstance);
    }

    if (g_bAutoQuit)
    {
        PostQuitMessage(0);
    }

    if (hWnd)
    {
        DestroyWindow(hWnd);
    }

    if (g_bWaived)
    {
        exit(EXIT_WAIVED);
    }
    else
    {
        exit(g_bException ? EXIT_FAILURE : EXIT_SUCCESS);
    }
}
Esempio n. 3
0
// Draw the final result on the screen
HRESULT drawScene(int field_num)
{
    HRESULT hr = S_OK;

    // Begin code to handle case where the D3D gets lost
    if (g_bDeviceLost)
    {
        // test the cooperative level to see if it's okay
        // to render
        if (FAILED(hr = g_pD3DDevice->TestCooperativeLevel()))
        {
            fprintf(stderr, "TestCooperativeLevel = %08x failed, will attempt to reset\n", hr);

            // if the device was truly lost, (i.e., a fullscreen device just lost focus), wait
            // until we get it back

            if (hr == D3DERR_DEVICELOST)
            {
                fprintf(stderr, "TestCooperativeLevel = %08x DeviceLost, will retry next call\n", hr);
                return S_OK;
            }

            // eventually, we will get this return value,
            // indicating that we can now reset the device
            if (hr == D3DERR_DEVICENOTRESET)
            {
                fprintf(stderr, "TestCooperativeLevel = %08x will try to RESET the device\n", hr);
                // if we are windowed, read the desktop mode and use the same format for
                // the back buffer; this effectively turns off color conversion

                if (g_bWindowed)
                {
                    g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &g_d3ddm);
                    g_d3dpp.BackBufferFormat = g_d3ddm.Format;
                }

                // now try to reset the device
                if (FAILED(hr = g_pD3DDevice->Reset(&g_d3dpp)))
                {
                    fprintf(stderr, "TestCooperativeLevel = %08x RESET device FAILED\n", hr);
                    return hr;
                }
                else
                {
                    fprintf(stderr, "TestCooperativeLevel = %08x RESET device SUCCESS!\n", hr);
                    // Reinit All other resources including CUDA contexts
                    initCudaResources(0, NULL, true, false);
                    fprintf(stderr, "TestCooperativeLevel = %08x INIT device SUCCESS!\n", hr);

                    // we have acquired the device
                    g_bDeviceLost = false;
                }
            }
        }
    }

    // Normal D3D9 rendering code
    if (!g_bDeviceLost)
    {

        // init the scene
        if (g_bUseDisplay)
        {
            g_pD3DDevice->BeginScene();
            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0);
            // render image
            g_pImageDX->render(field_num);
            // end the scene
            g_pD3DDevice->EndScene();
        }

        hr = g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

        if (hr == D3DERR_DEVICELOST)
        {
            fprintf(stderr, "drawScene Present = %08x detected D3D DeviceLost\n", hr);
            g_bDeviceLost = true;

            // We check for DeviceLost, if that happens we want to release resources
            g_pFrameQueue->endDecode();
            g_pVideoSource->stop();

            freeCudaResources(false);
        }
    }
    else
    {
        fprintf(stderr, "drawScene (DeviceLost == TRUE), waiting\n");
    }

    return S_OK;
}