main.cpp 2.61 KB
#include "../DxDecoder/DxDecoderWrap.h"
#include "../DxDecoder/ColorSpace.h"

#include <thread>
#include <iostream>

static void process_thread();

DxDecoderWrap *taskTcuvid ;

using namespace std;

int main() {
    int AddTaskSucFlag = 0;
    
    DxConfig cfg = { 0 };
	cfg.devId = 0;
	cfg.decMode = 0;
	cfg.colorFmt = 0;
	cfg.forceTcp = false;
	cfg.type = DX_DECODER_TYPE_SFXLAB;
	taskTcuvid = new DxDecoderWrap(&cfg);
	if (NULL == taskTcuvid)
	{
		printf("Add New DxDecoder Failed!");
		AddTaskSucFlag = -1;
		return false;
	}

    int skip_frame_ = 0;
	if (taskTcuvid->DxOpenDecoder("file:///home/cmhu/data/video/Street.uvf?fastdecode=on", skip_frame_) != 0)
	{
		cout << "Add Task Failed! Please check you video file name!" << endl;
		delete taskTcuvid;
		taskTcuvid = NULL;
		AddTaskSucFlag = -1;
		return false;
	}

	int tmp_total_frame = taskTcuvid->DxGetFrameCount();

	printf("finish add codec. tmp_total_frame: %d \n", tmp_total_frame);

    std::thread* m_psThreadPtr = new std::thread(process_thread);

    while (getchar() == 'q');

    return 0;
}

void process_thread() {
    DxGPUFrame frame = {};
    DxGPUFrame task_algorithm_data = {};

    while (true)
    {
        if (taskTcuvid->DxLockFrame(&frame) == 0)
        {
            if (!task_algorithm_data.frame && frame.width > 0 && frame.height > 0)
            {
                cudaError_t cudaStatus = cudaMalloc((void**)&task_algorithm_data.frame, 3 * frame.size * frame.height * sizeof(unsigned char));
                if (cudaStatus != cudaSuccess) {
                    fprintf(stderr, "here cudaMalloc m_pRGBData[0] failed! error: %s\n", cudaGetErrorString(cudaStatus));
                    break;
                }

                task_algorithm_data.height = frame.height;
                task_algorithm_data.width = frame.width;
                task_algorithm_data.size = frame.size;
            }

            //copy decode data
            if (task_algorithm_data.frame)
            {
                int height = frame.height;
                int width = frame.width;

                Nv12ToColor24<BGR24>( (unsigned char *)frame.frame, width, (unsigned char *)task_algorithm_data.frame, 3 * width, width, height, 0 );

                task_algorithm_data.timestamp = frame.timestamp;

                 cout << "帧号:" << task_algorithm_data.timestamp << endl;
            }
            else
            {
                std::cout << "NOT ALLOC: taskDataToBackup.frame" << task_algorithm_data.frame << std::endl;
            }

            taskTcuvid->DxUnlockFrame();
        } else {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    
}