Home > other >  Ffmpeg API code, memory leaks
Ffmpeg API code, memory leaks

Time:01-21


version: ffmpeg 4.3.1

FFmpeg_Code01 function called main function in the circulation, enter a, call a
Each time through the task manager found that enter, special working set (events) [memory] went up some, of course function internal memory a spike in the process of decoding time not
I waited a moment also didn't fall,

This is what happened? What other free memory code didn't call?

The HTML code is as follows, VS2017 engineering



 
# include "PCH. H"
#include

# ifdef _MSC_VER
#include
# pragma warning (4819) disable:
# pragma warning (4996) disable:
# endif

#include
#include
#include
#include

Extern "C" {
#include
#include
#include
#include

#include
}

# ifdef __MINGW32__
# undef main/* Prevents SDL from overriding the main () */
# endif

# ifdef _MSC_VER
# undef main
# endif

# ifdef _MSC_VER
# pragma warning (4244) disable:
# pragma comment (lib, "avformat")
# pragma comment (lib, "avcodec")
# pragma comment (lib, "avutil")
# pragma comment (lib, "swscale")
# endif

Int FFmpeg_Code01 (int arg c, char * argv []);

Int get_error_msg (int code, STD: : string& MSG) {
STD: : string str_err_msg;
Str_err_msg. Resize (256);
Int ret=av_strerror (code, (char *) str_err_msg. Data (), str_err_msg. The length ());
MSG=str_err_msg. C_str ();
return ret;
}

Int main (int arg c, char * argv [])
{
int ret=0;
Do
{
Ret=FFmpeg_Code01 (arg c, argv);
Printf (" Press Enter to next loop. \ n ");
getchar();
} the while (true);
return ret;
}

Int CreateAndFillFrame (AVFrame * * ppFrameYUV, int width, int height, const uint8_t * pbufs)
{
Int ret=1;
* ppFrameYUV=av_frame_alloc ();
Int bytes=0;
STD: : string errorMessage;
Do
{
Ret=av_image_fill_arrays ((* ppFrameYUV) - & gt; Data, (* ppFrameYUV) - & gt; Linesize, pbufs AV_PIX_FMT_YUV420P, width, height, 32);
If (ret & lt; 0 {
Get_error_msg (ret, errorMessage);
Printf (" av_image_fill_arrays failed, % s \ n ", errorMessage) c_str ());
break;
}
Ret=0;
} while (false);
return ret;
}

Int FFmpeg_Code01 (int arg c, char * argv [])
{
Int ret=1;
int cnt=0;
AVFormatContext * pFormatCtx=NULL;
AVDictionary * pDictionary=NULL;
AVPacket packet;
AVFrame * frame=NULL, * frameYUV=NULL;
Pbufs uint8_t *=NULL;
Struct SwsContext * swsCtx=NULL;
Bool videoCodecOpened=false;
AVCodecContext * vCodecCtx=NULL;
Path=const char * argv [1].
STD: : string errorMessage;

Do
{
Ret=avformat_open_input (& amp; PFormatCtx, path, NULL, & amp; PDictionary);
if (ret !=0) {
Get_error_msg (ret, errorMessage);
Printf (" avformat_open_input failed, % s \ n ", errorMessage) c_str ());
break;
}

Int CNT=pFormatCtx - & gt; Nb_streams;
Ret=avformat_find_stream_info (pFormatCtx, NULL);
if (ret !=0) {
Get_error_msg (ret, errorMessage);
Printf (" avformat_find_stream_info failed, % s \ n ", errorMessage) c_str ());
break;
}

Int audioStreamIdx=1, videoStreamIdx=1;
AVCodec * vCodec=NULL;
VideoStreamIdx=av_find_best_stream (pFormatCtx AVMediaType: : AVMEDIA_TYPE_VIDEO, 1, 1, & amp; VCodec, 0);
If (videoStreamIdx & lt; 0 {
Printf (" av_find_best_stream failed \ n ");
break;
}

VCodecCtx=pFormatCtx - & gt; Streams [videoStreamIdx] - & gt; The codec.
if (! VCodec) {
VCodec=avcodec_find_decoder (vCodecCtx - & gt; Codec_id);
if (! VCodec) {
Printf (" avcodec_find_decoder failed, codec_id=[% d] \ n ", vCodecCtx - & gt; Codec_id);
break;
}
}

Ret=avcodec_open2 (vCodecCtx vCodec, NULL);
If (ret & lt; 0 {
Get_error_msg (ret, errorMessage);
Printf (" avcodec_open2 failed, % s \ n ", errorMessage) c_str ());
break;
}
VideoCodecOpened=true;

Frame=av_frame_alloc ();
FrameYUV=av_frame_alloc ();

Int width=vCodecCtx - & gt; Width;
Int height=vCodecCtx - & gt; Height;
Int bytes=0;

Bytes=av_image_get_buffer_size (AV_PIX_FMT_YUV420P, width, height, 32).
Pbufs=(uint8_t *) av_malloc (bytes * sizeof (uint8_t));
if (! Pbufs) {
Printf (" av_malloc failed. \ n ");
break;
}

Ret=av_image_fill_arrays (frameYUV - & gt; The data, frameYUV - & gt; Linesize, pbufs AV_PIX_FMT_YUV420P, width, height, 32);
If (ret & lt; 0 {
Get_error_msg (ret, errorMessage);
Printf (" av_image_fill_arrays failed, % s \ n ", errorMessage) c_str ());
break;
}

SwsCtx=sws_getContext (width, height, vCodecCtx - & gt; Pix_fmt, width, height, AV_PIX_FMT_YUV420P SWS_BICUBIC, NULL, NULL, NULL);
if (! SwsCtx) {
Printf (" sws_getContext failed. \ n ");
break;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related