我使用C#來調用ffmpeg庫。 我使用示例代碼來解碼MP4格式的視頻。FFmpeg內存越來越大avcodec_decode_video2
然而,我發現,在內存中變了很多(如10MB),當我打電話給avcodec_decode_video2功能。
存儲持續增長,直到我叫avcodec_close(&編解碼器環境)功能。
我想,其原因可能是avcodec_decode_video2函數的調用過程中AVCodecContext ALLOC 10MB的數據。在AVCodecContext關閉之前,這個內存不會被釋放。
我不知道我是否正確。
主要代碼如下。
private bool readNextBuffer(int index)
{
/// Read frames into buffer
//m_codecContext->refcounted_frames = 0;
int frameNumber = 0;
FrameBuffer[index].Clear();
GC.Collect();
while (frameNumber < m_bufferSize)
{
AVPacket Packet; /// vedio packet
AVPacket* pPacket = &Packet;
//AVFrame* pDecodedFrame = FFmpegInvoke.avcodec_alloc_frame();
AVFrame* pDecodedFrame = FFmpegInvoke.av_frame_alloc();
if (FFmpegInvoke.av_read_frame(m_pFormatContext, pPacket) < 0)
{
Console.WriteLine("The end of the vedio.");
break;
}
if (pPacket->stream_index == pStream->index)
{
/// Decode vedio frame
int gotPicture = 0;
int size = FFmpegInvoke.avcodec_decode_video2(m_codecContext, pDecodedFrame, &gotPicture, pPacket);
if (size < 0)
{
Console.WriteLine("End of the vedio.");
//throw new Exception(string.Format("Error while decoding frame {0}", frameNumber));
break;
}
if (gotPicture == 1)
{
/// Allocate an AVFrame structure
if (convertFrame(pDecodedFrame, index))
{
frameNumber++;
}
else
{
Console.WriteLine("Error: convert failed.");
}
}
}
FFmpegInvoke.av_frame_free(&pDecodedFrame);
FFmpegInvoke.av_free_packet(pPacket);
GC.Collect();
}
nowFrameIndex = 0;
return FrameBuffer.Count > 0 ? true : false;
}
什麼解析mp4文件?什麼視頻編解碼器包含它(h264?hevc?別的?)? –
每幀的分辨率爲2048 * 2048,編解碼器爲h264。內存似乎與我的m_bufferSize線性, –