]> git.sesse.net Git - ffmpeg/commitdiff
avcodec/avcodec: Sanitize options before using them
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Tue, 16 Mar 2021 19:01:52 +0000 (20:01 +0100)
committerAndreas Rheinhardt <andreas.rheinhardt@gmail.com>
Sat, 20 Mar 2021 00:55:41 +0000 (01:55 +0100)
This is how it is supposed to happen, yet when using frame threading,
the codec's init function has been called before preinit. This can lead
to crashes when e.g. using unsupported lowres values for decoders
together with frame threading.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
libavcodec/avcodec.c

index 13fe3d4ebfe026def264cda58b6ec2e2389fa2c6..b3940b97e0bb3bc139888b45e88d378273314cf2 100644 (file)
@@ -315,6 +315,13 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
         avctx->time_base.den = avctx->sample_rate;
     }
 
+    if (av_codec_is_encoder(avctx->codec))
+        ret = ff_encode_preinit(avctx);
+    else
+        ret = ff_decode_preinit(avctx);
+    if (ret < 0)
+        goto free_and_end;
+
     if (!HAVE_THREADS)
         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
 
@@ -336,13 +343,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (!HAVE_THREADS && !(codec->caps_internal & FF_CODEC_CAP_AUTO_THREADS))
         avctx->thread_count = 1;
 
-    if (av_codec_is_encoder(avctx->codec))
-        ret = ff_encode_preinit(avctx);
-    else
-        ret = ff_decode_preinit(avctx);
-    if (ret < 0)
-        goto free_and_end;
-
     if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
         || avci->frame_thread_encoder)) {
         ret = avctx->codec->init(avctx);