2021-05-25 08:01:15 +08:00
|
|
|
#pragma once
|
2020-01-17 10: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
|
|
|
|
2022-06-28 22:12:42 +08:00
|
|
|
#include "tools/replay/filereader.h"
|
2020-01-17 10: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-17 10: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-14 19:55:12 -07: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);
|
2022-02-23 13:52:50 +01:00
|
|
|
bool load(const std::byte *data, size_t size, bool no_hw_decoder = false, std::atomic<bool> *abort = nullptr);
|
2022-05-14 19:55:12 -07:00
|
|
|
bool get(int idx, uint8_t *yuv);
|
2021-10-04 22:45:28 +08:00
|
|
|
int getYUVSize() const { return width * height * 3 / 2; }
|
2021-11-29 19:09:14 +08:00
|
|
|
size_t getFrameCount() const { return packets.size(); }
|
2021-06-08 14:31:59 +08:00
|
|
|
bool valid() const { return valid_; }
|
2021-04-24 00:59:09 -07:00
|
|
|
|
2021-05-25 08:01:15 +08:00
|
|
|
int width = 0, height = 0;
|
2021-12-16 08:19:07 +08:00
|
|
|
int aligned_width = 0, aligned_height = 0;
|
2021-04-24 00:59:09 -07:00
|
|
|
|
2020-01-17 10:07:22 -08:00
|
|
|
private:
|
2021-11-17 18:17:59 +08:00
|
|
|
bool initHardwareDecoder(AVHWDeviceType hw_device_type);
|
2022-05-14 19:55:12 -07:00
|
|
|
bool decode(int idx, uint8_t *yuv);
|
2021-11-17 18:17:59 +08:00
|
|
|
AVFrame * decodeFrame(AVPacket *pkt);
|
2022-05-14 19:55:12 -07:00
|
|
|
bool copyBuffers(AVFrame *f, uint8_t *yuv);
|
2021-10-19 05:19:23 +08:00
|
|
|
|
2021-11-29 19:09:14 +08:00
|
|
|
std::vector<AVPacket*> packets;
|
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;
|
2021-10-19 05:19:23 +08:00
|
|
|
int key_frames_count_ = 0;
|
2021-06-08 14:31:59 +08:00
|
|
|
bool valid_ = false;
|
2021-11-01 18:55:56 +08:00
|
|
|
AVIOContext *avio_ctx_ = nullptr;
|
2021-11-17 18:17:59 +08:00
|
|
|
|
|
|
|
|
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;
|
2022-02-23 13:52:50 +01:00
|
|
|
inline static std::atomic<bool> has_hw_decoder = true;
|
2020-01-17 10:07:22 -08:00
|
|
|
};
|