对 x264(libx264) 原始 yuv 帧数据进行编码
encode x264(libx264) raw yuv frame data
我正在尝试使用原始 YUV 帧数据对 MP4 视频进行编码,但我不确定如何填充平面数据(最好不使用其他库,如 ffmpeg)
帧数据已经用I420编码,不需要转换
这是我正在尝试做的事情:
const char *frameData = /* Raw frame data */;
x264_t *encoder = x264_encoder_open(¶m);
x264_picture_t imgInput, imgOutput;
x264_picture_alloc(&imgInput, X264_CSP_I420, width, height);
// how can I fill the struct data of imgInput
x264_nal_t *nals;
int i_nals;
int frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &imgInput, &imgOutput);
我找到的等效命令行是:
x264 --output video.mp4 --fps 15 --input-res 1280x800 imgdata_01.raw
但我不知道该应用程序是如何做到的。
谢谢。
查看 libx264 API 用法 example。此示例使用 fread() 将 x264_picture_alloc() 分配的帧填充为来自标准输入的实际 i420 数据。如果你已经在内存中有 i420 数据并且想跳过 memcpy 步骤而不是它你可以:
- 使用 x264_picture_init() 代替 x264_picture_alloc() 和 x264_picture_clean()。因为您不需要在堆上为帧数据分配内存。
- 填充x264_picture_t.img结构字段:
- i_csp = X264_CSP_I420;
- i_plane = 3;
- 平面[0] = 指向 Y 平面的指针;
- i_stride[0] = Y 平面的步幅(以字节为单位);
- plane[1] = 指向 U 平面的指针;
- i_stride[1] = U 平面的步幅(以字节为单位);
- plane[2] = 指向 V 平面的指针;
- i_stride[2] = V 平面的步幅(以字节为单位);
为了完成上述答案,这是一个填充 x264_picture_t
图片的示例。
int fillImage(uint8_t* buffer, int width, int height, x264_picture_t*pic){
int ret = x264_picture_alloc(pic, X264_CSP_I420, width, height);
if (ret < 0) return ret;
pic->img.i_plane = 3; // Y, U and V
pic->img.i_stride[0] = width;
// U and V planes are half the size of Y plane
pic->img.i_stride[1] = width / 2;
pic->img.i_stride[2] = width / 2;
int uvsize = ((width + 1) >> 1) * ((height + 1) >> 1);
pic->img.plane[0] = buffer; // Y Plane pointer
pic->img.plane[1] = buffer + (width * height); // U Plane pointer
pic->img.plane[2] = pic->img.plane[1] + uvsize; // V Plane pointer
return ret;
}
我正在尝试使用原始 YUV 帧数据对 MP4 视频进行编码,但我不确定如何填充平面数据(最好不使用其他库,如 ffmpeg)
帧数据已经用I420编码,不需要转换
这是我正在尝试做的事情:
const char *frameData = /* Raw frame data */;
x264_t *encoder = x264_encoder_open(¶m);
x264_picture_t imgInput, imgOutput;
x264_picture_alloc(&imgInput, X264_CSP_I420, width, height);
// how can I fill the struct data of imgInput
x264_nal_t *nals;
int i_nals;
int frameSize = x264_encoder_encode(encoder, &nals, &i_nals, &imgInput, &imgOutput);
我找到的等效命令行是:
x264 --output video.mp4 --fps 15 --input-res 1280x800 imgdata_01.raw
但我不知道该应用程序是如何做到的。
谢谢。
查看 libx264 API 用法 example。此示例使用 fread() 将 x264_picture_alloc() 分配的帧填充为来自标准输入的实际 i420 数据。如果你已经在内存中有 i420 数据并且想跳过 memcpy 步骤而不是它你可以:
- 使用 x264_picture_init() 代替 x264_picture_alloc() 和 x264_picture_clean()。因为您不需要在堆上为帧数据分配内存。
- 填充x264_picture_t.img结构字段:
- i_csp = X264_CSP_I420;
- i_plane = 3;
- 平面[0] = 指向 Y 平面的指针;
- i_stride[0] = Y 平面的步幅(以字节为单位);
- plane[1] = 指向 U 平面的指针;
- i_stride[1] = U 平面的步幅(以字节为单位);
- plane[2] = 指向 V 平面的指针;
- i_stride[2] = V 平面的步幅(以字节为单位);
为了完成上述答案,这是一个填充 x264_picture_t
图片的示例。
int fillImage(uint8_t* buffer, int width, int height, x264_picture_t*pic){
int ret = x264_picture_alloc(pic, X264_CSP_I420, width, height);
if (ret < 0) return ret;
pic->img.i_plane = 3; // Y, U and V
pic->img.i_stride[0] = width;
// U and V planes are half the size of Y plane
pic->img.i_stride[1] = width / 2;
pic->img.i_stride[2] = width / 2;
int uvsize = ((width + 1) >> 1) * ((height + 1) >> 1);
pic->img.plane[0] = buffer; // Y Plane pointer
pic->img.plane[1] = buffer + (width * height); // U Plane pointer
pic->img.plane[2] = pic->img.plane[1] + uvsize; // V Plane pointer
return ret;
}