aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2020-12-03 02:57:18 +0100
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2021-01-09 04:06:31 +0100
commit505d4de064aff879674d0670cf1c96ca46545da3 (patch)
treeff628be60c33104cdba788378f172f209c9d86ed
parent69aeba8a19ac2fa6e1c9bdfb19229b513f314bb1 (diff)
downloadffmpeg-505d4de064aff879674d0670cf1c96ca46545da3.tar.gz
avcodec/[e]ac3enc: Make encoders init-threadsafe, fix race
ff_eac3_exponent_init() set values twice when initializing a static table; ergo the initialization code must not run concurrently with a running EAC-3 encoder. Yet this code is executed every time an EAC-3 encoder is initialized. So use ff_thread_once() for this and also for a similar initialization performed for all AC-3 encoders to make them all init-threadsafe. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
-rw-r--r--libavcodec/ac3enc.c17
-rw-r--r--libavcodec/ac3enc_fixed.c2
-rw-r--r--libavcodec/ac3enc_float.c2
-rw-r--r--libavcodec/eac3enc.c2
4 files changed, 12 insertions, 11 deletions
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 0afe94a909..1a8a530e52 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -36,6 +36,7 @@
#include "libavutil/internal.h"
#include "libavutil/mem_internal.h"
#include "libavutil/opt.h"
+#include "libavutil/thread.h"
#include "avcodec.h"
#include "internal.h"
#include "me_cmp.h"
@@ -304,7 +305,7 @@ void ff_ac3_apply_rematrixing(AC3EncodeContext *s)
/*
* Initialize exponent tables.
*/
-static av_cold void exponent_init(AC3EncodeContext *s)
+static av_cold void exponent_init(void)
{
int expstr, i, grpsize;
@@ -317,9 +318,6 @@ static av_cold void exponent_init(AC3EncodeContext *s)
}
/* LFE */
exponent_group_tab[0][0][7] = 2;
-
- if (CONFIG_EAC3_ENCODER && s->eac3)
- ff_eac3_exponent_init();
}
@@ -2409,6 +2407,7 @@ static av_cold int allocate_buffers(AC3EncodeContext *s)
av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
{
+ static AVOnce init_static_once = AV_ONCE_INIT;
AC3EncodeContext *s = avctx->priv_data;
int ret, frame_size_58;
@@ -2448,15 +2447,15 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
s->mdct_init = ff_ac3_float_mdct_init;
s->allocate_sample_buffers = ff_ac3_float_allocate_sample_buffers;
}
- if (CONFIG_EAC3_ENCODER && s->eac3)
+ if (CONFIG_EAC3_ENCODER && s->eac3) {
+ static AVOnce init_static_once = AV_ONCE_INIT;
+ ff_thread_once(&init_static_once, ff_eac3_exponent_init);
s->output_frame_header = ff_eac3_output_frame_header;
- else
+ } else
s->output_frame_header = ac3_output_frame_header;
set_bandwidth(s);
- exponent_init(s);
-
bit_alloc_init(s);
ret = s->mdct_init(s);
@@ -2473,5 +2472,7 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx)
dprint_options(s);
+ ff_thread_once(&init_static_once, exponent_init);
+
return 0;
}
diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c
index 428bbfb3c5..d2e67f3214 100644
--- a/libavcodec/ac3enc_fixed.c
+++ b/libavcodec/ac3enc_fixed.c
@@ -155,7 +155,7 @@ AVCodec ff_ac3_fixed_encoder = {
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
AV_SAMPLE_FMT_NONE },
.priv_class = &ac3enc_class,
- .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
.supported_samplerates = ff_ac3_sample_rate_tab,
.channel_layouts = ff_ac3_channel_layouts,
.defaults = ac3_defaults,
diff --git a/libavcodec/ac3enc_float.c b/libavcodec/ac3enc_float.c
index 99863a9722..571f603182 100644
--- a/libavcodec/ac3enc_float.c
+++ b/libavcodec/ac3enc_float.c
@@ -153,5 +153,5 @@ AVCodec ff_ac3_encoder = {
.supported_samplerates = ff_ac3_sample_rate_tab,
.channel_layouts = ff_ac3_channel_layouts,
.defaults = ac3_defaults,
- .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
};
diff --git a/libavcodec/eac3enc.c b/libavcodec/eac3enc.c
index 8e1032f268..00721aa645 100644
--- a/libavcodec/eac3enc.c
+++ b/libavcodec/eac3enc.c
@@ -266,5 +266,5 @@ AVCodec ff_eac3_encoder = {
.supported_samplerates = ff_ac3_sample_rate_tab,
.channel_layouts = ff_ac3_channel_layouts,
.defaults = ac3_defaults,
- .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
+ .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
};