VideoEncoder.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * 叶海辉
  3. * QQ群121376426
  4. * http://blog.yundiantech.com/
  5. */
  6. #ifndef VIDEOENCORDER_H
  7. #define VIDEOENCORDER_H
  8. #include <list>
  9. #include <thread>
  10. #include "Mutex/Cond.h"
  11. extern "C"
  12. {
  13. #include "libavcodec/avcodec.h"
  14. #include "libavformat/avformat.h"
  15. #include "libswscale/swscale.h"
  16. #include "libavdevice/avdevice.h"
  17. }
  18. struct FrameDataNode
  19. {
  20. uint8_t * buffer;
  21. int size;
  22. int64_t time;
  23. FrameDataNode()
  24. {
  25. buffer = nullptr;
  26. size = 0;
  27. }
  28. };
  29. /// 编码h.264的线程 这里把编码和采集分开 放到单独的线程 是因为编码也比较耗时
  30. class VideoEncoder
  31. {
  32. public:
  33. explicit VideoEncoder();
  34. ~VideoEncoder();
  35. void setWidth(int w, int h);//设置编码后的图像高宽(这个必须和输入的yuv图像数据一样 且必须是偶数)
  36. void setQuantity(int value);// 设置编码质量
  37. void startEncode();
  38. void stopEncode();
  39. /**
  40. * @brief inputYuvBuffer 输入需要编码的YUV数据
  41. * @param buffer
  42. * @param size
  43. * @param time
  44. */
  45. void inputYuvBuffer(uint8_t *buffer, int size, int64_t time);
  46. protected:
  47. void run();
  48. private:
  49. AVCodecContext* pCodecCtx;
  50. AVCodec* pCodec;
  51. uint8_t* picture_buf;
  52. AVFrame* picture;
  53. std::list<FrameDataNode> mYuvBufferList; //YUV数据队列
  54. Cond *mCond;
  55. int mBitRate; //video bitRate
  56. int mWidth;
  57. int mHeight;
  58. bool mIsStop;
  59. bool openEncoder(); //打开编码器
  60. bool closeEncoder(); //关闭编码器
  61. };
  62. #endif // VIDEOENCORDER_H