video_encoder.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef __VIDEO_ENCODER_H__
  2. #define __VIDEO_ENCODER_H__
  3. #include "abstract_encoder.h"
  4. #include "basic/frame.h"
  5. template <>
  6. class Encoder<MediaType::VIDEO> : public AbstractEncoder {
  7. public:
  8. struct Param {
  9. int bitRate;
  10. int width;
  11. int height;
  12. int fps;
  13. std::string name;
  14. };
  15. Encoder();
  16. ~Encoder() { Close(); }
  17. bool Open(const Param& encodeParam, AVFormatContext* fmtCtx);
  18. virtual bool PushFrame(AVFrame* frame, bool isEnd, uint64_t pts) override;
  19. virtual void AfterEncode() override;
  20. virtual void Close() override;
  21. static const std::vector<std::string>& GetUsableEncoders();
  22. private:
  23. bool
  24. _Init(const Param& encodeParam, AVFormatContext* fmtCtx);
  25. bool _SetHwFrameCtx();
  26. bool _Trans(AVFrame* frame);
  27. AVFrame* _ToHardware();
  28. static void _FindUsableEncoders();
  29. bool _isHardware = false;
  30. std::unique_ptr<FfmpegConverter> _converter = nullptr;
  31. AVFrame* _bufferFrame = nullptr;
  32. static constexpr const char* _encoderNames[4] = {
  33. "h264_nvenc",
  34. "h264_qsv",
  35. "h264_amf",
  36. "libx264",
  37. };
  38. static std::vector<std::string> _usableEncoders;
  39. AVBufferRef* _hwDeviceCtx = nullptr;
  40. AVFrame* _hwFrame = nullptr;
  41. AVPixelFormat _pixFmt = AV_PIX_FMT_NV12;
  42. };
  43. #endif