我发现当avcodec_send_frame返回false时,则会导致内存泄漏。每次发送编码的帧数据大约为70MB,所以当avcodec_send_frame返回false时,会导致可怕的内存泄漏。 我想知道如何解决这个问题。
int ffmpeg_video_encode(int index)
{
int got_picture = 0;
int s = sws_scale(scxt, m_pFrameBGR->data, m_pFrameBGR->linesize, 0, m_pVideoCodecCtx->height, m_pFrameYUV->data, m_pFrameYUV->linesize);
m_pFrameYUV->data[0] = m_pYUVBuf;
m_pFrameYUV->data[1] = m_pYUVBuf + m_iYPlaneSize;
m_pFrameYUV->data[2] = m_pYUVBuf + m_iYPlaneSize + m_iYPlaneSize * 1 / 4;
m_pFrameYUV->pkt_pts = index * (m_videoStream->time_base.den) / ((m_videoStream->time_base.num) * m_iOutputVideoFPS);
//av_frame_make_writable(m_pFrameYUV);
av_init_packet(&m_pktVideo);
int ret = avcodec_send_frame(m_pVideoCodecCtx, m_pFrameYUV);
while (ret >= 0)
{
ret = avcodec_receive_packet(m_pVideoCodecCtx, &m_pktVideo);
if (AVERROR(EAGAIN) == ret || AVERROR_EOF == ret)
{
av_packet_unref(&m_pktVideo);
av_free_packet(&m_pktVideo);
return -1;
}
else if (ret < 0)
{
fprintf(stderr, "Error during encoding\n");
av_packet_unref(&m_pktVideo);
av_free_packet(&m_pktVideo);
return -1;
}
m_pktVideo.stream_index = m_videoStream->index;
ret = av_write_frame(m_pFormatCtx_v, &m_pktVideo);
av_packet_unref(&m_pktVideo);
av_free_packet(&m_pktVideo);
}
av_free_packet(&m_pktVideo);
return 0;
}