3. libswscale: 用于picture格式/大小转换, 占用cpu挺高 :), 用起来很简单, 基本就是
sws = sws_getContext(....);
sws_scale(sws, ...)
4. libx264 压缩: 考虑主要用于互动, 所以使用 preset=fast, tune=zerolatency, 320x240, 10fps, 300kbps, jj实测延迟很低, 小于 100ms
使用的数据结构
struct Ctx
{
x264_t *x264;
x264_picture_t picture;
x264_param_t param;
void *output; // 用于保存编码后的完整帧
int output_bufsize, output_datasize;
int64_t pts; // 输入 pts
int64_t (*get_pts)(struct Ctx *);
int64_t info_pts, info_dts;
int info_key_frame;
int info_valid;
};
vc_open(...) 设置必要的参数, 打开编码器
void *vc_open (int width, int height)
{
Ctx *ctx = new Ctx;
// 设置编码属性
//x264_param_default(&ctx->param);
x264_param_default_preset(&ctx->param, "fast", "zerolatency");
ctx->param.i_width = width;
ctx->param.i_height = height;
ctx->param.b_repeat_headers = 1; // 重复SPS/PPS 放到关键帧前面
ctx->param.b_cabac = 1;
ctx->param.i_fps_num = 10;
ctx->param.i_fps_den = 1;
ctx->param.i_keyint_max = 30;
ctx->param.i_keyint_min = 10;
// rc
ctx->param.rc.i_rc_method = X264_RC_CRF;
ctx->param.rc.i_bitrate = 300;
//ctx->param.rc.f_rate_tolerance = 0.1;
//ctx->param.rc.i_vbv_max_bitrate = ctx->param.rc.i_bitrate * 1.3;
//ctx->param.rc.f_rf_constant = 600;
//ctx->param.rc.f_rf_constant_max = ctx->param.rc.f_rf_constant * 1.3;
#ifdef DEBUG
ctx->param.i_log_level = X264_LOG_WARNING;
#else
ctx->param.i_log_level = X264_LOG_NONE;
#endif // release
ctx->x264 = x264_encoder_open(&ctx->param);
if (!ctx->x264) {
fprintf(stderr, "%s: x264_encoder_open err/n", __func__);
delete ctx;
return 0;
}
x264_picture_init(&ctx->picture);
ctx->picture.img.i_csp = X264_CSP_I420;
ctx->picture.img.i_plane = 3;
ctx->output = malloc(128*1024);
ctx->output_bufsize = 128*1024;
ctx->output_datasize = 0;
ctx->get_pts = first_pts;
ctx->info_valid = 0;
return ctx;
}