多线程解码视频帧, 添加todo.txt
This commit is contained in:
@@ -11,11 +11,11 @@ extern "C" {
|
||||
|
||||
template<typename T>
|
||||
requires std::is_same_v<T, AVPacket> || std::is_same_v<T, AVFrame>
|
||||
struct MediaQueue
|
||||
struct MediaQueue
|
||||
{
|
||||
static constexpr int MAX_SIZE = 500;
|
||||
static constexpr int MAX_SIZE = 30;
|
||||
bool full = false;
|
||||
std::queue<T> queue;
|
||||
std::queue<T*> queue;
|
||||
std::condition_variable cv;
|
||||
std::mutex mut;
|
||||
|
||||
@@ -38,14 +38,15 @@ bool MediaQueue<T>::push(const T* item) {
|
||||
auto temp = av_packet_alloc();
|
||||
av_packet_ref(temp, item);
|
||||
size += temp->size;
|
||||
queue.push(*temp);
|
||||
}else if(std::is_same_v<T, AVFrame>) {
|
||||
queue.push(temp);
|
||||
}
|
||||
else if (std::is_same_v<T, AVFrame>) {
|
||||
auto temp = av_frame_alloc();
|
||||
av_frame_ref(temp, item);
|
||||
queue.push(*temp);
|
||||
queue.push(temp);
|
||||
}
|
||||
count++;
|
||||
if(count >= MAX_SIZE) {
|
||||
if (count >= MAX_SIZE) {
|
||||
full = true;
|
||||
}
|
||||
cv.notify_all();
|
||||
@@ -57,23 +58,24 @@ bool MediaQueue<T>::pop(T* item, bool block, bool quit) {
|
||||
std::unique_lock lock(mut);
|
||||
while (!quit) {
|
||||
if (!queue.empty()) {
|
||||
T temp = queue.front();
|
||||
if constexpr (std::is_same_v<T, AVPacket>) {
|
||||
if (av_packet_ref(item, &temp) < 0) {
|
||||
AVPacket* temp = queue.front();
|
||||
if (av_packet_ref(item, temp) < 0) {
|
||||
return false;
|
||||
}
|
||||
av_packet_unref(&temp);
|
||||
av_packet_unref(temp);
|
||||
}
|
||||
else if (std::is_same_v<T, AVFrame>) {
|
||||
if (av_frame_ref(item, &temp) < 0) {
|
||||
AVFrame* temp = queue.front();
|
||||
if (av_frame_ref(item, temp) < 0) {
|
||||
return false;
|
||||
}
|
||||
av_frame_unref(&temp);
|
||||
av_frame_unref(temp);
|
||||
}
|
||||
queue.pop();
|
||||
count--;
|
||||
full = false;
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
else if (block) {
|
||||
cv.wait(lock);
|
||||
@@ -85,11 +87,10 @@ bool MediaQueue<T>::pop(T* item, bool block, bool quit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
struct VideoParam
|
||||
{
|
||||
|
||||
MediaQueue<AVPacket> queue;
|
||||
MediaQueue<AVPacket> packetQueue;
|
||||
MediaQueue<AVFrame> frameQueue;
|
||||
AVFormatContext* fmtCtx;
|
||||
AVCodecContext* codecCtx;
|
||||
int width;
|
||||
@@ -97,11 +98,12 @@ struct VideoParam
|
||||
int videoStreamIndex;
|
||||
|
||||
bool eof = false;
|
||||
|
||||
bool pause = false;
|
||||
bool quit = false;
|
||||
};
|
||||
|
||||
void InitDecoder(const char* filepath, VideoParam& param);
|
||||
void RequestPacket(VideoParam& param);
|
||||
AVFrame* RequestFrame(VideoParam& param);
|
||||
void RequestFrame(VideoParam& param);
|
||||
#endif
|
||||
Reference in New Issue
Block a user