Skip to content

Commit 54c865f

Browse files
committed
swscale/utils: Fix potential race when initializing xyz tables
Signed-off-by: Andreas Rheinhardt <[email protected]>
1 parent 1c78db8 commit 54c865f

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

libswscale/utils.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,35 @@ static void fill_rgb2yuv_table(SwsInternal *c, const int table[4], int dstRange)
692692
AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
693693
}
694694

695-
static int fill_xyztables(SwsInternal *c)
695+
#if CONFIG_SMALL
696+
static void init_xyz_tables(uint16_t xyzgamma_tab[4096], uint16_t xyzgammainv_tab[65536],
697+
uint16_t rgbgamma_tab[65536], uint16_t rgbgammainv_tab[4096])
698+
#else
699+
static uint16_t xyzgamma_tab[4096], rgbgammainv_tab[4096];
700+
static uint16_t rgbgamma_tab[65536], xyzgammainv_tab[65536];
701+
static av_cold void init_xyz_tables(void)
702+
#endif
696703
{
697-
int i;
698-
double xyzgamma = XYZ_GAMMA;
699-
double rgbgamma = 1.0 / RGB_GAMMA;
704+
double xyzgamma = XYZ_GAMMA;
705+
double rgbgamma = 1.0 / RGB_GAMMA;
700706
double xyzgammainv = 1.0 / XYZ_GAMMA;
701707
double rgbgammainv = RGB_GAMMA;
708+
709+
/* set input gamma vectors */
710+
for (int i = 0; i < 4096; i++) {
711+
xyzgamma_tab[i] = lrint(pow(i / 4095.0, xyzgamma) * 65535.0);
712+
rgbgammainv_tab[i] = lrint(pow(i / 4095.0, rgbgammainv) * 65535.0);
713+
}
714+
715+
/* set output gamma vectors */
716+
for (int i = 0; i < 65536; i++) {
717+
rgbgamma_tab[i] = lrint(pow(i / 65535.0, rgbgamma) * 4095.0);
718+
xyzgammainv_tab[i] = lrint(pow(i / 65535.0, xyzgammainv) * 4095.0);
719+
}
720+
}
721+
722+
static int fill_xyztables(SwsInternal *c)
723+
{
702724
static const int16_t xyz2rgb_matrix[3][4] = {
703725
{13270, -6295, -2041},
704726
{-3969, 7682, 170},
@@ -707,10 +729,7 @@ static int fill_xyztables(SwsInternal *c)
707729
{1689, 1464, 739},
708730
{ 871, 2929, 296},
709731
{ 79, 488, 3891} };
710-
#if !CONFIG_SMALL
711-
static uint16_t xyzgamma_tab[4096], rgbgammainv_tab[4096];
712-
static uint16_t rgbgamma_tab[65536], xyzgammainv_tab[65536];
713-
#endif
732+
714733
if (c->xyzgamma)
715734
return 0;
716735

@@ -724,26 +743,16 @@ static int fill_xyztables(SwsInternal *c)
724743
c->rgbgammainv = c->xyzgamma + 4096;
725744
c->rgbgamma = c->rgbgammainv + 4096;
726745
c->xyzgammainv = c->rgbgamma + 65536;
746+
init_xyz_tables(c->xyzgamma, c->xyzgammainv, c->rgbgamma, c->rgbgammainv);
727747
#else
728748
c->xyzgamma = xyzgamma_tab;
729749
c->rgbgamma = rgbgamma_tab;
730750
c->xyzgammainv = xyzgammainv_tab;
731751
c->rgbgammainv = rgbgammainv_tab;
732-
if (xyzgamma_tab[4095])
733-
return 0;
734-
#endif
735752

736-
/* set input gamma vectors */
737-
for (i = 0; i < 4096; i++) {
738-
c->xyzgamma[i] = lrint(pow(i / 4095.0, xyzgamma) * 65535.0);
739-
c->rgbgammainv[i] = lrint(pow(i / 4095.0, rgbgammainv) * 65535.0);
740-
}
741-
742-
/* set output gamma vectors */
743-
for (i = 0; i < 65536; i++) {
744-
c->rgbgamma[i] = lrint(pow(i / 65535.0, rgbgamma) * 4095.0);
745-
c->xyzgammainv[i] = lrint(pow(i / 65535.0, xyzgammainv) * 4095.0);
746-
}
753+
static AVOnce xyz_init_static_once = AV_ONCE_INIT;
754+
ff_thread_once(&xyz_init_static_once, init_xyz_tables);
755+
#endif
747756
return 0;
748757
}
749758

0 commit comments

Comments
 (0)