Merge remote-tracking branch 'origin/2.4' into merge-2.4
[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 #include "opencv2/contrib.hpp"
14
15 int main(int argc, const char* argv[])
16 {
17     if (argc != 2)
18     {
19         std::cerr << "Usage : video_writer <input video file>" << std::endl;
20         return -1;
21     }
22
23     const double FPS = 25.0;
24
25     cv::VideoCapture reader(argv[1]);
26
27     if (!reader.isOpened())
28     {
29         std::cerr << "Can't open input video file" << std::endl;
30         return -1;
31     }
32
33     cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
34
35     cv::VideoWriter writer;
36     cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
37
38     cv::Mat frame;
39     cv::cuda::GpuMat d_frame;
40
41     std::vector<double> cpu_times;
42     std::vector<double> gpu_times;
43     cv::TickMeter tm;
44
45     for (int i = 1;; ++i)
46     {
47         std::cout << "Read " << i << " frame" << std::endl;
48
49         reader >> frame;
50
51         if (frame.empty())
52         {
53             std::cout << "Stop" << std::endl;
54             break;
55         }
56
57         if (!writer.isOpened())
58         {
59             std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
60
61             std::cout << "Open CPU Writer" << std::endl;
62
63             if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
64                 return -1;
65         }
66
67         if (d_writer.empty())
68         {
69             std::cout << "Open CUDA Writer" << std::endl;
70
71             d_writer = cv::cudacodec::createVideoWriter("output_gpu.avi", frame.size(), FPS);
72         }
73
74         d_frame.upload(frame);
75
76         std::cout << "Write " << i << " frame" << std::endl;
77
78         tm.reset(); tm.start();
79         writer.write(frame);
80         tm.stop();
81         cpu_times.push_back(tm.getTimeMilli());
82
83         tm.reset(); tm.start();
84         d_writer->write(d_frame);
85         tm.stop();
86         gpu_times.push_back(tm.getTimeMilli());
87     }
88
89     std::cout << std::endl << "Results:" << std::endl;
90
91     std::sort(cpu_times.begin(), cpu_times.end());
92     std::sort(gpu_times.begin(), gpu_times.end());
93
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();
96
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;
99
100     return 0;
101 }
102
103 #else
104
105 int main()
106 {
107     std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;
108     return 0;
109 }
110
111 #endif