`
xpp02
  • 浏览: 1012721 次
社区版块
存档分类
最新评论

使用ffmpeg进行h.264编码

 
阅读更多

m_fmt->video_codec = CODEC_ID_H264;

/* 添加视频流 */
m_video_st = av_new_stream(m_oc, 0);
if (!m_video_st) {
return 0;
}
m_videoc=avcodec_alloc_context();
m_videoc = m_video_st->codec;

/* 视频相关参数 */
m_videoc->codec_id = m_fmt->video_codec;
m_videoc->codec_type = CODEC_TYPE_VIDEO;
m_videoc->bit_rate = video_bitrate*1000;
m_videoc->width = video_width;
m_videoc->height = video_height;
m_videoc->time_base.den = 25;
m_videoc->time_base.num = 1;
m_videoc->dct_algo = 0;
m_videoc->gop_size = 12;
m_videoc->me_pre_cmp=2;
m_videoc->cqp = 26;
m_videoc->me_method =7;
m_videoc->qmin = 3;
m_videoc->qmax = 31;
m_videoc->max_qdiff = 3;
m_videoc->qcompress = 0.5;
m_videoc->qblur = 0.5;
m_videoc->nsse_weight = 8;
m_videoc->i_quant_factor = (float)0.8;
m_videoc->b_quant_factor = 1.25;
m_videoc->b_quant_offset = 1.25;

m_videoc->pix_fmt = PIX_FMT_YUV420P;//当前YUV420

if (m_videoc->codec_id == CODEC_ID_MPEG2VIDEO) {
m_videoc->max_b_frames = 2;
}
if (m_videoc->codec_id == CODEC_ID_MPEG1VIDEO){
m_videoc->mb_decision=2;
}


if(!strcmp(m_oc->oformat->name, "mp4")
|| !strcmp(m_oc->oformat->name, "mov")
|| !strcmp(m_oc->oformat->name, "3gp"))
//m_videoc->flags |= CODEC_FLAG_GLOBAL_HEADER;
m_videoc->flags |=CODEC_FLAG_QSCALE;

/* 检查参数是否设置完毕*/
if (av_set_parameters(m_oc, NULL) < 0) {
return 0;
}
dump_format(m_oc, 0, filename, 1);

/* 所有视频设置完毕,打开视频解码器,并打开必要的编码缓冲器 */
if (m_video_st){

AVCodec *video_codec;

/* 查找视频解码器 */
video_codec = avcodec_find_encoder(m_videoc->codec_id);
if (!video_codec) {
return 0;
}

/* 打开视频解码器 */
if (avcodec_open(m_videoc, video_codec) < 0) {
return 0;
}

video_outbuf = NULL;
if (!(m_oc->oformat->flags & AVFMT_RAWPICTURE)) {
/* allocate output buffer */
/* XXX: API change will be done */
/* buffers passed into lav* can be allocated any way you prefer,
as long as they're aligned enough for the architecture, and
they're freed appropriately (such as using av_free for buffers
allocated with av_malloc) */
video_outbuf_size = 200000;
video_outbuf = (uint8_t *)av_malloc(video_outbuf_size);

}

/* allocate the encoded raw picture */
picture = alloc_picture(m_videoc->pix_fmt, m_videoc->width, m_videoc->height);
if (!picture) {
return 0;
}

/* if the output format is not YUV420P, then a temporary YUV420P
picture is needed too. It is then converted to the required
output format */
tmp_picture = NULL;
if (m_videoc->pix_fmt != PIX_FMT_YUV420P) {
tmp_picture = alloc_picture(PIX_FMT_YUV420P, m_videoc->width, m_videoc->height);
if (!tmp_picture) {
return 0;
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics