videoio(ffmpeg): avoid memory leaks

This commit is contained in:
Alexander Alekhin 2022-04-01 18:02:14 +00:00
parent a93fb52632
commit 1b3a06a02a

View File

@ -2210,17 +2210,13 @@ bool CvVideoWriter_FFMPEG::writeFrame( const unsigned char* data, int step, int
/// close video output stream and free associated memory /// close video output stream and free associated memory
void CvVideoWriter_FFMPEG::close() void CvVideoWriter_FFMPEG::close()
{ {
// nothing to do if already released
if ( !picture )
return;
/* no more frame to compress. The codec has a latency of a few /* no more frame to compress. The codec has a latency of a few
frames if using B frames, so we get the last frames by frames if using B frames, so we get the last frames by
passing the same picture again */ passing the same picture again */
// TODO -- do we need to account for latency here? // TODO -- do we need to account for latency here?
/* write the trailer, if any */ /* write the trailer, if any */
if(ok && oc) if (picture && ok && oc)
{ {
#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0) #if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(57, 0, 0)
if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
@ -2244,32 +2240,34 @@ void CvVideoWriter_FFMPEG::close()
// free pictures // free pictures
#if LIBAVFORMAT_BUILD > 4628 #if LIBAVFORMAT_BUILD > 4628
if( video_st->codec->pix_fmt != input_pix_fmt) if (picture && video_st && video_st->codec->pix_fmt != input_pix_fmt)
#else #else
if( video_st->codec.pix_fmt != input_pix_fmt) if (picture && video_st && video_st->codec.pix_fmt != input_pix_fmt)
#endif #endif
{ {
if(picture->data[0]) if(picture->data[0])
free(picture->data[0]); free(picture->data[0]);
picture->data[0] = 0; picture->data[0] = 0;
} }
av_free(picture); av_freep(&picture);
if (input_picture) av_freep(&input_picture);
av_free(input_picture);
/* close codec */ if (video_st && video_st->codec)
{
/* close codec */
#if LIBAVFORMAT_BUILD > 4628 #if LIBAVFORMAT_BUILD > 4628
avcodec_close(video_st->codec); avcodec_close(video_st->codec);
#else #else
avcodec_close(&(video_st->codec)); avcodec_close(&(video_st->codec));
#endif #endif
}
av_free(outbuf); av_freep(&outbuf);
if (oc) if (oc)
{ {
if (!(fmt->flags & AVFMT_NOFILE)) if (fmt && !(fmt->flags & AVFMT_NOFILE))
{ {
/* close the output file */ /* close the output file */