使用x264编码

首先,检查x264.h文件,该文件包含了一些你将用到的函数、结构的声明或定义。x264.c(你下载的源码包中可以找到该文件)中有一个简单的例子。大部分人说会告诉你以该例子为基础,但是我发现对于初学者来说,这个例子太复杂了,等有了一定基础再回头参考这个例子就很不错。

第一步,设置x264_param_t中的一些参数,一个不错的描述参数的网站是:http://mewiki.project357.com/wiki/X264_Settings。再看看x264_param_default_preset函数,它允许你只关注部分功能,对于其它参数它会自动帮你设置。接着使用函数x264_param_apply_profile(你可能想要”baseline”profile)。

下面是我代码中的一些例子:

x264_param_t param;
x264_param_default_preset(&param, "veryfast", "zerolatency");
param.i_threads = 1;
param.i_width = width;
param.i_height = height;
param.i_fps_num = fps;
param.i_fps_den = 1;
// Intra refres:
param.i_keyint_max = fps;
param.b_intra_refresh = 1;
//Rate control:
param.rc.i_rc_method = X264_RC_CRF;
param.rc.f_rf_constant = 25;
param.rc.f_rf_constant_max = 35;
//For streaming:
param.b_repeat_headers = 1;
param.b_annexb = 1;
x264_param_apply_profile(&paramm, "baseline");
之后你可以像下面这样初始化编码器:

x264_t* encoder = x264_encoder_open(&param);
x264_picture_t pic_in, pic_out;
x264_picture_alloc(&pic_in, X264_CSP_I420, w, h)
X264期望输入YUV420P数据(我猜也支持其它一些格式,但是这是最常用的格式)。你可以使用libswscale(ffmpeg中的)把图像转换到正确的格式。像这样初始化(我这里是把RGB data转换成24bpp)。

struct SwsContext* convertCtx = sws_getContext(in_w, in_h, PIX_FMT_RGB24, out_w, out_h, PIX_FMT_YUV420P, 
SWS_FAST_BILINEAR, NULL, NULL, NULL);

sws_getContext函数说明:

struct SwsContext*  sws_getContext (int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags, SwsFilter *srcFilter, 
SwsFilter *dstFilter, double *param)
Returns an SwsContext to be used in sws_scale .
Params: 
srcW, srcH, srcFormat : source width, height, and pix format
dstW, dstH, dstFormat : destination width, height, and pix format
flags : Method of scaling to use. Choices are SWS_FAST_BILINEAR, SWS_BILINEAR, SWS_BICUBIC, SWS_X, SWS_POINT, SWS_AREA, SWS_BICUBLIN, 
SWS_GAUSS, SWS_SINC, SWS_LANCZOS, SWS_SPLINE. Other flags include CPU capability flags: SWS_CPU_CAPS_MMX, SWS_CPU_CAPS_MMX2, 
SWS_CPU_CAPS_3DNOW, SWS_CPU_CAPS_ALTIVEC. Other flags include (currently not completely implemented) SWS_FULL_CHR_H_INT, SWS_FULL_CHR_H_INP, 
and SWS_DIRECT_BGR. Finally we have SWS_ACCURATE_RND and perhaps the most useful for beginners, SWS_PRINT_INFO. I have no idea what most of 
these do. Maybe email me?
srcFilter, dstFilter : SwsFilter for source and destination. SwsFilter involves chroma/luminsence filtering. A value of NULL sets these to the default.
param : should be a pointer to an int[2] buffer with coefficients. Not documented. Looks like it's used to alter the default scaling algorithms slightly. 
A value of NULL sets this to the default. Experts only!

之后,编码就像下面这样,对于没一帧:

//data is a pointer to you RGB structure
int srcstride = w*3; //RGB stride is just 3*width
sws_scale(convertCtx, &data, &srcstride, 0, h, pic_in.img.plane, pic_in.img.stride);
x264_nal_t* nals;
int i_nals;
int frame_size = x264_encoder_encode(encoder, &nals, &i_nals, &pic_in, &pic_out);
if (frame_size >= 0)
{
    // OK
}
如果使用其它参数,将可能会有一些延迟帧。如果有延迟帧,frame_size有时会返回0,你必须要一直调用 x264_encoder_encode s函数直到 x264_encoder_delayed_frame返回0。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值