[FFmpeg-cvslog] swscale/utils: Fix potential race when initializing xyz tables
Andreas Rheinhardt
git at videolan.org
Tue May 27 15:05:21 EEST 2025
ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Sat May 24 16:02:04 2025 +0200| [54c865fbec6f5914b0c041bff8c15b63b14964f1] | committer: Andreas Rheinhardt
swscale/utils: Fix potential race when initializing xyz tables
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=54c865fbec6f5914b0c041bff8c15b63b14964f1
---
libswscale/utils.c | 53 +++++++++++++++++++++++++++++++----------------------
1 file changed, 31 insertions(+), 22 deletions(-)
diff --git a/libswscale/utils.c b/libswscale/utils.c
index f659e22fdc..94a47ea5d0 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -692,13 +692,35 @@ static void fill_rgb2yuv_table(SwsInternal *c, const int table[4], int dstRange)
AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
}
-static int fill_xyztables(SwsInternal *c)
+#if CONFIG_SMALL
+static void init_xyz_tables(uint16_t xyzgamma_tab[4096], uint16_t xyzgammainv_tab[65536],
+ uint16_t rgbgamma_tab[65536], uint16_t rgbgammainv_tab[4096])
+#else
+static uint16_t xyzgamma_tab[4096], rgbgammainv_tab[4096];
+static uint16_t rgbgamma_tab[65536], xyzgammainv_tab[65536];
+static av_cold void init_xyz_tables(void)
+#endif
{
- int i;
- double xyzgamma = XYZ_GAMMA;
- double rgbgamma = 1.0 / RGB_GAMMA;
+ double xyzgamma = XYZ_GAMMA;
+ double rgbgamma = 1.0 / RGB_GAMMA;
double xyzgammainv = 1.0 / XYZ_GAMMA;
double rgbgammainv = RGB_GAMMA;
+
+ /* set input gamma vectors */
+ for (int 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);
+ }
+
+ /* set output gamma vectors */
+ for (int 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);
+ }
+}
+
+static int fill_xyztables(SwsInternal *c)
+{
static const int16_t xyz2rgb_matrix[3][4] = {
{13270, -6295, -2041},
{-3969, 7682, 170},
@@ -707,10 +729,7 @@ 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;
@@ -724,26 +743,16 @@ static int fill_xyztables(SwsInternal *c)
c->rgbgammainv = c->xyzgamma + 4096;
c->rgbgamma = c->rgbgammainv + 4096;
c->xyzgammainv = c->rgbgamma + 65536;
+ init_xyz_tables(c->xyzgamma, c->xyzgammainv, c->rgbgamma, c->rgbgammainv);
#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++) {
- 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++) {
- c->rgbgamma[i] = lrint(pow(i / 65535.0, rgbgamma) * 4095.0);
- c->xyzgammainv[i] = lrint(pow(i / 65535.0, xyzgammainv) * 4095.0);
- }
+ static AVOnce xyz_init_static_once = AV_ONCE_INIT;
+ ff_thread_once(&xyz_init_static_once, init_xyz_tables);
+#endif
return 0;
}
More information about the ffmpeg-cvslog
mailing list