diff mbox series

[FFmpeg-devel,7/7] lavc: do not hold the codec init lock longer than necessary

Message ID 20210310120332.27225-7-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,1/7] lavc: factor out encoder init/validation from avcodec_open2() | expand

Checks

Context Check Description
andriy/configure warning Failed to apply patch

Commit Message

Anton Khirnov March 10, 2021, 12:03 p.m. UTC
The only potentially unsafe operation is the codec-specific init
function.
---
 libavcodec/utils.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Comments

Andreas Rheinhardt March 10, 2021, 12:11 p.m. UTC | #1
Anton Khirnov:
> The only potentially unsafe operation is the codec-specific init
> function.
> ---
>  libavcodec/utils.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/libavcodec/utils.c b/libavcodec/utils.c
> index e0f6234bd7..952adb5277 100644
> --- a/libavcodec/utils.c
> +++ b/libavcodec/utils.c
> @@ -570,8 +570,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>      if (options)
>          av_dict_copy(&tmp, *options, 0);
>  
> -    lock_avcodec(codec);
> -
>      avci = av_mallocz(sizeof(*avci));
>      if (!avci) {
>          ret = AVERROR(ENOMEM);
> @@ -723,9 +721,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>          av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
>  
>      if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
> -        unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
>          ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
> -        lock_avcodec(codec);
>          if (ret < 0)
>              goto free_and_end;
>      }
> @@ -749,7 +745,9 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>  
>      if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
>          || avci->frame_thread_encoder)) {
> +        lock_avcodec(codec);
>          ret = avctx->codec->init(avctx);
> +        unlock_avcodec(codec);
>          if (ret < 0) {
>              codec_init_ok = -1;
>              goto free_and_end;
> @@ -834,7 +832,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
>      }
>  
>  end:
> -    unlock_avcodec(codec);
>      if (options) {
>          av_dict_free(options);
>          *options = tmp;
> 
ff_frame_thread_init also calls codec->init directly.

- Andreas
diff mbox series

Patch

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index e0f6234bd7..952adb5277 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -570,8 +570,6 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (options)
         av_dict_copy(&tmp, *options, 0);
 
-    lock_avcodec(codec);
-
     avci = av_mallocz(sizeof(*avci));
     if (!avci) {
         ret = AVERROR(ENOMEM);
@@ -723,9 +721,7 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
 
     if (CONFIG_FRAME_THREAD_ENCODER && av_codec_is_encoder(avctx->codec)) {
-        unlock_avcodec(codec); //we will instantiate a few encoders thus kick the counter to prevent false detection of a problem
         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
-        lock_avcodec(codec);
         if (ret < 0)
             goto free_and_end;
     }
@@ -749,7 +745,9 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
 
     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
         || avci->frame_thread_encoder)) {
+        lock_avcodec(codec);
         ret = avctx->codec->init(avctx);
+        unlock_avcodec(codec);
         if (ret < 0) {
             codec_init_ok = -1;
             goto free_and_end;
@@ -834,7 +832,6 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     }
 
 end:
-    unlock_avcodec(codec);
     if (options) {
         av_dict_free(options);
         *options = tmp;