diff mbox series

[FFmpeg-devel,23/39] avcodec/h261enc: Make encoder init-threadsafe

Message ID 20201210111657.2276739-24-andreas.rheinhardt@gmail.com
State Accepted
Commit 18e89b964cd226785ca647e521ab09e16026fd26
Headers show
Series Make mpegvideo encoders init-threadsafe
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 10, 2020, 11:16 a.m. UTC
This is easy now that the H.261 encoder is the only user that
initializes the non-VLC parts of ff_h261_rl_tcoeff.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/h261.c    | 13 -------------
 libavcodec/h261.h    |  3 ---
 libavcodec/h261enc.c | 16 ++++++++++++----
 3 files changed, 12 insertions(+), 20 deletions(-)

Comments

Anton Khirnov Jan. 18, 2021, 10:51 a.m. UTC | #1
Quoting Andreas Rheinhardt (2020-12-10 12:16:41)
> This is easy now that the H.261 encoder is the only user that
> initializes the non-VLC parts of ff_h261_rl_tcoeff.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---

Looks good modulo my previous question about shared table
diff mbox series

Patch

diff --git a/libavcodec/h261.c b/libavcodec/h261.c
index 47bad4e57f..01abfadcda 100644
--- a/libavcodec/h261.c
+++ b/libavcodec/h261.c
@@ -30,8 +30,6 @@ 
 
 #define IS_FIL(a)    ((a) & MB_TYPE_H261_FIL)
 
-uint8_t ff_h261_rl_table_store[2][2 * MAX_RUN + MAX_LEVEL + 3];
-
 static void h261_loop_filter(uint8_t *src, int stride)
 {
     int x, y, xy, yz;
@@ -79,14 +77,3 @@  void ff_h261_loop_filter(MpegEncContext *s)
     h261_loop_filter(dest_cb, uvlinesize);
     h261_loop_filter(dest_cr, uvlinesize);
 }
-
-av_cold void ff_h261_common_init(void)
-{
-    static int done = 0;
-
-    if (done)
-        return;
-
-    ff_rl_init(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
-    done = 1;
-}
diff --git a/libavcodec/h261.h b/libavcodec/h261.h
index 399a404b2b..25728a295d 100644
--- a/libavcodec/h261.h
+++ b/libavcodec/h261.h
@@ -48,8 +48,6 @@  typedef struct H261Context {
 
 #define MB_TYPE_H261_FIL 0x800000
 
-extern uint8_t ff_h261_rl_table_store[2][2 * MAX_RUN + MAX_LEVEL + 3];
-
 extern const uint8_t ff_h261_mba_code[35];
 extern const uint8_t ff_h261_mba_bits[35];
 extern const uint8_t ff_h261_mtype_code[10];
@@ -60,7 +58,6 @@  extern const uint8_t ff_h261_cbp_tab[63][2];
 extern RLTable ff_h261_rl_tcoeff;
 
 void ff_h261_loop_filter(MpegEncContext *s);
-void ff_h261_common_init(void);
 
 int ff_h261_get_picture_format(int width, int height);
 void ff_h261_reorder_mb_index(MpegEncContext *s);
diff --git a/libavcodec/h261enc.c b/libavcodec/h261enc.c
index 8d5d62b786..eec526c500 100644
--- a/libavcodec/h261enc.c
+++ b/libavcodec/h261enc.c
@@ -27,6 +27,7 @@ 
 
 #include "libavutil/attributes.h"
 #include "libavutil/avassert.h"
+#include "libavutil/thread.h"
 #include "avcodec.h"
 #include "mpegutils.h"
 #include "mpegvideo.h"
@@ -361,9 +362,17 @@  static av_cold void init_uni_h261_rl_tab(const RLTable *rl, uint8_t *len_tab)
     }
 }
 
+static av_cold void h261_encode_init_static(void)
+{
+    static uint8_t h261_rl_table_store[2][2 * MAX_RUN + MAX_LEVEL + 3];
+
+    ff_rl_init(&ff_h261_rl_tcoeff, h261_rl_table_store);
+    init_uni_h261_rl_tab(&ff_h261_rl_tcoeff, uni_h261_rl_len);
+}
+
 av_cold void ff_h261_encode_init(MpegEncContext *s)
 {
-    ff_h261_common_init();
+    static AVOnce init_static_once = AV_ONCE_INIT;
 
     s->min_qcoeff       = -127;
     s->max_qcoeff       = 127;
@@ -371,10 +380,9 @@  av_cold void ff_h261_encode_init(MpegEncContext *s)
     s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
     s->ac_esc_length    = 6+6+8;
 
-    init_uni_h261_rl_tab(&ff_h261_rl_tcoeff, uni_h261_rl_len);
-
     s->intra_ac_vlc_length      = s->inter_ac_vlc_length      = uni_h261_rl_len;
     s->intra_ac_vlc_last_length = s->inter_ac_vlc_last_length = uni_h261_rl_len + 128*64;
+    ff_thread_once(&init_static_once, h261_encode_init_static);
 }
 
 static const AVClass h261_class = {
@@ -393,7 +401,7 @@  AVCodec ff_h261_encoder = {
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = ff_mpv_encode_end,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
                                                      AV_PIX_FMT_NONE },
     .priv_class     = &h261_class,