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.

107 lines
3.5 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. av_register_all();
  49. avcodec_register_all();
  50. avdevice_register_all();
  51. avformat_network_init();
  52. };
  53. return _FFD_REGISTER_ALL_FLAG;
  54. }
  55. //************************************
  56. // Method: avframeToCvmat
  57. // Access: public
  58. // Returns: cv::Mat
  59. // Qualifier:
  60. // Parameter: const AVFrame * frame
  61. // Description: AVFrame?MAT
  62. //************************************
  63. cv::Mat avframeToCvmat(const AVFrame * frame)
  64. {
  65. int width = frame->width;
  66. int height = frame->height;
  67. cv::Mat image(height, width, CV_8UC3);
  68. int cvLinesizes[1];
  69. cvLinesizes[0] = image.step1();
  70. SwsContext * conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height, AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  71. sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
  72. sws_freeContext(conversion);
  73. return image;
  74. }
  75. //************************************
  76. // Method: cvmatToAvframe
  77. // Access: public
  78. // Returns: AVFrame *
  79. // Qualifier:
  80. // Parameter: cv::Mat * image
  81. // Parameter: AVFrame * frame
  82. // Description: MAT?AVFrame
  83. //************************************
  84. AVFrame * cvmatToAvframe(cv::Mat * image, AVFrame * frame)
  85. {
  86. int width = image->cols;
  87. int height = image->rows;
  88. int cvLinesizes[1];
  89. cvLinesizes[0] = image->step1();
  90. if (frame == NULL)
  91. {
  92. frame = av_frame_alloc();
  93. av_image_alloc(frame->data, frame->linesize, width, height, AVPixelFormat::AV_PIX_FMT_YUV420P, 1);
  94. }
  95. SwsContext * conversion = sws_getContext(width, height, AVPixelFormat::AV_PIX_FMT_BGR24, width, height, (AVPixelFormat) frame->format, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  96. sws_scale(conversion, &image->data, cvLinesizes, 0, height, frame->data, frame->linesize);
  97. sws_freeContext(conversion);
  98. return frame;
  99. }
  100. #undef LOG