Use unique_ptr with custom deleter for VideoPicture::rgbaFrame

pull/593/head
Gleb Mazovetskiy 4 years ago
parent b7076549a3
commit eb93fdfbea

@ -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);
}
} }

@ -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;
}; };

Loading…
Cancel
Save