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

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

Loading…
Cancel
Save