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.

110 lines
3.7 KiB

7 years ago
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
  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. FrameAspectRatio RetVal = FAR_UNKNOWN;
  24. /*if (((width / 4) * 3) == height) RetVal = FAR_4X3;
  25. if (((width / 5) * 4) == height) RetVal = FAR_5X4;
  26. if (((width / 16) * 9) == height) RetVal = FAR_16X9;
  27. if (RetVal == FAR_UNKNOWN)RetVal = FAR_NOT_STANDARD;
  28. */
  29. RetVal = (float) width / (float)height;
  30. return RetVal;
  31. };
  32. bool initLibAV(void){
  33. if (!_FFD_REGISTER_ALL_FLAG){
  34. char Buffer[256];
  35. _FFD_REGISTER_ALL_FLAG = true;
  36. LOG("Init LIBAV");
  37. sprintf(Buffer, "LibAV Codec: %d.%d.%d", LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO);
  38. LOG(Buffer);
  39. sprintf(Buffer, "LibAV Device: %d.%d.%d", LIBAVDEVICE_VERSION_MAJOR, LIBAVDEVICE_VERSION_MINOR, LIBAVDEVICE_VERSION_MICRO);
  40. LOG(Buffer);
  41. sprintf(Buffer, "LibAV Format: %d.%d.%d", LIBAVFORMAT_VERSION_MAJOR, LIBAVFORMAT_VERSION_MINOR, LIBAVFORMAT_VERSION_MICRO);
  42. LOG(Buffer);
  43. sprintf(Buffer, "LibAV SWScale: %d.%d.%d", LIBSWSCALE_VERSION_MAJOR, LIBSWSCALE_VERSION_MINOR, LIBSWSCALE_VERSION_MICRO);
  44. LOG(Buffer);
  45. av_register_all();
  46. avcodec_register_all();
  47. avdevice_register_all();
  48. avformat_network_init();
  49. };
  50. return _FFD_REGISTER_ALL_FLAG;
  51. };
  52. //************************************
  53. // Method: avframeToCvmat
  54. // Access: public
  55. // Returns: cv::Mat
  56. // Qualifier:
  57. // Parameter: const AVFrame * frame
  58. // Description: AVFrame?MAT
  59. //************************************
  60. cv::Mat avframeToCvmat(const AVFrame *frame) {
  61. int width = frame->width;
  62. int height = frame->height;
  63. cv::Mat image(height, width, CV_8UC3);
  64. int cvLinesizes[1];
  65. cvLinesizes[0] = image.step1();
  66. SwsContext *conversion = sws_getContext(
  67. width, height, (AVPixelFormat)frame->format, width, height,
  68. AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  69. sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data,
  70. cvLinesizes);
  71. sws_freeContext(conversion);
  72. return image;
  73. }
  74. //************************************
  75. // Method: cvmatToAvframe
  76. // Access: public
  77. // Returns: AVFrame *
  78. // Qualifier:
  79. // Parameter: cv::Mat * image
  80. // Parameter: AVFrame * frame
  81. // Description: MAT?AVFrame
  82. //************************************
  83. AVFrame *cvmatToAvframe(cv::Mat *image, AVFrame *frame) {
  84. int width = image->cols;
  85. int height = image->rows;
  86. int cvLinesizes[1];
  87. cvLinesizes[0] = image->step1();
  88. if (frame == NULL) {
  89. frame = av_frame_alloc();
  90. av_image_alloc(frame->data, frame->linesize, width, height,
  91. AVPixelFormat::AV_PIX_FMT_YUV420P, 1);
  92. }
  93. SwsContext *conversion = sws_getContext(
  94. width, height, AVPixelFormat::AV_PIX_FMT_BGR24, width, height,
  95. (AVPixelFormat)frame->format, SWS_FAST_BILINEAR, NULL, NULL, NULL);
  96. sws_scale(conversion, &image->data, cvLinesizes, 0, height, frame->data,
  97. frame->linesize);
  98. sws_freeContext(conversion);
  99. return frame;
  100. }
  101. #undef LOG