2021-05-25 08:01:15 +08:00
|
|
|
#pragma once
|
2020-01-18 02:07:22 +08:00
|
|
|
|
2021-11-17 18:17:59 +08:00
|
|
|
#include <memory>
|
2021-05-25 08:01:15 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-11-14 03:55:04 +08:00
|
|
|
|
2023-08-24 15:03:16 +08:00
|
|
|
#include "cereal/visionipc/visionbuf.h"
|
2022-06-28 22:12:42 +08:00
|
|
|
#include "tools/replay/filereader.h"
|
2020-01-18 02:07:22 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
}
|
|
|
|
|
2021-11-17 18:17:59 +08:00
|
|
|
struct AVFrameDeleter {
|
|
|
|
void operator()(AVFrame* frame) const { av_frame_free(&frame); }
|
|
|
|
};
|
|
|
|
|
2021-11-29 21:10:24 +08:00
|
|
|
class FrameReader {
|
2020-01-18 02:07:22 +08:00
|
|
|
public:
|
2021-11-29 21:10:24 +08:00
|
|
|
FrameReader();
|
2021-05-25 08:01:15 +08:00
|
|
|
~FrameReader();
|
2022-05-15 10:55:12 +08:00
|
|
|
bool load(const std::string &url, bool no_hw_decoder = false, std::atomic<bool> *abort = nullptr, bool local_cache = false,
|
|
|
|
int chunk_size = -1, int retries = 0);
|
2024-04-18 01:05:32 +08:00
|
|
|
bool loadFromFile(const std::string &file, bool no_hw_decoder = false, std::atomic<bool> *abort = nullptr);
|
2023-08-24 15:03:16 +08:00
|
|
|
bool get(int idx, VisionBuf *buf);
|
2024-04-18 01:05:32 +08:00
|
|
|
size_t getFrameCount() const { return packets_info.size(); }
|
2021-04-24 15:59:09 +08:00
|
|
|
|
2021-05-25 08:01:15 +08:00
|
|
|
int width = 0, height = 0;
|
2021-04-24 15:59:09 +08:00
|
|
|
|
2020-01-18 02:07:22 +08:00
|
|
|
private:
|
2021-11-17 18:17:59 +08:00
|
|
|
bool initHardwareDecoder(AVHWDeviceType hw_device_type);
|
2023-08-24 15:03:16 +08:00
|
|
|
bool decode(int idx, VisionBuf *buf);
|
2021-11-17 18:17:59 +08:00
|
|
|
AVFrame * decodeFrame(AVPacket *pkt);
|
2023-08-24 15:03:16 +08:00
|
|
|
bool copyBuffers(AVFrame *f, VisionBuf *buf);
|
2021-10-19 05:19:23 +08:00
|
|
|
|
2021-11-26 21:41:14 +08:00
|
|
|
std::unique_ptr<AVFrame, AVFrameDeleter>av_frame_, hw_frame;
|
2021-11-17 18:17:59 +08:00
|
|
|
AVFormatContext *input_ctx = nullptr;
|
|
|
|
AVCodecContext *decoder_ctx = nullptr;
|
|
|
|
|
|
|
|
AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
AVBufferRef *hw_device_ctx = nullptr;
|
2021-11-29 19:09:14 +08:00
|
|
|
int prev_idx = -1;
|
2024-04-18 01:05:32 +08:00
|
|
|
struct PacketInfo {
|
|
|
|
int flags;
|
|
|
|
int64_t pos;
|
|
|
|
};
|
|
|
|
std::vector<PacketInfo> packets_info;
|
2022-02-23 20:52:50 +08:00
|
|
|
inline static std::atomic<bool> has_hw_decoder = true;
|
2020-01-18 02:07:22 +08:00
|
|
|
};
|