video_acceleration.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include <iostream>
  2. #include <chrono>
  3. #include "opencv2/core.hpp"
  4. #include "opencv2/core/ocl.hpp"
  5. #include "opencv2/core/utility.hpp"
  6. #include "opencv2/imgproc.hpp"
  7. #include "opencv2/videoio.hpp"
  8. #include "opencv2/highgui.hpp"
  9. using namespace cv;
  10. using namespace std;
  11. const char* keys =
  12. "{ i input | | input video file }"
  13. "{ o output | | output video file, or specify 'null' to measure decoding without rendering to screen}"
  14. "{ backend | any | VideoCapture and VideoWriter backend, valid values: 'any', 'ffmpeg', 'msmf', 'gstreamer' }"
  15. "{ accel | any | GPU Video Acceleration, valid values: 'none', 'any', 'd3d11', 'vaapi', 'mfx' }"
  16. "{ device | -1 | Video Acceleration device (GPU) index (-1 means default device) }"
  17. "{ out_w | | output width (resize by calling cv::resize) }"
  18. "{ out_h | | output height (resize by calling cv::resize) }"
  19. "{ bitwise_not| false | apply simple image processing - bitwise_not pixels by calling cv::bitwise_not }"
  20. "{ opencl | true | use OpenCL (inside VideoCapture/VideoWriter and for image processing) }"
  21. "{ codec | H264 | codec id (four characters string) of output file encoder }"
  22. "{ h help | | print help message }";
  23. struct {
  24. cv::VideoCaptureAPIs backend;
  25. const char* str;
  26. } backend_strings[] = {
  27. { cv::CAP_ANY, "any" },
  28. { cv::CAP_FFMPEG, "ffmpeg" },
  29. { cv::CAP_MSMF, "msmf" },
  30. { cv::CAP_GSTREAMER, "gstreamer" },
  31. };
  32. struct {
  33. VideoAccelerationType acceleration;
  34. const char* str;
  35. } acceleration_strings[] = {
  36. { VIDEO_ACCELERATION_NONE, "none" },
  37. { VIDEO_ACCELERATION_ANY, "any" },
  38. { VIDEO_ACCELERATION_D3D11, "d3d11" },
  39. { VIDEO_ACCELERATION_VAAPI, "vaapi" },
  40. { VIDEO_ACCELERATION_MFX, "mfx" },
  41. };
  42. class FPSCounter {
  43. public:
  44. FPSCounter(double _interval) : interval(_interval) {
  45. }
  46. ~FPSCounter() {
  47. NewFrame(true);
  48. }
  49. void NewFrame(bool last_frame = false) {
  50. num_frames++;
  51. auto now = std::chrono::high_resolution_clock::now();
  52. if (!last_time.time_since_epoch().count()) {
  53. last_time = now;
  54. }
  55. double sec = std::chrono::duration_cast<std::chrono::duration<double>>(now - last_time).count();
  56. if (sec >= interval || last_frame) {
  57. printf("FPS(last %.2f sec) = %.2f\n", sec, num_frames / sec);
  58. fflush(stdout);
  59. num_frames = 0;
  60. last_time = now;
  61. }
  62. }
  63. private:
  64. double interval = 1;
  65. std::chrono::time_point<std::chrono::high_resolution_clock> last_time;
  66. int num_frames = 0;
  67. };
  68. int main(int argc, char** argv)
  69. {
  70. cv::CommandLineParser cmd(argc, argv, keys);
  71. if (cmd.has("help"))
  72. {
  73. cout << "Usage : video_acceleration [options]" << endl;
  74. cout << "Available options:" << endl;
  75. cmd.printMessage();
  76. return EXIT_SUCCESS;
  77. }
  78. string infile = cmd.get<string>("i");
  79. string outfile = cmd.get<string>("o");
  80. string codec = cmd.get<string>("codec");
  81. int device = cmd.get<int>("device");
  82. int out_w = cmd.get<int>("out_w");
  83. int out_h = cmd.get<int>("out_h");
  84. bool use_opencl = cmd.get<bool>("opencl");
  85. bool bitwise_not = cmd.get<bool>("bitwise_not");
  86. cv::VideoCaptureAPIs backend = cv::CAP_ANY;
  87. string backend_str = cmd.get<string>("backend");
  88. for (size_t i = 0; i < sizeof(backend_strings)/sizeof(backend_strings[0]); i++) {
  89. if (backend_str == backend_strings[i].str) {
  90. backend = backend_strings[i].backend;
  91. break;
  92. }
  93. }
  94. VideoAccelerationType accel = VIDEO_ACCELERATION_ANY;
  95. string accel_str = cmd.get<string>("accel");
  96. for (size_t i = 0; i < sizeof(acceleration_strings) / sizeof(acceleration_strings[0]); i++) {
  97. if (accel_str == acceleration_strings[i].str) {
  98. accel = acceleration_strings[i].acceleration;
  99. break;
  100. }
  101. }
  102. ocl::setUseOpenCL(use_opencl);
  103. VideoCapture capture(infile, backend, {
  104. CAP_PROP_HW_ACCELERATION, (int)accel,
  105. CAP_PROP_HW_DEVICE, device
  106. });
  107. if (!capture.isOpened()) {
  108. cerr << "Failed to open VideoCapture" << endl;
  109. return 1;
  110. }
  111. cout << "VideoCapture backend = " << capture.getBackendName() << endl;
  112. VideoAccelerationType actual_accel = static_cast<VideoAccelerationType>(static_cast<int>(capture.get(CAP_PROP_HW_ACCELERATION)));
  113. for (size_t i = 0; i < sizeof(acceleration_strings) / sizeof(acceleration_strings[0]); i++) {
  114. if (actual_accel == acceleration_strings[i].acceleration) {
  115. cout << "VideoCapture acceleration = " << acceleration_strings[i].str << endl;
  116. cout << "VideoCapture acceleration device = " << (int)capture.get(CAP_PROP_HW_DEVICE) << endl;
  117. break;
  118. }
  119. }
  120. VideoWriter writer;
  121. if (!outfile.empty() && outfile != "null") {
  122. const char* codec_str = codec.c_str();
  123. int fourcc = VideoWriter::fourcc(codec_str[0], codec_str[1], codec_str[2], codec_str[3]);
  124. double fps = capture.get(CAP_PROP_FPS);
  125. Size frameSize = { out_w, out_h };
  126. if (!out_w || !out_h) {
  127. frameSize = { (int)capture.get(CAP_PROP_FRAME_WIDTH), (int)capture.get(CAP_PROP_FRAME_HEIGHT) };
  128. }
  129. writer = VideoWriter(outfile, backend, fourcc, fps, frameSize, {
  130. VIDEOWRITER_PROP_HW_ACCELERATION, (int)accel,
  131. VIDEOWRITER_PROP_HW_DEVICE, device
  132. });
  133. if (!writer.isOpened()) {
  134. cerr << "Failed to open VideoWriter" << endl;
  135. return 1;
  136. }
  137. cout << "VideoWriter backend = " << writer.getBackendName() << endl;
  138. actual_accel = static_cast<VideoAccelerationType>(static_cast<int>(writer.get(VIDEOWRITER_PROP_HW_ACCELERATION)));
  139. for (size_t i = 0; i < sizeof(acceleration_strings) / sizeof(acceleration_strings[0]); i++) {
  140. if (actual_accel == acceleration_strings[i].acceleration) {
  141. cout << "VideoWriter acceleration = " << acceleration_strings[i].str << endl;
  142. cout << "VideoWriter acceleration device = " << (int)writer.get(VIDEOWRITER_PROP_HW_DEVICE) << endl;
  143. break;
  144. }
  145. }
  146. }
  147. cout << "\nStarting frame loop. Press ESC to exit\n";
  148. FPSCounter fps_counter(0.5); // print FPS every 0.5 seconds
  149. UMat frame, frame2, frame3;
  150. for (;;)
  151. {
  152. capture.read(frame);
  153. if (frame.empty()) {
  154. cout << "End of stream" << endl;
  155. break;
  156. }
  157. if (out_w && out_h) {
  158. cv::resize(frame, frame2, cv::Size(out_w, out_h));
  159. //cv::cvtColor(frame, outframe, COLOR_BGRA2RGBA);
  160. }
  161. else {
  162. frame2 = frame;
  163. }
  164. if (bitwise_not) {
  165. cv::bitwise_not(frame2, frame3);
  166. }
  167. else {
  168. frame3 = frame2;
  169. }
  170. if (writer.isOpened()) {
  171. writer.write(frame3);
  172. }
  173. if (outfile.empty()) {
  174. imshow("output", frame3);
  175. char key = (char) waitKey(1);
  176. if (key == 27)
  177. break;
  178. else if (key == 'm') {
  179. ocl::setUseOpenCL(!cv::ocl::useOpenCL());
  180. cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL enabled" : "CPU") << " mode\n";
  181. }
  182. }
  183. fps_counter.NewFrame();
  184. }
  185. return EXIT_SUCCESS;
  186. }