diff mbox series

[FFmpeg-devel,172/217] avcodec/vima: Make decoder init-threadsafe

Message ID 20201202042244.519127-38-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,01/45] avcodec/a64multienc: Fix memleak upon init failure
Related show

Checks

Context Check Description
andriy/x86 warning Failed to apply patch

Commit Message

Andreas Rheinhardt Dec. 2, 2020, 4:21 a.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/vima.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/vima.c b/libavcodec/vima.c
index b4620acf6b..b0a2448ede 100644
--- a/libavcodec/vima.c
+++ b/libavcodec/vima.c
@@ -26,13 +26,13 @@ 
  */
 
 #include "libavutil/channel_layout.h"
+#include "libavutil/thread.h"
 
 #include "adpcm_data.h"
 #include "avcodec.h"
 #include "get_bits.h"
 #include "internal.h"
 
-static int predict_table_init = 0;
 static uint16_t predict_table[5786 * 2];
 
 static const uint8_t size_table[] = {
@@ -84,16 +84,9 @@  static const int8_t *const step_index_tables[] = {
     index_table4, index_table5, index_table6
 };
 
-static av_cold int decode_init(AVCodecContext *avctx)
+static av_cold void predict_table_init(void)
 {
-    int start_pos;
-
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
-
-    if (predict_table_init)
-        return 0;
-
-    for (start_pos = 0; start_pos < 64; start_pos++) {
+    for (int start_pos = 0; start_pos < 64; start_pos++) {
         unsigned int dest_pos, table_pos;
 
         for (table_pos = 0, dest_pos = start_pos;
@@ -110,7 +103,15 @@  static av_cold int decode_init(AVCodecContext *avctx)
             predict_table[dest_pos] = put;
         }
     }
-    predict_table_init = 1;
+}
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    static AVOnce init_static_once = AV_ONCE_INIT;
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+
+    ff_thread_once(&init_static_once, predict_table_init);
 
     return 0;
 }
@@ -215,4 +216,5 @@  AVCodec ff_adpcm_vima_decoder = {
     .init         = decode_init,
     .decode       = decode_frame,
     .capabilities = AV_CODEC_CAP_DR1,
+    .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
 };