我必须从 cairo 表面像素数据中填充 ffmpeg AVFrame->data。我有这个代码:
/* Image info and pixel data */
width = cairo_image_surface_get_width( surface );
height = cairo_image_surface_get_height( surface );
stride = cairo_image_surface_get_stride( surface );
pix = cairo_image_surface_get_data( surface );
for( row = 0; row < height; row++ )
{
data = pix + row * stride;
for( col = 0; col < width; col++ )
{
img->video_frame->data[0][row * img->video_frame->linesize[0] + col] = data[0];
img->video_frame->data[1][row * img->video_frame->linesize[1] + col] = data[1];
//img->video_frame->data[2][row * img->video_frame->linesize[2] + col] = data[2];
data += 4;
}
img->video_frame->pts++;
}
但是导出的视频中的颜色是错误的。原来的心是红色的。有人可以指出我正确的方向吗?遗憾的是,encode.c 示例毫无用处,并且在 Internet 上存在很多关于 Y、Cr 和 Cb 的混淆,我真的不明白。请随时询问更多详细信息。非常感谢。最佳答案
您需要使用 libswscale
从 RGB24
转换源图像数据至YUV420P
.
就像是:
int width = cairo_image_surface_get_width( surface );
int height = cairo_image_surface_get_height( surface );
int stride = cairo_image_surface_get_stride( surface );
uint8_t *pix = cairo_image_surface_get_data( surface );
uint8_t *data[1] = { pix };
int linesize[1] = { stride };
struct SwsContext *sws_ctx = sws_getContext(width, height, AV_PIX_FMT_RGB24 ,
width, height, AV_PIX_FMT_YUV420P,
SWS_BILINEAR, NULL, NULL, NULL);
sws_scale(sws_ctx, data, linesize, 0, height,
img->video_frame->data, img->video_frame->linesize);
sws_freeContext(sws_ctx);
请参阅此处的示例:scaling_video
关于ffmpeg - 正确的 AVFrame RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70247548/