专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 多媒体/流媒体开发

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

发布时间:2011-06-27 19:30:56 文章来源:www.iduyao.cn 采编人员:星星草
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;
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: