swscale/graph: make _reinit() only call _init(), not _create()

This allows us to preserve the same memory allocation when
reinitializing a graph, which is a nice bonus.

Signed-off-by: Niklas Haas <git@haasn.dev>
This commit is contained in:
Niklas Haas
2026-05-11 19:39:02 +02:00
parent 56305c460c
commit 9fe0ff3d56
3 changed files with 22 additions and 14 deletions

View File

@@ -963,20 +963,18 @@ static int opts_equal(const SwsContext *c1, const SwsContext *c2)
}
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
int field, SwsGraph **out_graph)
int ff_sws_graph_reinit(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst,
const SwsFormat *src, int field)
{
SwsGraph *graph = *out_graph;
if (graph && ff_fmt_equal(&graph->src, src) &&
ff_fmt_equal(&graph->dst, dst) &&
opts_equal(ctx, &graph->opts_copy))
if (ff_fmt_equal(&graph->src, src) && ff_fmt_equal(&graph->dst, dst) &&
opts_equal(ctx, &graph->opts_copy))
{
ff_sws_graph_update_metadata(graph, &src->color);
return 0;
}
ff_sws_graph_free(out_graph);
return ff_sws_graph_create(ctx, dst, src, field, out_graph);
graph_uninit(graph);
return ff_sws_graph_init(graph, ctx, dst, src, field);
}
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)

View File

@@ -212,13 +212,14 @@ void ff_sws_graph_free(SwsGraph **graph);
void ff_sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
/**
* Wrapper around ff_sws_graph_create() that reuses the existing graph if the
* Wrapper around ff_sws_graph_init() that reuses the existing graph if the
* format is compatible. This will also update dynamic per-frame metadata.
* Must be called after changing any of the fields in `ctx`, or else they will
* have no effect.
*
* Must also be called after changing any of the fields in `ctx`, or else they
* will have no effect.
*/
int ff_sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
int field, SwsGraph **graph);
int ff_sws_graph_reinit(SwsGraph *graph, SwsContext *ctx, const SwsFormat *dst,
const SwsFormat *src, int field);
/**
* Dispatch the filter graph on a single field of the given frames. Internally

View File

@@ -1504,7 +1504,16 @@ int sws_frame_setup(SwsContext *ctx, const AVFrame *dst, const AVFrame *src)
goto fail;
}
ret = ff_sws_graph_reinit(ctx, &dst_fmt, &src_fmt, field, &s->graph[field]);
if (!s->graph[field]) {
s->graph[field] = ff_sws_graph_alloc();
if (!s->graph[field]) {
err_msg = "Failed allocating scaling graph";
ret = AVERROR(ENOMEM);
goto fail;
}
}
ret = ff_sws_graph_reinit(s->graph[field], ctx, &dst_fmt, &src_fmt, field);
if (ret < 0) {
err_msg = "Failed initializing scaling graph";
goto fail;