gpt4 book ai didi

c++ - FFMPEG 解码 - 内存泄漏

转载 作者:太空狗 更新时间:2023-10-29 23:17:51 24 4
gpt4 key购买 nike

开发一个测试用的应用,遇到了一个错误。在处理数据包的同时,我遇到了一个非常可怕的问题,内存泄漏。

我认为 av_free_packet 应用正确(参见代码)。当我运行应用程序时,内存在播放音频文件的同时增长到 500MB,这是不正常的。 VLC 或 WMplayer (Windows Media Player) 只浪费 30/20mb 读取该文件。

代码如下:

static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = 0;
static AVFrame frame;
static bool first_time = true;

if(first_time){
first_time=false;
}

int len1, data_size = 0;

for(;;){
bool do_rt = false;

while(audio_pkt_size > 0){
int obt_frame = 0;

len1 = avcodec_decode_audio4(_audio_ccontext,&frame,&obt_frame,&pkt);
if(len1 < 0){
audio_pkt_size = 0;
break;
}
audio_pkt_data+=len1;
audio_pkt_size-=len1;
if(obt_frame){
data_size = av_samples_get_buffer_size(NULL,channel_count,sample_fr,_audio_ccontext->sample_fmt,1);
memcpy((int16_t*)audio_buf,frame.data[0],data_size);

}
if(data_size <= 0){
continue;
}
do_rt = true;
}

if(pkt.data){
//MessageBox(0,"hi","Hi",MB_OK); // This is only for test if the app si reaching this av_free_packet
av_free_packet(&pkt);
}

if(do_rt){
return data_size;
}

// Try to get a new packet
if(!audio_packets.empty()){
WaitForSingleObject(Queue_Audio_Mutex,INFINITE);
pkt = *audio_packets.front();
audio_packets.pop();
ReleaseMutex(Queue_Audio_Mutex);

audio_pkt_size = pkt.size;
audio_pkt_data = pkt.data;
}else{
return -1;
}
}
return 0;
}

非常感谢您的帮助。非常感谢。

最佳答案

我认为您也需要释放框架。根据 ffmpeg 引用

avcodec_decode_audio4()

解码器将通过调用 AVCodecContext.get_buffer2() 回调为解码帧分配缓冲区。当 AVCodecContext.refcounted_frames 设置为 1 时,帧被引用计数并且返回的引用属于调用者。当不再需要框架时,调用者必须使用 av_frame_unref() 释放框架。请点击链接。您可以尝试添加

av_frame_unref(&frame)

关于c++ - FFMPEG 解码 - 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16311034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com