framereader.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include "msgq/visionipc/visionbuf.h"
  5. #include "tools/replay/filereader.h"
  6. #include "tools/replay/util.h"
  7. extern "C" {
  8. #include <libavcodec/avcodec.h>
  9. #include <libavformat/avformat.h>
  10. }
  11. class VideoDecoder;
  12. class FrameReader {
  13. public:
  14. FrameReader();
  15. ~FrameReader();
  16. bool load(CameraType type, const std::string &url, bool no_hw_decoder = false, std::atomic<bool> *abort = nullptr, bool local_cache = false,
  17. int chunk_size = -1, int retries = 0);
  18. bool loadFromFile(CameraType type, const std::string &file, bool no_hw_decoder = false, std::atomic<bool> *abort = nullptr);
  19. bool get(int idx, VisionBuf *buf);
  20. size_t getFrameCount() const { return packets_info.size(); }
  21. int width = 0, height = 0;
  22. VideoDecoder *decoder_ = nullptr;
  23. AVFormatContext *input_ctx = nullptr;
  24. int prev_idx = -1;
  25. struct PacketInfo {
  26. int flags;
  27. int64_t pos;
  28. };
  29. std::vector<PacketInfo> packets_info;
  30. };
  31. class VideoDecoder {
  32. public:
  33. VideoDecoder();
  34. ~VideoDecoder();
  35. bool open(AVCodecParameters *codecpar, bool hw_decoder);
  36. bool decode(FrameReader *reader, int idx, VisionBuf *buf);
  37. int width = 0, height = 0;
  38. private:
  39. bool initHardwareDecoder(AVHWDeviceType hw_device_type);
  40. AVFrame *decodeFrame(AVPacket *pkt);
  41. bool copyBuffer(AVFrame *f, VisionBuf *buf);
  42. AVFrame *av_frame_, *hw_frame_;
  43. AVCodecContext *decoder_ctx = nullptr;
  44. AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE;
  45. AVBufferRef *hw_device_ctx = nullptr;
  46. };