samples: gpu: removed inclusion of non-existent opencv2/contrib/contrib.hpp header...
[platform/upstream/opencv.git] / samples / gpu / video_writer.cpp
1 #include <iostream>
2
3 #include "opencv2/opencv_modules.hpp"
4
5 #if defined(HAVE_OPENCV_CUDACODEC) && defined(WIN32)
6
7 #include <vector>
8 #include <numeric>
9
10 #include "opencv2/core.hpp"
11 #include "opencv2/cudacodec.hpp"
12 #include "opencv2/highgui.hpp"
13
14 int main(int argc, const char* argv[])
15 {
16     if (argc != 2)
17     {
18         std::cerr << "Usage : video_writer <input video file>" << std::endl;
19         return -1;
20     }
21
22     const double FPS = 25.0;
23
24     cv::VideoCapture reader(argv[1]);
25
26     if (!reader.isOpened())
27     {
28         std::cerr << "Can't open input video file" << std::endl;
29         return -1;
30     }
31
32     cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
33
34     cv::VideoWriter writer;
35     cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
36
37     cv::Mat frame;
38     cv::cuda::GpuMat d_frame;
39
40     std::vector<double> cpu_times;
41     std::vector<double> gpu_times;
42     cv::TickMeter tm;
43
44     for (int i = 1;; ++i)
45     {
46         std::cout << "Read " << i << " frame" << std::endl;
47
48         reader >> frame;
49
50         if (frame.empty())
51         {
52             std::cout << "Stop" << std::endl;
53             break;
54         }
55
56         if (!writer.isOpened())
57         {
58             std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
59
60             std::cout << "Open CPU Writer" << std::endl;
61
62             if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
63                 return -1;
64         }
65
66         if (d_writer.empty())
67         {
68             std::cout << "Open CUDA Writer" << std::endl;
69
70             d_writer = cv::cudacodec::createVideoWriter("output_gpu.avi", frame.size(), FPS);
71         }
72
73         d_frame.upload(frame);
74
75         std::cout << "Write " << i << " frame" << std::endl;
76
77         tm.reset(); tm.start();
78         writer.write(frame);
79         tm.stop();
80         cpu_times.push_back(tm.getTimeMilli());
81
82         tm.reset(); tm.start();
83         d_writer->write(d_frame);
84         tm.stop();
85         gpu_times.push_back(tm.getTimeMilli());
86     }
87
88     std::cout << std::endl << "Results:" << std::endl;
89
90     std::sort(cpu_times.begin(), cpu_times.end());
91     std::sort(gpu_times.begin(), gpu_times.end());
92
93     double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
94     double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
95
96     std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
97     std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
98
99     return 0;
100 }
101
102 #else
103
104 int main()
105 {
106     std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;
107     return 0;
108 }
109
110 #endif