[FFmpeg-devel] [PATCH v2 3/3] swscale/utils: lazily allocate XYZ tables on CONFIG_SMALL
Niklas Haas
ffmpeg at haasn.xyz
Mon Dec 23 14:26:35 EET 2024
From: Niklas Haas <git at haasn.dev>
This has the downside of requiring these tables to be recomputed on every
init, but saves ~270 kB of static data.
Signed-off-by: Niklas Haas <git at haasn.dev>
---
libswscale/utils.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 82bb93e184..10b1f0701a 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -951,29 +951,42 @@ static int fill_xyztables(SwsInternal *c)
{1689, 1464, 739},
{ 871, 2929, 296},
{ 79, 488, 3891} };
+#if !CONFIG_SMALL
static uint16_t xyzgamma_tab[4096], rgbgammainv_tab[4096];
static uint16_t rgbgamma_tab[65536], xyzgammainv_tab[65536];
+#endif
+ if (c->xyzgamma)
+ return 0;
memcpy(c->xyz2rgb_matrix, xyz2rgb_matrix, sizeof(c->xyz2rgb_matrix));
memcpy(c->rgb2xyz_matrix, rgb2xyz_matrix, sizeof(c->rgb2xyz_matrix));
+
+#if CONFIG_SMALL
+ c->xyzgamma = av_malloc(sizeof(uint16_t) * 2 * (4096 + 65536));
+ if (!c->xyzgamma)
+ return AVERROR(ENOMEM);
+ c->rgbgammainv = c->xyzgamma + 4096;
+ c->rgbgamma = c->rgbgammainv + 4096;
+ c->xyzgammainv = c->rgbgamma + 65536;
+#else
c->xyzgamma = xyzgamma_tab;
c->rgbgamma = rgbgamma_tab;
c->xyzgammainv = xyzgammainv_tab;
c->rgbgammainv = rgbgammainv_tab;
-
if (xyzgamma_tab[4095])
return 0;
+#endif
/* set input gamma vectors */
for (i = 0; i < 4096; i++) {
- xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 65535.0);
- rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 65535.0);
+ c->xyzgamma[i] = lrint(pow(i / 4095.0, xyzgamma) * 65535.0);
+ c->rgbgammainv[i] = lrint(pow(i / 4095.0, rgbgammainv) * 65535.0);
}
/* set output gamma vectors */
for (i = 0; i < 65536; i++) {
- rgbgamma_tab[i] = lrint(pow(i / 65535.0, rgbgamma) * 4095.0);
- xyzgammainv_tab[i] = lrint(pow(i / 65535.0, xyzgammainv) * 4095.0);
+ c->rgbgamma[i] = lrint(pow(i / 65535.0, rgbgamma) * 4095.0);
+ c->xyzgammainv[i] = lrint(pow(i / 65535.0, xyzgammainv) * 4095.0);
}
return 0;
}
@@ -2529,6 +2542,9 @@ void sws_freeContext(SwsContext *sws)
av_freep(&c->gamma);
av_freep(&c->inv_gamma);
+#if CONFIG_SMALL
+ av_freep(&c->xyzgamma);
+#endif
av_freep(&c->rgb0_scratch);
av_freep(&c->xyz_scratch);
--
2.47.0
More information about the ffmpeg-devel
mailing list