我使用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线性, –