6#pragma warning(disable: 4244 4267 4996)
12#include <QWaitCondition>
17#include <libavcodec/avcodec.h>
18#include <libavformat/avformat.h>
19#include <libswscale/swscale.h>
20#include <libavutil/imgutils.h>
53 explicit VideoDecoder(
const QByteArray &video_data, QObject *parent =
nullptr);
120 void Seek(
double position);
144 QMutexLocker locker(&
mutex_);
171 void videoInfo(
int width,
int height,
double fps,
double duration,
bool has_audio);
206 emit
error(
"Błąd strumienia audio: " + message);
238 static int ReadPacket(
void *opaque, uint8_t *buf,
int buf_size);
249 static int64_t
SeekPacket(
void *opaque, int64_t offset,
int whence);
Decodes and plays audio data using FFmpeg libraries in a separate thread.
Definition audio_decoder.h:33
void Seek(double position)
Seeks to a specific position in the video stream. Seeks the internal audio decoder (if present) and s...
Definition video_decoder.cpp:258
double current_position_
Current estimated playback position in seconds (used if no audio).
Definition video_decoder.h:272
double current_audio_position_
Current playback position reported by the internal AudioDecoder. Access protected by mutex_.
Definition video_decoder.h:299
void videoInfo(int width, int height, double fps, double duration, bool has_audio)
Emitted after successful initialization, providing video stream details.
QMutex mutex_
Mutex protecting access to shared state variables (paused_, stopped_, seeking_, etc....
Definition video_decoder.h:275
void run() override
The main function executed by the QThread. Contains the loop that reads packets, decodes video frames...
Definition video_decoder.cpp:287
void HandleAudioError(const QString &message)
Slot connected to the internal AudioDecoder's error signal. Relays the audio error message via the ma...
Definition video_decoder.h:205
int video_stream_
Index of the video stream within the format context.
Definition video_decoder.h:266
bool paused_
Flag indicating if playback is currently paused. Access protected by mutex_.
Definition video_decoder.h:281
void Reset()
Resets the playback position to the beginning of the video. Pauses playback, seeks both video and int...
Definition video_decoder.cpp:211
bool IsPaused() const
Checks if the playback is currently paused. This operation is thread-safe.
Definition video_decoder.h:143
void HandleAudioFinished()
Slot connected to the internal AudioDecoder's playbackFinished signal. Sets the audio_finished_ flag ...
Definition video_decoder.cpp:405
bool audio_initialized_
Flag indicating if the internal AudioDecoder has been successfully initialized.
Definition video_decoder.h:301
AudioDecoder * audio_decoder_
Pointer to the internal AudioDecoder instance (if audio stream exists).
Definition video_decoder.h:295
VideoDecoder(const QByteArray &video_data, QObject *parent=nullptr)
Constructs a VideoDecoder object. Initializes internal state, allocates memory for FFmpeg's custom I/...
Definition video_decoder.cpp:8
bool stopped_
Flag indicating if the thread should stop execution. Access protected by mutex_.
Definition video_decoder.h:279
bool seeking_
Flag indicating if a seek operation is pending. Access protected by mutex_.
Definition video_decoder.h:283
AVFrame * frame_
FFmpeg frame structure to hold decoded video data.
Definition video_decoder.h:264
void playbackFinished()
Emitted when both video and audio (if present) streams reach the end.
void error(const QString &message)
Emitted when an error occurs during initialization or decoding.
bool video_finished_
Flag indicating if the video stream has reached the end. Access protected by mutex_.
Definition video_decoder.h:305
void CleanupFFmpegResources()
Releases all FFmpeg resources (contexts, frames, scaler) and deletes the internal AudioDecoder....
Definition video_decoder.cpp:421
float GetVolume() const
Gets the current playback volume (from the internal AudioDecoder). Returns 0.0 if there is no audio s...
Definition video_decoder.cpp:283
double frame_rate_
Calculated average frame rate of the video in frames per second.
Definition video_decoder.h:268
int64_t last_frame_pts_
Presentation timestamp (PTS) of the previously decoded frame (used for frame timing).
Definition video_decoder.h:292
void positionChanged(double position)
Emitted periodically during playback to indicate the current position (based on audio if available,...
int audio_stream_index_
Index of the audio stream within the format context.
Definition video_decoder.h:297
QWaitCondition wait_condition_
Condition variable used to pause/resume the decoding thread.
Definition video_decoder.h:277
bool audio_finished_
Flag indicating if the internal AudioDecoder has finished playback. Access protected by mutex_.
Definition video_decoder.h:303
static int ReadPacket(void *opaque, uint8_t *buf, int buf_size)
Custom read function for FFmpeg's AVIOContext. Reads data from the internal QBuffer (buffer_).
Definition video_decoder.cpp:452
void Pause()
Toggles the paused state of the playback. If paused, playback stops; if unpaused, playback resumes....
Definition video_decoder.cpp:244
int64_t seek_position_
Target timestamp (in video stream timebase) for the pending seek operation. Access protected by mutex...
Definition video_decoder.h:285
void ExtractFirstFrame()
Synchronously extracts the first video frame from the data. Performs a separate, simplified FFmpeg in...
Definition video_decoder.cpp:108
void SetVolume(float volume) const
Sets the playback volume (delegated to the internal AudioDecoder). Has no effect if there is no audio...
Definition video_decoder.cpp:277
void ReleaseResources()
Releases all FFmpeg and internal AudioDecoder resources. Calls CleanupFFmpegResources()....
Definition video_decoder.h:85
bool HasAudio() const
Checks if the video file contains an audio stream.
Definition video_decoder.h:152
~VideoDecoder() override
Destructor. Stops the decoding thread, waits for it to finish, and releases all allocated resources.
Definition video_decoder.cpp:18
bool Initialize()
Initializes the FFmpeg components for decoding the video and potentially audio. Opens the input strea...
Definition video_decoder.cpp:29
void Stop()
Stops the decoding thread and playback gracefully. Sets the stopped_ flag, stops the internal audio d...
Definition video_decoder.cpp:233
QByteArray video_data_
The raw video data provided in the constructor.
Definition video_decoder.h:252
AVCodecContext * codec_context_
FFmpeg context for the video codec.
Definition video_decoder.h:260
void frameReady(const QImage &frame)
Emitted for each decoded video frame.
QElapsedTimer frame_timer_
Timer used for frame timing synchronization when no audio is present.
Definition video_decoder.h:288
AVIOContext * io_context_
FFmpeg context for custom I/O operations (reading from memory).
Definition video_decoder.h:256
SwsContext * sws_context_
FFmpeg context for image scaling and pixel format conversion (to RGB24).
Definition video_decoder.h:262
double duration_
Total duration of the video in seconds.
Definition video_decoder.h:270
AVFormatContext * format_context_
FFmpeg context for handling the container format (e.g., MP4, WebM).
Definition video_decoder.h:258
bool first_frame_
Flag indicating if the current frame is the first one after start/seek.
Definition video_decoder.h:290
void UpdateAudioPosition(double position)
Slot connected to the internal AudioDecoder's positionChanged signal. Updates the internal current_au...
Definition video_decoder.cpp:414
QBuffer buffer_
QBuffer used by FFmpeg's custom I/O context to read video_data_.
Definition video_decoder.h:254
static int64_t SeekPacket(void *opaque, int64_t offset, int whence)
Custom seek function for FFmpeg's AVIOContext. Adjusts the position within the internal QBuffer (buff...
Definition video_decoder.cpp:458