2012-10-17 15:12:04 +08:00
|
|
|
#include <iostream>
|
2013-06-04 17:32:35 +08:00
|
|
|
|
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
2013-07-23 18:29:21 +08:00
|
|
|
#if defined(HAVE_OPENCV_CUDACODEC) && defined(WIN32)
|
2013-06-04 17:32:35 +08:00
|
|
|
|
2012-10-17 15:12:04 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <numeric>
|
|
|
|
|
2013-06-04 17:32:35 +08:00
|
|
|
#include "opencv2/core.hpp"
|
2013-07-23 18:29:21 +08:00
|
|
|
#include "opencv2/cudacodec.hpp"
|
2013-06-04 17:32:35 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/contrib.hpp"
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
int main(int argc, const char* argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage : video_writer <input video file>" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double FPS = 25.0;
|
|
|
|
|
|
|
|
cv::VideoCapture reader(argv[1]);
|
|
|
|
|
|
|
|
if (!reader.isOpened())
|
|
|
|
{
|
|
|
|
std::cerr << "Can't open input video file" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-26 15:34:37 +08:00
|
|
|
cv::cuda::printShortCudaDeviceInfo(cv::cuda::getDevice());
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
cv::VideoWriter writer;
|
2013-07-23 16:33:11 +08:00
|
|
|
cv::Ptr<cv::cudacodec::VideoWriter> d_writer;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
cv::Mat frame;
|
2013-08-26 15:34:37 +08:00
|
|
|
cv::cuda::GpuMat d_frame;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
|
|
|
std::vector<double> cpu_times;
|
|
|
|
std::vector<double> gpu_times;
|
|
|
|
cv::TickMeter tm;
|
|
|
|
|
|
|
|
for (int i = 1;; ++i)
|
|
|
|
{
|
|
|
|
std::cout << "Read " << i << " frame" << std::endl;
|
|
|
|
|
|
|
|
reader >> frame;
|
|
|
|
|
|
|
|
if (frame.empty())
|
|
|
|
{
|
|
|
|
std::cout << "Stop" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!writer.isOpened())
|
|
|
|
{
|
|
|
|
std::cout << "Frame Size : " << frame.cols << "x" << frame.rows << std::endl;
|
|
|
|
|
|
|
|
std::cout << "Open CPU Writer" << std::endl;
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
if (!writer.open("output_cpu.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), FPS, frame.size()))
|
2012-10-17 15:12:04 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:04:27 +08:00
|
|
|
if (d_writer.empty())
|
2012-10-17 15:12:04 +08:00
|
|
|
{
|
2013-07-24 17:55:18 +08:00
|
|
|
std::cout << "Open CUDA Writer" << std::endl;
|
2012-10-17 15:12:04 +08:00
|
|
|
|
2013-07-23 16:33:11 +08:00
|
|
|
d_writer = cv::cudacodec::createVideoWriter("output_gpu.avi", frame.size(), FPS);
|
2012-10-17 15:12:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
d_frame.upload(frame);
|
|
|
|
|
|
|
|
std::cout << "Write " << i << " frame" << std::endl;
|
|
|
|
|
|
|
|
tm.reset(); tm.start();
|
|
|
|
writer.write(frame);
|
|
|
|
tm.stop();
|
|
|
|
cpu_times.push_back(tm.getTimeMilli());
|
|
|
|
|
|
|
|
tm.reset(); tm.start();
|
2013-04-22 18:04:27 +08:00
|
|
|
d_writer->write(d_frame);
|
2012-10-17 15:12:04 +08:00
|
|
|
tm.stop();
|
|
|
|
gpu_times.push_back(tm.getTimeMilli());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl << "Results:" << std::endl;
|
|
|
|
|
|
|
|
std::sort(cpu_times.begin(), cpu_times.end());
|
|
|
|
std::sort(gpu_times.begin(), gpu_times.end());
|
|
|
|
|
|
|
|
double cpu_avg = std::accumulate(cpu_times.begin(), cpu_times.end(), 0.0) / cpu_times.size();
|
|
|
|
double gpu_avg = std::accumulate(gpu_times.begin(), gpu_times.end(), 0.0) / gpu_times.size();
|
|
|
|
|
|
|
|
std::cout << "CPU [XVID] : Avg : " << cpu_avg << " ms FPS : " << 1000.0 / cpu_avg << std::endl;
|
|
|
|
std::cout << "GPU [H264] : Avg : " << gpu_avg << " ms FPS : " << 1000.0 / gpu_avg << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-04 17:32:35 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-07-24 17:55:18 +08:00
|
|
|
std::cout << "OpenCV was built without CUDA Video encoding support\n" << std::endl;
|
2013-06-04 17:32:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|