原因:发现在调用编码时没有指定帧的pts值,故单独调用libx264模块在此验证pts值的影响
概述:首先编译libx264源码,将生成的lib和.h文件加载的工程中进行调用。
流程:
1:首先定义x264_param_t对象,该对象主要用于配置编码器参数信息。其中不同的preset和tune以及profile内部将会配置不同的参数信息,最终实现编码速度和编码质量的平衡。
//设置preset和tune
x264_param_default_preset(¶m, "fast", nullptr);
//设置profile
x264_param_apply_profile(¶m, "baseline")
//常规设置
param.i_csp = X264_CSP_I420;
param.i_width = iW;
param.i_height = iH;
2:创建输入图像结构体,用于添加输入数据,数据填充格式需要和设置相同
x264_picture_t pic,pout;
x264_picture_alloc(&pic, param.i_csp, param.i_width, param.i_height)
3:创建编码器
x264_t *h = x264_encoder_open(¶m);
4:填充数据进行编码,编码前可以指定帧类型,和pts时间戳,如果不指定时间戳则默认pts和dts编码后都为0.最终编码数据存放在x264_nal_t对象中.
fread(pic.img.plane[0], 1, iW *iH, pSrc);
fread(pic.img.plane[1], 1, iW*iH / 4, pSrc);
fread(pic.img.plane[2], 1, iW*iH / 4, pSrc);
pic.i_type = X264_TYPE_IDR;//指定IDR帧
pic.i_pts = iT;//指定时间戳
x264_encoder_encode(h, &pnal, &nal_t, &pic, &pic_out);
可以看出在x264_nal_t结构体中包含帧类型,以及数据地址
typedef struct x264_nal_t
{
int i_ref_idc; /* nal_priority_e */
int i_type; /* nal_unit_type_e */
int b_long_startcode;
int i_first_mb; /* If this NAL is a slice, the index of the first MB in the slice. */
int i_last_mb; /* If this NAL is a slice, the index of the last MB in the slice. */
/* Size of payload (including any padding) in bytes. */
int i_payload;
/* If param->b_annexb is set, Annex-B bytestream with startcode.
* Otherwise, startcode is replaced with a 4-byte size.
* This size is the size used in mp4/similar muxing; it is equal to i_payload-4 */
uint8_t *p_payload;
/* Size of padding in bytes. */
int i_padding;
} x264_nal_t;
5:刷新解码器,读取剩余数据,其中x264_encoder_delayed_frames返回缓冲的帧数量,其中与参考帧之间b帧数量(i_brame)和
宏块编码参考帧(i_lookahead),线程个数(i_thread_frames),线程预测参考帧(i_sync_lookahead)等有关.
while (x264_encoder_delayed_frames(h))
{
x264_encoder_encode(h, &pnal, &nal_t, nullptr, &pic_out)
}
6:关闭设备
x264_encoder_close(h);
x264_picture_clean(&pic);
总结:
以上就是通过libx264的编码调用,其中通过不同的profile及preset就可以满足大部分参数配置,如果需要减少编码时间和提高编码质量则需要手动指定参数。