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 #include "tick_meter.hpp"
16 int main(int argc, const char* argv[])
20 std::cerr << "Usage : video_writer <input video file>" << std::endl;
24 const double FPS = 25.0;
26 cv::VideoCapture reader(argv[1]);
28 if (!reader.isOpened())
30 std::cerr << "Can't open input video file" << std::endl;
34 cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
36 cv::VideoWriter writer;
37 cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
40 cv::cuda::GpuMat d_frame;
42 std::vector<double> cpu_times;
43 std::vector<double> gpu_times;
48 std::cout << "Read " << i << " frame" << std::endl;
54 std::cout << "Stop" << std::endl;
58 if (!writer.isOpened())
60 std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
62 std::cout << "Open CPU Writer" << std::endl;
64 if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
70 std::cout << "Open CUDA Writer" << std::endl;
72 const cv::String outputFilename = "output_gpu.avi";
73 d_writer = cv::cudacodec::createVideoWriter(outputFilename, frame.size(), FPS);
76 d_frame.upload(frame);
78 std::cout << "Write " << i << " frame" << std::endl;
80 tm.reset(); tm.start();
83 cpu_times.push_back(tm.getTimeMilli());
85 tm.reset(); tm.start();
86 d_writer->write(d_frame);
88 gpu_times.push_back(tm.getTimeMilli());
91 std::cout << std::endl << "Results:" << std::endl;
93 std::sort(cpu_times.begin(), cpu_times.end());
94 std::sort(gpu_times.begin(), gpu_times.end());
96 double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
97 double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
99 std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
100 std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
109 std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;