FFMPEG:压缩之H264编码(YUV420P->H264) 分类: ...

720*576@25hz,550帧的yuv420p数据,编码时间13.3秒。


FFMPEG:压缩之H264编码(YUV420P->H264) 分类: ..._第1张图片
void CTest0Dlg::OnButton5() 
{
// TODO: Add your control notification handler code here
int nWidth = 720;
int nHeight= 576;

av_register_all();
avcodec_register_all();
AVFrame *m_pYUVFrame = new AVFrame[1];;  //YUV帧数据
AVCodecContext *c= NULL;
AVCodec *pCodecH264; //编码器
uint8_t * yuv_buff;//

//查找h264编码器
pCodecH264 = avcodec_find_encoder(CODEC_ID_H264);
if(!pCodecH264)
{
 fprintf(stderr, "h264 codec not found\n");
 exit(1);
}

c= avcodec_alloc_context();
c->bit_rate = 1000000;// put sample parameters 
c->width =720;// 
c->height = 576;// 

// frames per second 
AVRational rate;
rate.num = 1;
rate.den = 25;
c->time_base= rate;//(AVRational){1,25};
c->gop_size = 10; // emit one intra frame every ten frames 
c->max_b_frames=1;
c->thread_count = 1;
c->pix_fmt = PIX_FMT_YUV420P;//PIX_FMT_RGB24;

//av_opt_set(c->priv_data, /*"preset"*/"libvpx-1080p.ffpreset", /*"slow"*/NULL, 0);
//打开编码器
if(avcodec_open(c,pCodecH264)<0)
 TRACE("不能打开编码库");

int size = c->width * c->height;
yuv_buff = (uint8_t *) malloc((size * 3) / 2); // size for YUV 420 

//图象编码
int outbuf_size=100000;
uint8_t * outbuf= (uint8_t*)malloc(outbuf_size); 
int u_size = 0;
FILE *f=NULL; 
char * filename = "e:\\pic\\000.264";
f = fopen(filename, "wb");
if (!f)
{
 TRACE( "could not open %s\n", filename);
 exit(1);
}

AVPacket avpkt;
    FILE *fp;
fp = fopen("d:\\temp\\VIDEO720576.yuv","rb+");

//AVFrame *pTFrame=new AVFrame
while (1)
{
 int len = fread(yuv_buff,1,nWidth* nHeight*3/2,fp);
 if (len==0)
 {
 break;
 }
 avpicture_fill((AVPicture*)m_pYUVFrame, (uint8_t*)yuv_buff, PIX_FMT_YUV420P, nWidth, nHeight);
 int got_packet_ptr = 0;
 av_init_packet(&avpkt);
 avpkt.data = outbuf;
 avpkt.size = outbuf_size;
while(1)
{
 u_size = avcodec_encode_video(c, outbuf, outbuf_size, m_pYUVFrame);


 if (u_size > 0 && u_size<100000)
 {
fwrite(avpkt.data, 1, u_size, f);
break;
 }
}
}

fclose(f); 
fclose(fp); 
delete []m_pYUVFrame;
free(outbuf);
avcodec_close(c);
av_free(c);
MessageBox("over");

}

http://download.csdn.net/detail/mao0514/8202691


版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/mao0504/p/4706477.html

你可能感兴趣的:(FFMPEG:压缩之H264编码(YUV420P->H264) 分类: ...)