首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > 多媒体 >

ffmpeg解码 内存增加,该如何解决

2012-03-23 
ffmpeg解码 内存增加void main(){av_register_all()//注册库中所有可能有用的文件格式和编码器testff()t

ffmpeg解码 内存增加
void main()
{
av_register_all();//注册库中所有可能有用的文件格式和编码器

testff();

testff();//每重新调用一次,内存增加

testff();

}
void testff()
{  
  const char *filename="test.264";




AVFormatContext *ic;
//ic=av_alloc_format_context();
//打开文件
if (av_open_input_file(&ic,filename,NULL,0,NULL)!=0)
{
printf("can not open file%s\n",filename);
exit(1);
}
  //取出流信息
if (av_find_stream_info(ic)<0)
{
printf("can not find suitable codec parameters\n");
exit(1);
}

int i;
int videoindex=-1;
for (i=0;i<ic->nb_streams;i++)
{
if (ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
videoindex=i;
}

}
if (videoindex==-1)
{
printf("can not find video stream\n");
exit(1);
}

AVCodecContext *vCodeCtx;
vCodeCtx=ic->streams[videoindex]->codec;//取得视频编码的上下文指针
AVCodec *vCodec;
vCodec=avcodec_find_decoder(vCodeCtx->codec_id);//寻找合适编码器
if (vCodec==NULL)
{
printf("cannot find suitable video decoder\n");
exit(1);
}
//打开该视频编码器
if (avcodec_open(vCodeCtx,vCodec)<0)
{
printf("cannot open the video decoder\n");
exit(1);
}


AVPacket pack;  
int frameFinished;
int len;
int ret;

//////////////////////////////////////////////////////////////////////////
// Allocate an AVFrame structure
  AVFrame *pFrameRGB=avcodec_alloc_frame();
AVFrame *pFrameEnc= avcodec_alloc_frame();
  if(pFrameRGB==NULL)
  return -1;


  // Determine required buffer size and allocate buffer
int numBytes=avpicture_get_size(PIX_FMT_RGB24, vCodeCtx->width,vCodeCtx->height);
unsigned char* bufferRGB=(unsigned char *)malloc(numBytes);


// Assign appropriate parts of buffer to image planes i n pFrameRGB
avpicture_fill((AVPicture *)pFrameRGB, bufferRGB, PIX_FMT_RGB24,
  vCodeCtx->width, vCodeCtx->height);


  i=0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//读取输入视频,进行处理
/////////////////////////////////////////////////////////////////////////////////////////////////////////

while (av_read_frame(ic,&pack)>=0)//从输入文件读取一个包
  {
  if (pack.stream_index==videoindex)//判断是否为当前视频流中的包
{

len=avcodec_decode_video(vCodeCtx,pFrameEnc,&frameFinished,pack.data,pack.size);

if (len<0)
{
printf("error while decoding\n");
exit(0);
}
if (frameFinished)
{

// encode the image 
// Convert the image into YUV format that SDL uses
static struct SwsContext *img_convert_ctx;
if(img_convert_ctx == NULL) 
{
int w = vCodeCtx->width;
int h = vCodeCtx->height;

img_convert_ctx = sws_getContext(w, h, 
vCodeCtx->pix_fmt, 
w, h, PIX_FMT_RGB24, SWS_BICUBIC,
NULL, NULL, NULL);
if(img_convert_ctx == NULL) 
{
fprintf(stderr, "Cannot initialize the conversion context!\n");
exit(1);
}
}
int ret = sws_scale(img_convert_ctx, pFrameEnc->data, pFrameEnc->linesize, 0, 
vCodeCtx->height, pFrameRGB->data, pFrameRGB->linesize);
#if 0 // this use to be true, as of 1/2009, but apparently it is no longer true in 3/2009
if(ret) {
fprintf(stderr, "SWS_Scale failed [%d]!\n", ret);
continue;


}
#endif
//if videoindex

  }
  }
  
  av_free_packet(&pack);
 }


 //关闭解码器
avcodec_close(vCodeCtx);
//内存释放部分
av_free(pFrameEnc);

av_free(pFrameRGB->data[0]);
av_free(pFrameRGB);
av_close_input_file(ic);
  av_free(ic);

}

[解决办法]
看看ffmpeg的Demo程序,是如何释放程序的

你可以一点一点注释看是哪个内存泄露了
[解决办法]
应该有内存泄漏吧,不然不会这么费内存的

热点排行