ASAPCabinetFE
 
Loading...
Searching...
No Matches
video_decoder.h
Go to the documentation of this file.
1#ifndef VIDEO_DECODER_H
2#define VIDEO_DECODER_H
3
4#include <SDL2/SDL.h>
5extern "C" {
6#include <libavutil/buffer.h>
7#include <libavutil/pixfmt.h>
8}
9#include <chrono>
10#include <string>
11#include <mutex>
12#include <atomic>
13
14// Forward declarations for FFmpeg structs
15struct AVFormatContext;
16struct AVCodecContext;
17struct AVFrame;
18struct AVPacket;
19struct SwsContext;
20
21class FFmpegPlayer; // Forward declaration
22
41public:
50
57
69 bool setup(AVFormatContext* formatContext, SDL_Renderer* renderer, int width, int height);
70
76 void play();
77
83 void stop();
84
90 void update();
91
97 SDL_Texture* getTexture() const;
98
106 bool decodeVideoFrame();
107
113 void updateTexture();
114
120 void flush();
121
127 void resetPlaybackTimes();
128
129 void applyPendingTextureUpdate(); // Uploads queued frame data to the texture (main thread)
130 void queueFrameForTextureUpdate(); // Queues frame data from decoder thread
131
132private:
133 friend class FFmpegPlayer; // Allow FFmpegPlayer to access private members
134 FFmpegPlayer* player_;
135 AVCodecContext* videoCodecContext_;
136 SDL_Renderer* renderer_;
137 int width_;
138 int height_;
139 AVFrame* videoFrame_;
140 AVFrame* rgbFrame_;
141 AVPacket* videoPacket_;
142 SwsContext* swsContext_;
143 int videoStreamIndex_;
144 uint8_t* rgbBuffer_;
145 SDL_Texture* texture_;
146 double videoClock_;
147 std::chrono::high_resolution_clock::time_point lastFrameTime_;
148 std::chrono::high_resolution_clock::time_point playbackStartTime_;
149 bool needsReset_;
150 AVBufferRef* hwDeviceCtx_ = nullptr;
151 AVPixelFormat expectedSwFormat_ = AV_PIX_FMT_NONE;
152 // thread-swap buffer for main thread upload
153 std::mutex pendingMutex_;
154 std::vector<uint8_t> pendingBuffer_; // contiguous RGB24 data: height * width * 3
155 std::atomic_bool hasPendingFrame_{false};
156 int pendingPitch_{0}; // bytes per row in pendingBuffer_ (usually width*3)
157
158
164 void cleanup();
165
166};
167
168#endif // VIDEO_DECODER_H
Implements video and audio playback using FFmpeg.
Definition ffmpeg_player.h:30
Decodes and renders video frames using FFmpeg and SDL.
Definition video_decoder.h:40
void resetPlaybackTimes()
Resets playback timing variables.
Definition video_decoder.cpp:517
void stop()
Stops video playback.
Definition video_decoder.cpp:223
void updateTexture()
Updates the SDL texture with the current RGB frame data.
Definition video_decoder.cpp:480
void play()
Starts video playback.
Definition video_decoder.cpp:218
SDL_Texture * getTexture() const
Retrieves the current video texture.
Definition video_decoder.cpp:266
void flush()
Flushes the decoder buffers.
Definition video_decoder.cpp:510
~VideoDecoder()
Destroys the VideoDecoder instance and cleans up resources.
Definition video_decoder.cpp:27
void update()
Updates the video frame based on playback timing.
Definition video_decoder.cpp:228
bool decodeVideoFrame()
Decodes the next video frame.
Definition video_decoder.cpp:320
bool setup(AVFormatContext *formatContext, SDL_Renderer *renderer, int width, int height)
Sets up the video decoder with format context and renderer.
Definition video_decoder.cpp:32