ffmpeg转h264为YUV文件

wangjian8855 2012-05-03 02:48:11
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);
}



avcodec_close(vCodec);
avformat_close_input(&ic);
fclose(ff);
printf("执行完毕\n");
system("pause");
return 0;

}
...全文
785 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
A1234567TAN 2013-06-22
  • 打赏
  • 举报
回复
编码后的264文件可能有问题
bestoml 2013-06-18
  • 打赏
  • 举报
回复
SaveYuv不对了,FFMPEG解码后的YUV有一个LINESIZE,每COPY完一行,跳跃的不是WIDTH,而是LINESIZE.要传入LINESIZE的信息.查下LINESIZE的信息你就明白的了.
chiwa737 2012-05-07
  • 打赏
  • 举报
回复
同意上面的观点,
用用别的解码器,或渲染器看看是不是正常
飞天舞 2012-05-04
  • 打赏
  • 举报
回复
是你的视频源的问题吧,解码数据不正确
rightorwrong 2012-05-04
  • 打赏
  • 举报
回复
用什么显示的,可能是你显示的代码和yuv格式不匹配
wangjian8855 2012-05-03
  • 打赏
  • 举报
回复
等到高手。。。。

2,542

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧