1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 22:23:51 +00:00

Use unique_ptr with custom deleter for VideoPicture::rgbaFrame

This commit is contained in:
Gleb Mazovetskiy 2021-03-07 03:57:54 +00:00
parent b7076549a3
commit eb93fdfbea
2 changed files with 17 additions and 9 deletions

View file

@ -156,6 +156,12 @@ void PacketQueue::clear()
this->mutex.unlock (); this->mutex.unlock ();
} }
void VideoPicture::AVFrameDeleter::operator()(AVFrame* frame) const
{
av_freep(frame->data);
av_frame_free(&frame);
}
int VideoState::istream_read(void *user_data, uint8_t *buf, int buf_size) int VideoState::istream_read(void *user_data, uint8_t *buf, int buf_size)
{ {
try try
@ -623,8 +629,9 @@ int VideoState::stream_open(int stream_index, AVFormatContext *pFormatCtx)
} }
// Allocate RGBA frame queue. // Allocate RGBA frame queue.
for (std::size_t i = 0; i < VIDEO_PICTURE_ARRAY_SIZE; ++i) { for (std::size_t i = 0; i < VIDEO_PICTURE_ARRAY_SIZE; ++i)
AVFrame *frame = av_frame_alloc(); {
std::unique_ptr<AVFrame, VideoPicture::AVFrameDeleter> frame{av_frame_alloc()};
if (frame == nullptr) { if (frame == nullptr) {
std::cerr << "av_frame_alloc failed" << std::endl; std::cerr << "av_frame_alloc failed" << std::endl;
return -1; return -1;
@ -638,7 +645,7 @@ int VideoState::stream_open(int stream_index, AVFormatContext *pFormatCtx)
std::cerr << "av_image_alloc failed" << std::endl; std::cerr << "av_image_alloc failed" << std::endl;
return -1; return -1;
} }
this->pictq[i].rgbaFrame = frame; this->pictq[i].rgbaFrame = std::move(frame);
} }
this->video_thread.reset(new VideoThread(this)); this->video_thread.reset(new VideoThread(this));
@ -784,11 +791,8 @@ void VideoState::deinit()
} }
// Dellocate RGBA frame queue. // Dellocate RGBA frame queue.
for (std::size_t i = 0; i < VIDEO_PICTURE_ARRAY_SIZE; ++i) { for (std::size_t i = 0; i < VIDEO_PICTURE_ARRAY_SIZE; ++i)
if (this->pictq[i].rgbaFrame == nullptr) continue; this->pictq[i].rgbaFrame = nullptr;
av_freep(&this->pictq[i].rgbaFrame->data[0]);
av_frame_free(&this->pictq[i].rgbaFrame);
}
} }

View file

@ -95,7 +95,11 @@ struct VideoPicture {
VideoPicture() : pts(0.0) VideoPicture() : pts(0.0)
{ } { }
AVFrame* rgbaFrame = nullptr; struct AVFrameDeleter {
void operator()(AVFrame* frame) const;
};
std::unique_ptr<AVFrame, AVFrameDeleter> rgbaFrame;
double pts; double pts;
}; };