当前位置: 代码迷 >> 多媒体/流媒体开发 >> ffmpeg转h264为YUV文件,该怎么处理
  详细解决方案

ffmpeg转h264为YUV文件,该怎么处理

热度:3286   发布时间:2013-02-26 00:00:00.0
ffmpeg转h264为YUV文件
ffmpeg转h264为YUV文件,现在我得到的YUV播放的时候不正常,播放的时候上下抖动,并且有些有些乱七八糟的彩色条。这是怎么回事呢,等到高手解答。代码如下:


int SaveYuv(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
{
FILE *f;
int i;

f=fopen(filename,"ab+");  
for(i=0;i<ysize;i++)
{
fwrite(buf + i * wrap, 1, xsize, f ); 
}
fclose(f);
return 1;
}
static AVFrame *alloc_picture(PixelFormat pix_fmt, int width, int height) 


  AVFrame *picture;
  AVPicture *pp;
  uint8_t *picture_buf;
  int size;

  picture = avcodec_alloc_frame();
  if (!picture)
  return NULL;
  size = avpicture_get_size(pix_fmt, width, height);
  picture_buf = (uint8_t *)av_malloc(size);
  if (!picture_buf) {
  av_free(picture);
  return NULL;
  }
  avpicture_fill((AVPicture *)picture, picture_buf,
  pix_fmt, width, height);
  return picture;


int main()
{
  AVFormatContext *ic;
  AVCodecContext *vCodec;
  AVCodec *codec;

  av_register_all();
  ic=avformat_alloc_context();
  const char *fileName="test.h264";
  char *outputfilename="test3.yuv";
  FILE *ff=fopen(outputfilename,"ab+");

  if(avformat_open_input(&ic,fileName,NULL,0)!=0)
  {
  exit(1);
  }

  if(av_find_stream_info(ic)<0)
  {
  exit(1);
  }

  av_dump_format(ic,0,fileName,0);
  int videoindex=-1;
  for(int i=0;i<ic->nb_streams;i++)
  {
if(ic->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoindex=i;
break;
}
  }
  if(-1==videoindex)
  {
  exit(1);
  }

  vCodec=ic->streams[videoindex]->codec;
  codec=avcodec_find_decoder(vCodec->codec_id);

  if(!codec)
  {
  printf("cant find the decoder!\n");
exit(1);
  }

  if(avcodec_open(vCodec,codec)<0)
  {
  printf("cant open the decoder!\n");
  exit(1);
  }
   
  AVPacket packet;
  AVFrame *picture;
  static struct SwsContext *img_convert_ctx;
  AVFrame *pic_temp;
  int decodeframesize=0;
  int framefinished=0;
  int frame_num=0;
  static int sws_flags = SWS_BICUBIC; //差值算法

  //picture=avcodec_alloc_frame();
  
  
  av_init_packet(&packet);
   
  while(av_read_frame(ic,&packet)>=0 )
  {
if(packet.stream_index==videoindex)
{
picture=alloc_picture(vCodec->pix_fmt,vCodec->width,vCodec->height);
pic_temp=alloc_picture(vCodec->pix_fmt,vCodec->width,vCodec->height);

avcodec_decode_video2(vCodec,picture,&framefinished,&packet);
 
 
if(framefinished)
{
 
SaveYuv(picture->data[0], picture->linesize[0],vCodec->width, vCodec->height, outputfilename);
SaveYuv(picture->data[1], picture->linesize[1],vCodec->width/2, vCodec->height/2, outputfilename);
SaveYuv(picture->data[2], picture->linesize[2],vCodec->width/2, vCodec->height/2, outputfilename);
 

}
av_free(picture);
av_free(pic_temp);
}
av_free_packet(&packet);
  相关解决方案