[FFmpeg-devel] [PATCH 43/45] avcodec/faxcompr: Make ff_ccitt_unpack_init() thread-safe

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Fri Nov 27 03:02:47 EET 2020


This will allow to make the TIFF decoder's init function thread-safe.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/faxcompr.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/libavcodec/faxcompr.c b/libavcodec/faxcompr.c
index 2a1d2bc3f6..3dd64cf730 100644
--- a/libavcodec/faxcompr.c
+++ b/libavcodec/faxcompr.c
@@ -24,6 +24,7 @@
  * CCITT Fax Group 3 and 4 decompression
  * @author Konstantin Shishkov
  */
+#include "libavutil/thread.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "put_bits.h"
@@ -96,15 +97,12 @@ static const uint8_t ccitt_group3_2d_lens[11] = {
 
 static VLC ccitt_vlc[2], ccitt_group3_2d_vlc;
 
-av_cold void ff_ccitt_unpack_init(void)
+static av_cold void ccitt_unpack_init(void)
 {
     static VLC_TYPE code_table1[528][2];
     static VLC_TYPE code_table2[648][2];
     int i;
-    static int initialized = 0;
 
-    if (initialized)
-        return;
     ccitt_vlc[0].table = code_table1;
     ccitt_vlc[0].table_allocated = 528;
     ccitt_vlc[1].table = code_table2;
@@ -119,7 +117,12 @@ av_cold void ff_ccitt_unpack_init(void)
     INIT_VLC_STATIC(&ccitt_group3_2d_vlc, 9, 11,
                     ccitt_group3_2d_lens, 1, 1,
                     ccitt_group3_2d_bits, 1, 1, 512);
-    initialized = 1;
+}
+
+av_cold void ff_ccitt_unpack_init(void)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+    ff_thread_once(&init_static_once, ccitt_unpack_init);
 }
 
 static int decode_uncompressed(AVCodecContext *avctx, GetBitContext *gb,
-- 
2.25.1



More information about the ffmpeg-devel mailing list