Skip to content

Commit 6da940e

Browse files
committed
swscale/graph: allow dynamically updating HDR metadata
Without triggering a full graph reinit.
1 parent efff80c commit 6da940e

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

libswscale/graph.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ static void free_lut3d(void *priv)
475475
sws_lut3d_free(&lut);
476476
}
477477

478+
static void setup_lut3d(const SwsImg *out, const SwsImg *in, const SwsPass *pass)
479+
{
480+
SwsLut3D *lut = pass->priv;
481+
482+
/* Update dynamic frame metadata from the original source frame */
483+
sws_lut3d_update(lut, &pass->graph->src.color);
484+
}
485+
478486
static void run_lut3d(const SwsImg *out_base, const SwsImg *in_base,
479487
int y, int h, const SwsPass *pass)
480488
{
@@ -543,6 +551,7 @@ static int adapt_colors(SwsGraph *graph, SwsFormat src, SwsFormat dst,
543551
sws_lut3d_free(&lut);
544552
return AVERROR(ENOMEM);
545553
}
554+
pass->setup = setup_lut3d;
546555
pass->free = free_lut3d;
547556

548557
*output = pass;
@@ -678,16 +687,26 @@ static int opts_equal(const SwsContext *c1, const SwsContext *c2)
678687
int sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
679688
int field, SwsGraph **out_graph)
680689
{
681-
const SwsGraph *graph = *out_graph;
690+
SwsGraph *graph = *out_graph;
682691
if (graph && ff_fmt_equal(&graph->src, src) &&
683692
ff_fmt_equal(&graph->dst, dst) &&
684693
opts_equal(ctx, &graph->opts_copy))
694+
{
695+
sws_graph_update_metadata(graph, &src->color);
685696
return 0;
697+
}
686698

687699
sws_graph_free(out_graph);
688700
return sws_graph_create(ctx, dst, src, field, out_graph);
689701
}
690702

703+
void sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color)
704+
{
705+
if (!color)
706+
return;
707+
708+
ff_color_update_dynamic(&graph->src.color, color);
709+
}
691710

692711
void sws_graph_run(SwsGraph *graph, uint8_t *const out_data[4],
693712
const int out_linesize[4],

libswscale/graph.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,15 @@ int sws_graph_create(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src
134134
void sws_graph_free(SwsGraph **graph);
135135

136136
/**
137-
* Wrapper around sws_graph_create that does nothing if the format is
138-
* unchanged. Must be called after changing any of the fields in `ctx`, or else
139-
* they will have no effect.
137+
* Update dynamic per-frame HDR metadata without requiring a full reinit.
138+
*/
139+
void sws_graph_update_metadata(SwsGraph *graph, const SwsColor *color);
140+
141+
/**
142+
* Wrapper around sws_graph_create() that reuses the existing graph if the
143+
* format is compatible. This will also update dynamic per-frame metadata.
144+
* Must be called after changing any of the fields in `ctx`, or else they will
145+
* have no effect.
140146
*/
141147
int sws_graph_reinit(SwsContext *ctx, const SwsFormat *dst, const SwsFormat *src,
142148
int field, SwsGraph **graph);

libswscale/utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ typedef struct SwsColor {
6565
AVRational frame_avg; /* per-frame/scene average luminance, or 0 */
6666
} SwsColor;
6767

68+
static inline void ff_color_update_dynamic(SwsColor *dst, const SwsColor *src)
69+
{
70+
dst->frame_peak = src->frame_peak;
71+
dst->frame_avg = src->frame_avg;
72+
}
73+
6874
/* Subset of AVFrame parameters that uniquely determine pixel representation */
6975
typedef struct SwsFormat {
7076
int width, height;

0 commit comments

Comments
 (0)