x264编码实时视频流

春已暖花已开 2018-07-03 03:57:47
我使用x264编码实时视频流,只能编码一帧数据,不再进行第二帧数据的编码。放在一个while循环里面,只循环一次。while循环本身没问题,注释掉x264_encoder_encode()可以正常循环。我怀疑是X264初始化设置参数问题。以下是我设置的初始化参数:
int encoder_init()
{

//encode_data.frame_num=0;
encode_data.csp = X264_CSP_I420; //设置视频采样格式为YUV420 planar模式
encode_data.fp_dst = fopen("video.h264", "wb");
encode_data.pNals = NULL;
encode_data.pHandle = NULL;
encode_data.pPic_in = (x264_picture_t*)malloc(sizeof(x264_picture_t));
encode_data.pPic_out = (x264_picture_t*)malloc(sizeof(x264_picture_t));
encode_data.pParam = (x264_param_t *)malloc(sizeof(x264_param_t));

//check
if( encode_data.fp_dst == NULL){
printf("Error open files.\n");
return -1;
}
x264_param_default_preset(encode_data.pParam, "fast" , "zerolatency" );
//x264_param_default(encode_data.pParam);//设置参数集结构体x264_param_t的缺省值,对编码器进行参数设定
encode_data.pParam->i_width = 1280;
//printf("width:%d\n",encode_data.pParam->i_width);
encode_data.pParam->i_height = 720;
//printf("height:%d\n",encode_data.pParam->i_height);
encode_data.pParam->i_threads = 1;//不使用并行编码
encode_data.pParam->b_sliced_threads = 1;//slice并行编码
encode_data.pParam->rc.i_lookahead = 0;//无延时编码
encode_data.pParam->rc.i_rc_method = X264_RC_CQP;//参数i_rc_method表示码率控制,CQP(恒定质量),CRF(恒定码率),ABR(平均码率)
encode_data.pParam->rc.b_mb_tree = 0;//这个不为0,将导致编码延时帧,在实时编码时,必须为0
encode_data.pParam->b_annexb = 0;//如果设置了该项,则在每个NAL单元前加一个四字节的前缀符
encode_data.pParam->b_repeat_headers = 1;//在每个关键帧I帧前添加sps和pps,实时视频传输需要enable
//encode_data.pParam->i_frame_total = 0;//编码的桢数,不知道用0
encode_data.pParam->i_log_level = X264_LOG_DEBUG;//LOG参数,


encode_data.pParam->i_csp = X264_CSP_I420;//设置彩色空间,通常取值 X264_CSP_I420
encode_data.pParam->i_fps_den = 1;//帧率分母
encode_data.pParam->i_fps_num = 20;//帧率分子
//encode_data.pParam->analyse.i_mv_range_thread = -1;
//encode_data.pParam->i_sync_lookahead = X264_SYNC_LOOKAHEAD_AUTO;
encode_data.pParam->rc.i_bitrate = 1024 * 10;//码率参数
//profile level ,total 6 high444
x264_param_apply_profile(encode_data.pParam, x264_profile_names[0]);
encode_data.pHandle = x264_encoder_open(encode_data.pParam);//创建编码器句柄
//init in and out struct
x264_picture_init(encode_data.pPic_out);//初始化并设定 x264_picture_t

x264_picture_alloc(encode_data.pPic_in, X264_CSP_I420, encode_data.pParam->i_width, encode_data.pParam->i_height);

encode_data.y_size = 1280*720;

return 0;
}

编码部分程序:
memcpy(encode_data.pPic_in->img.plane[0], yuv_vsdk.data, encode_data.y_size); // Y
for(int j; j<5;j++){
printf("encode data 0:%u\n",(encode_data.pPic_in->img.plane[0])[j]);
}
if(encode_data.pPic_in->img.plane[0] == NULL)
{
printf("Y is NULL\n");
}
memcpy(encode_data.pPic_in->img.plane[1], yuv_vsdk.data+encode_data.y_size, encode_data.y_size/4); //U

for(int j; j<5;j++){
printf("encode data 1:%u\n",(encode_data.pPic_in->img.plane[1])[j]);
}
if(encode_data.pPic_in->img.plane[1] == NULL)
{
printf("U is NULL\n");

}
memcpy(encode_data.pPic_in->img.plane[2], yuv_vsdk.data+encode_data.y_size*5/4, encode_data.y_size/4); //V
for(int j; j<5;j++){
printf("encode data 2:%u\n",(encode_data.pPic_in->img.plane[2])[j]);
}

if(encode_data.pPic_in->img.plane[2] == NULL)
{
printf("V is NULL\n");
}

encode_data.iNal = 0;
encode_data.pNals = NULL;
encode_data.pPic_in->i_pts = i;
printf("i= %d\n",i);
ret = x264_encoder_encode(encode_data.pHandle, &encode_data.pNals, &encode_data.iNal, encode_data.pPic_in, encode_data.pPic_out);
i++;
printf("I am %d\n",i);

//printf(" thread:%u\n", encode_data.pHandle->lookahead->b_exit_thread);
printf("ret:%d\n",ret);
//printf("encoder error.\n");
if (ret< 0){
printf("Error.\n");
return -1;
}
//encode_data.pPic_in->i_pts = i;
printf("inal=%d\n",encode_data.iNal);

for (int j = 0; j < encode_data.iNal; j++){
fwrite(encode_data.pNals[j].p_payload, 1, encode_data.pNals[j].i_payload, encode_data.fp_dst);
memset(encode_data.pNals[j].p_payload,0,sizeof(uint8_t));
printf("write\n");
}

编译运行会提示一个bug:
x264 [debug]: frame= 0 QP=20.00 NAL=3 Slice:I Poc:0 I:3600 P:0 SKIP:0 size=261661 bytes
编译运行的info如下:
x264 [info]: frame I:1 Avg QP:20.00 size:261661
x264 [info]: mb I I16..4: 5.5% 0.0% 94.5%
x264 [info]: coded y,uvDC,uvAC intra: 96.4% 96.8% 96.8%
x264 [info]: i16 v,h,dc,p: 54% 3% 34% 10%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 8% 9% 11% 15% 13% 5% 20% 4% 15%
x264 [info]: i8c dc,h,v,p: 79% 3% 5% 13%
x264 [info]: kb/s:41865.76
...全文
229 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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