gpt4 book ai didi

c - LibAV/FFMpeg 中的叠加过滤器在 C 中返回奇怪的(三重)帧

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:25 25 4
gpt4 key购买 nike

我试图制作一个合并两个帧的程序。我在 Windows 7 64 和 Visual Studio 2013 下使用 LibAV (libav-win32-20140428)。但结果很奇怪。

http://oi58.tinypic.com/rcobnm.jpg

使用的滤镜是 Overlay。当我将图表更改为仅使用一个流并添加 FADE 效果的图表时,一切都像魅力一样。但是 OVERLAY 和例如。 DRAWBOX 给我奇怪的失真(三帧合一和黑白效果)。这是代码:

static int init_filter_graph(AVFilterGraph **pGraph, AVFilterContext **pSrc1, AVFilterContext **pSink)
{
AVFilterGraph* tFilterGraph;
AVFilterContext* tBufferContext1;
AVFilter* tBuffer1;
AVFilterContext* tColorContext;
AVFilter* tColor;
AVFilterContext* tOverlayContext;
AVFilter* tOverlay;
AVFilterContext* tBufferSinkContext;
AVFilter* tBufferSink;

int tError;

/* Create a new filtergraph, which will contain all the filters. */
tFilterGraph = avfilter_graph_alloc();

if (!tFilterGraph) {
return -1;
}

{ // BUFFER FILTER 1
tBuffer1 = avfilter_get_by_name("buffer");
if (!tBuffer1) {
return -1;
}
tBufferContext1 = avfilter_graph_alloc_filter(tFilterGraph, tBuffer1, "src1");
if (!tBufferContext1) {
return -1;
}

av_dict_set(&tOptionsDict, "width", "320", 0);
av_dict_set(&tOptionsDict, "height", "240", 0);
av_dict_set(&tOptionsDict, "pix_fmt", "bgr24", 0);
av_dict_set(&tOptionsDict, "time_base", "1/25", 0);
av_dict_set(&tOptionsDict, "sar", "1", 0);
tError = avfilter_init_dict(tBufferContext1, &tOptionsDict);
av_dict_free(&tOptionsDict);
if (tError < 0) {
return tError;
}
}

{ // COLOR FILTER
tColor = avfilter_get_by_name("color");
if (!tColor) {
return -1;
}
tColorContext = avfilter_graph_alloc_filter(tFilterGraph, tColor, "color");
if (!tColorContext) {
return -1;
}

av_dict_set(&tOptionsDict, "color", "white", 0);
av_dict_set(&tOptionsDict, "size", "20x120", 0);
av_dict_set(&tOptionsDict, "framerate", "1/25", 0);
tError = avfilter_init_dict(tColorContext, &tOptionsDict);
av_dict_free(&tOptionsDict);
if (tError < 0) {
return tError;
}
}

{ // OVERLAY FILTER
tOverlay = avfilter_get_by_name("overlay");
if (!tOverlay) {
return -1;
}
tOverlayContext = avfilter_graph_alloc_filter(tFilterGraph, tOverlay, "overlay");
if (!tOverlayContext) {
return -1;
}

av_dict_set(&tOptionsDict, "x", "0", 0);
av_dict_set(&tOptionsDict, "y", "0", 0);
av_dict_set(&tOptionsDict, "main_w", "120", 0);
av_dict_set(&tOptionsDict, "main_h", "140", 0);
av_dict_set(&tOptionsDict, "overlay_w", "320", 0);
av_dict_set(&tOptionsDict, "overlay_h", "240", 0);
tError = avfilter_init_dict(tOverlayContext, &tOptionsDict);
av_dict_free(&tOptionsDict);
if (tError < 0) {
return tError;
}
}

{ // BUFFERSINK FILTER
tBufferSink = avfilter_get_by_name("buffersink");
if (!tBufferSink) {
return -1;
}

tBufferSinkContext = avfilter_graph_alloc_filter(tFilterGraph, tBufferSink, "sink");
if (!tBufferSinkContext) {
return -1;
}

tError = avfilter_init_str(tBufferSinkContext, NULL);
if (tError < 0) {
return tError;
}
}

// Linking graph
tError = avfilter_link(tBufferContext1, 0, tOverlayContext, 0);
if (tError >= 0) {
tError = avfilter_link(tColorContext, 0, tOverlayContext, 1);
}
if (tError >= 0) {
tError = avfilter_link(tOverlayContext, 0, tBufferSinkContext, 0);
}
if (tError < 0) {
return tError;
}

tError = avfilter_graph_config(tFilterGraph, NULL);
if (tError < 0) {
return tError;
}

*pGraph = tFilterGraph;
*pSrc1 = tBufferContext1;
*pSink = tBufferSinkContext;

return 0;
}

你认为是什么原因?

最佳答案

我在 LibAV's mailing list 得到了 Luca Barbato 的提示。 :

Can you try to reproduce that with avconv? From the image looks like you are getting a pixel format you are not really expecting.

lu

然后我发现,我需要使用 Format Filter , 以保持像素的格式相同。所以我补充说:

{ // FORMAT FILTER
tFormat = avfilter_get_by_name("format");
if (!tFormat) {
// Could not find the tFormat filter.
return -1;
}

tFormatContext = avfilter_graph_alloc_filter(tFilterGraph, tFormat, "format");
if (!tFormatContext) {
// Could not allocate the tFormat instance.
return -1;
}

av_dict_set(&tOptionsDict, "pix_fmts", "bgr24", 0);
tError = avfilter_init_dict(tFormatContext, &tOptionsDict);
av_dict_free(&tOptionsDict);
if (tError < 0) {
// Could not initialize the tFormat filter.
return tError;
}
}

init_filter_graph 函数中。

现在我的过滤图如下所示:

tError = avfilter_link(tBufferContext1, 0, tOverlayContext, 0);
if (tError >= 0)
{
tError = avfilter_link(tColorContext, 0, tOverlayContext, 1);
}
if (tError >= 0)
{
tError = avfilter_link(tOverlayContext, 0, tFormatContext, 0);
}
if (tError >= 0)
{
tError = avfilter_link(tFormatContext, 0, tBufferSinkContext, 0);
}
if (tError < 0) { // Error connecting filters.
return tError;
}

失真消失了,颜色又回来了!

enter image description here

关于c - LibAV/FFMpeg 中的叠加过滤器在 C 中返回奇怪的(三重)帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com