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"
15 int main(int argc, const char* argv[])
19 std::cerr << "Usage : video_writer <input video file>" << std::endl;
23 const double FPS = 25.0;
25 cv::VideoCapture reader(argv[1]);
27 if (!reader.isOpened())
29 std::cerr << "Can't open input video file" << std::endl;
33 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
35 cv::VideoWriter writer;
36 cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
39 cv::cuda::GpuMat d_frame;
41 std::vector<double> cpu_times;
42 std::vector<double> gpu_times;
47 std::cout << "Read " << i << " frame" << std::endl;
53 std::cout << "Stop" << std::endl;
57 if (!writer.isOpened())
59 std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
61 std::cout << "Open CPU Writer" << std::endl;
63 if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
69 std::cout << "Open CUDA Writer" << std::endl;
71 const cv::String outputFilename = "output_gpu.avi";
72 d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS);
75 d_frame.upload(frame);
77 std::cout << "Write " << i << " frame" << std::endl;
79 tm.reset(); tm.start();
82 cpu_times.push_back(tm.getTimeMilli());
84 tm.reset(); tm.start();
85 d_writer->write(d_frame);
87 gpu_times.push_back(tm.getTimeMilli());
90 std::cout << std::endl << "Results:" << std::endl;
92 std::sort(cpu_times.begin(), cpu_times.end());
93 std::sort(gpu_times.begin(), gpu_times.end());
95 double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
96 double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
98 std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
99 std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
108 std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;