Files
sunnypilot/tools/clib/framereader.h
Dean Lee 5540dcae78 Refactor FrameReader (#21002)
* Refactor FrameReader

* decodeThread

* delete frame

* remove joined

* continue

* less diff

* robust cv wait

* cache fist 15 frames

* notify_all

* rename variables

* need call avformat_find_stream_info before dump_format

* get width&height from codec

* use std::string

* delete in removeSegment

* use std::mutex in lockmgr_cb

* fix wrong min/max

* no get in process

* cleanup

* always notify decodeThread to do prefetch
2021-05-24 17:01:15 -07:00

54 lines
992 B
C++

#pragma once
#include <unistd.h>
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
// independent of QT, needs ffmpeg
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
class FrameReader {
public:
FrameReader(const std::string &fn);
~FrameReader();
uint8_t *get(int idx);
AVFrame *toRGB(AVFrame *);
int getRGBSize() { return width*height*3; }
void process();
int width = 0, height = 0;
private:
void decodeThread();
struct Frame{
AVPacket *pkt;
AVFrame *picture;
};
std::vector<Frame*> frames;
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
struct SwsContext *sws_ctx = NULL;
std::mutex mutex;
std::condition_variable cv_decode;
std::condition_variable cv_frame;
int decode_idx = -1;
std::atomic<bool> exit_ = false;
std::thread thread;
bool valid = true;
std::string url;
};