ffmpeg - 正确的 AVFrame RGB 值

标签 ffmpeg rgb cairo libav libavcodec

我必须从 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 的混淆,我真的不明白。请随时询问更多详细信息。非常感谢。
wrong RGB values

最佳答案

您需要使用 libswscaleRGB24 转换源图像数据至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/

相关文章:

ios - 在IOS上使用FFMPEG mux flv和发送rtmp

css - 灰度到黑色不透明度转换

segmentation-fault - 为什么我的 Gdk/cairo 类会导致段错误?

c - Gtk3 和 cairo g_timeout_add 不起作用

image - ffmpeg:将多个图像叠加到视频中

android - 从java代码调用为Android编译的FFmpeg可执行文件的简单方法是什么?

python - Tensorflow CIELAB 颜色空间边界

python - 如何在 Bokeh 中显示 TIFF 图像?

gtk - X11:绘制叠加层/从叠加层中删除图纸

c# - 使用 FFMPEG(c# 应用程序)向视频添加文本