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.

109 lines
3.6 KiB

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