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.

183 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __BASICLIBAV_H_INCL__
  2. #define __BASICLIBAV_H_INCL__
  3. #ifndef __linux__
  4. #include <winsock2.h>
  5. #include <ws2tcpip.h>
  6. #endif
  7. #include <math.h>
  8. #ifdef _OPENCV_
  9. #include <opencv2/opencv.hpp>
  10. // #include <opencv/highgui.h>
  11. #include <opencv2/videoio.hpp>
  12. // using namespace cv;
  13. #else
  14. #ifdef _SDL_
  15. #include <SDL.h>
  16. #include <SDL_render.h>
  17. #endif
  18. #endif
  19. #include "basicLogCollection.h"
  20. #include "basicTypes.h"
  21. #ifdef __linux__
  22. #ifndef UINT64_C
  23. #define UINT64_C uint64_t
  24. #endif
  25. #endif
  26. #define _CRT_SECURE_NO_DEPRECIATE
  27. extern "C"
  28. {
  29. #include <libavcodec/avcodec.h>
  30. #include <libavdevice/avdevice.h>
  31. #include <libavformat/avformat.h>
  32. #include <libavformat/avio.h>
  33. #include <libavutil/error.h>
  34. #include <libavutil/file.h>
  35. #include <libavutil/imgutils.h>
  36. #include <libavutil/mathematics.h>
  37. #include <libswresample/swresample.h>
  38. #include <libswscale/swscale.h>
  39. };
  40. // extern bool _FFD_REGISTER_ALL_FLAG;
  41. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1)
  42. #define av_frame_alloc avcodec_alloc_frame
  43. #define av_frame_free avcodec_free_frame
  44. #define avcodec_free_context av_free
  45. #define AV_PIX_FMT_BGR24 PIX_FMT_BGR24
  46. #define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
  47. #define AV_PIX_FMT_YUVJ422P PIX_FMT_YUVJ422P
  48. #endif
  49. #define CLEAR(x) memset(&(x), 0, sizeof(x))
  50. #ifndef V4L2_PIX_FMT_H264
  51. #define V4L2_PIX_FMT_H264 v4l2_fourcc('H', '2', '6', '4') /* H264 with start codes */
  52. #endif
  53. #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
  54. #define VIDEO_PICTURE_QUEUE_SIZE 30
  55. #define BUFFER_ALIGN 32
  56. #define AVLIB_SOURCE_NOTDEF 0
  57. #define AVLIB_SOURCE_DDSHOW 1
  58. #define AVLIB_SOURCE_V4L2 2
  59. #define AVLIB_SOURCE_IPSTREAM 3
  60. // #define PROCESS_YUV420P
  61. #define PROCESS_BGR24
  62. #define _NOAUDIO_
  63. #ifdef _OPENCV_
  64. typedef std::vector<cv::Mat *> _FrameQueue;
  65. #endif
  66. typedef float FrameAspectRatio;
  67. #define FAR_UNKNOWN .0f
  68. #define FAR_4X3 1.3333333333333333333333333333333f
  69. #define FAR_5X4 1.25f
  70. #define FAR_16X9 1.7777777777777777777777777777778f
  71. #define FAR_SQUARE 1.f
  72. #ifndef CODEC_FLAG_GLOBAL_HEADER
  73. // #define AV_CODEC_FLAG_GLOBAL_HEADER (1 << 22)
  74. #define CODEC_FLAG_GLOBAL_HEADER AV_CODEC_FLAG_GLOBAL_HEADER
  75. // #define AVFMT_RAWPICTURE 0x0020
  76. #endif
  77. typedef enum MedaLaneType
  78. {
  79. ML_NONE = 0x00,
  80. ML_IN = 0x01,
  81. ML_OUT = 0x02,
  82. ML_IN_OUT = 0x03,
  83. ML_IMAGE = 0x04,
  84. ML_VIDEO = 0x08,
  85. ML_AUDIO = 0x10
  86. } MedaLaneType;
  87. typedef enum AttachmentStatus
  88. {
  89. AS_NONE = 0,
  90. AS_SOURCE = 1,
  91. AS_DESTINATION = 2,
  92. AS_BOTH = 3
  93. } AttachmentStatus;
  94. typedef union MediaFormatInfo
  95. {
  96. struct VIDEO_INFO
  97. {
  98. /**** video ****/
  99. // PixelFormat PixelFMT;
  100. int ImageWidth;
  101. int ImageHeight;
  102. int VideoFPS;
  103. } VIDEO_INFO;
  104. struct AUDIO_INFO
  105. {
  106. /**** Audio ****/
  107. AVSampleFormat AudioFMT;
  108. int AudioSPS;
  109. int AudioCHA;
  110. int no_use;
  111. } AUDIO_INFO;
  112. } MediaFormatInfo;
  113. typedef struct VideoPicture
  114. {
  115. #ifdef _OPENCV_
  116. cv::Mat * m_pTexture;
  117. #else
  118. #ifdef _SDL_GUI_
  119. SDL_Surface * m_pTexture;
  120. #endif
  121. #endif
  122. int width;
  123. int height; /* source height & width */
  124. bool allocated;
  125. } VideoPicture;
  126. typedef struct VideoState
  127. {
  128. #ifndef _NOAUDIO_
  129. int m_iAudioStreamID;
  130. AVStream * m_pAudioStr;
  131. AVCodecContext * m_pAudioCtx;
  132. AVCodec * m_pAudioCodec;
  133. //_PacketQueue *m_pAudioQQ;
  134. uint8_t m_mAudioBuf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
  135. unsigned int m_iAudioBufSize;
  136. unsigned int m_iAudioBufIndex;
  137. AVPacket m_mAudioPack;
  138. uint8_t * m_pAudioPktData;
  139. int m_iAudioPktSize;
  140. #endif
  141. int m_iVideoStreamID;
  142. AVStream * m_pVideoStr;
  143. AVFormatContext * m_pFormatCtx;
  144. AVDictionary * m_pFormatOpts;
  145. AVCodecContext * m_pCodecCtx;
  146. AVCodec * m_pVideoCodec;
  147. SwsContext * m_pSwsCtx;
  148. char m_pFileName[MAX_PATH_LEN];
  149. MediaStateStatus m_bStatus;
  150. } VideoState;
  151. FrameAspectRatio detectAspectRatio(int width, int height);
  152. bool initLibAV(void);
  153. cv::Mat avframeToCvmat(const AVFrame * frame);
  154. AVFrame * cvmatToAvframe(cv::Mat * image, AVFrame * frame);
  155. #endif // __BASICLIBAV_H_INCL__