3 #include "opencv2/opencv_modules.hpp"
5 #if defined(HAVE_OPENCV_CUDACODEC) && defined(WIN32)
10 #include "opencv2/core.hpp"
11 #include "opencv2/cudacodec.hpp"
12 #include "opencv2/highgui.hpp"
14 int main(int argc, const char* argv[])
18 std::cerr << "Usage : video_writer <input video file>" << std::endl;
22 const double FPS = 25.0;
24 cv::VideoCapture reader(argv[1]);
26 if (!reader.isOpened())
28 std::cerr << "Can't open input video file" << std::endl;
32 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
34 cv::VideoWriter writer;
35 cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
38 cv::cuda::GpuMat d_frame;
40 std::vector<double> cpu_times;
41 std::vector<double> gpu_times;
46 std::cout << "Read " << i << " frame" << std::endl;
52 std::cout << "Stop" << std::endl;
56 if (!writer.isOpened())
58 std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
60 std::cout << "Open CPU Writer" << std::endl;
62 if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
68 std::cout << "Open CUDA Writer" << std::endl;
70 const cv::String outputFilename = "output_gpu.avi";
71 d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS);
74 d_frame.upload(frame);
76 std::cout << "Write " << i << " frame" << std::endl;
78 tm.reset(); tm.start();
81 cpu_times.push_back(tm.getTimeMilli());
83 tm.reset(); tm.start();
84 d_writer->write(d_frame);
86 gpu_times.push_back(tm.getTimeMilli());
89 std::cout << std::endl << "Results:" << std::endl;
91 std::sort(cpu_times.begin(), cpu_times.end());
92 std::sort(gpu_times.begin(), gpu_times.end());
94 double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
95 double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
97 std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
98 std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
107 std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;