You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef LRF_PROXY_ONLY
  2. #include "basicLIBAV.h"
  3. #ifdef WIN32
  4. #pragma comment(lib, "avcodec.lib")
  5. #pragma comment(lib, "avdevice.lib")
  6. #pragma comment(lib, "avfilter.lib")
  7. #pragma comment(lib, "avformat.lib")
  8. #pragma comment(lib, "avutil.lib")
  9. // #pragma comment(lib, "postproc.lib")
  10. #pragma comment(lib, "swresample.lib")
  11. // #pragma comment(lib, "avresample.lib")
  12. #pragma comment(lib, "swscale.lib")
  13. // New Deps
  14. #pragma comment(lib, "vfw32.lib")
  15. #pragma comment(lib, "ws2_32.lib")
  16. // #pragma comment( lib, "libeay32.lib")
  17. // #pragma comment( lib, "ssleay32.lib")
  18. #pragma comment(lib, "Secur32.lib")
  19. #pragma comment(lib, "Bcrypt.lib")
  20. #endif
  21. #define LOG(X) basicLogCollection::getInstance()->Log("basicLibAV", NULL, X)
  22. static bool _FFD_REGISTER_ALL_FLAG = false;
  23. FrameAspectRatio detectAspectRatio(int width, int height)
  24. {
  25. FrameAspectRatio RetVal = FAR_UNKNOWN;
  26. /*if (((width / 4) * 3) == height) RetVal = FAR_4X3;
  27. if (((width / 5) * 4) == height) RetVal = FAR_5X4;
  28. if (((width / 16) * 9) == height) RetVal = FAR_16X9;
  29. if (RetVal == FAR_UNKNOWN)RetVal = FAR_NOT_STANDARD;
  30. */
  31. RetVal = (float) width / (float) height;
  32. return RetVal;
  33. }
  34. bool initLibAV(void)
  35. {
  36. if (! _FFD_REGISTER_ALL_FLAG)
  37. {
  38. char Buffer[256];
  39. _FFD_REGISTER_ALL_FLAG = true;
  40. LOG("Init LIBAV");
  41. sprintf(Buffer, "LibAV Codec: %d.%d.%d", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO);
  42. LOG(Buffer);
  43. sprintf(Buffer, "LibAV Device: %d.%d.%d", LIBAVDEVICE_VERSION_MAJOR, LIBAVDEVICE_VERSION_MINOR, LIBAVDEVICE_VERSION_MICRO);
  44. LOG(Buffer);
  45. sprintf(Buffer, "LibAV Format: %d.%d.%d", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO);
  46. LOG(Buffer);
  47. sprintf(Buffer, "LibAV SWScale: %d.%d.%d", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO);
  48. LOG(Buffer);
  49. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  50. av_register_all();
  51. avcodec_register_all();
  52. #endif
  53. avdevice_register_all();
  54. avformat_network_init();
  55. };
  56. return _FFD_REGISTER_ALL_FLAG;
  57. }
  58. //************************************
  59. // Method: avframeToCvmat
  60. // Access: public
  61. // Returns: cv::Mat
  62. // Qualifier:
  63. // Parameter: const AVFrame * frame
  64. // Description: AVFrame?MAT
  65. //************************************
  66. cv::Mat avframeToCvmat(const AVFrame * frame)
  67. {
  68. int width = frame->width;
  69. int height = frame->height;
  70. cv::Mat image(height, width, CV_8UC3);
  71. int cvLinesizes[1];
  72. cvLinesizes[0] = image.step1();
  73. SwsContext * conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height, AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  74. sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
  75. sws_freeContext(conversion);
  76. return image;
  77. }
  78. //************************************
  79. // Method: cvmatToAvframe
  80. // Access: public
  81. // Returns: AVFrame *
  82. // Qualifier:
  83. // Parameter: cv::Mat * image
  84. // Parameter: AVFrame * frame
  85. // Description: MAT?AVFrame
  86. //************************************
  87. AVFrame * cvmatToAvframe(cv::Mat * image, AVFrame * frame)
  88. {
  89. int width = image->cols;
  90. int height = image->rows;
  91. int cvLinesizes[1];
  92. cvLinesizes[0] = image->step1();
  93. if (frame == NULL)
  94. {
  95. frame = av_frame_alloc();
  96. av_image_alloc(frame->data, frame->linesize, width, height, AVPixelFormat::AV_PIX_FMT_YUV420P, 1);
  97. }
  98. SwsContext * conversion = sws_getContext(width, height, AVPixelFormat::AV_PIX_FMT_BGR24, width, height, (AVPixelFormat) frame->format, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  99. sws_scale(conversion, &image->data, cvLinesizes, 0, height, frame->data, frame->linesize);
  100. sws_freeContext(conversion);
  101. return frame;
  102. }
  103. #undef LOG
  104. #endif // !LRF_PROXY_ONLY