From a9d1d11e4dc324be1ef8f10985fb414f04f504fd Mon Sep 17 00:00:00 2001
From: Dale Curtis <dalecurtis@chromium.org>
Date: Tue, 30 Jul 2024 23:12:21 +0000
Subject: [PATCH] Check codec_whitelist before reinitializing AVCtx.priv_data.

This ensures that if a codec isn't on codec_whitelist, its VUI
information can still be populated during find_stream_info()
via parsers.

Signed-off-by: Dale Curtis <dalecurtis@chromium.org>
---
 libavcodec/avcodec.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 214dca4566..f189263ff9 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -174,6 +174,14 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
         return AVERROR(EINVAL);
 
+    if ((ret = av_opt_set_dict(avctx, options)) < 0)
+        return AVERROR(EINVAL);
+
+    if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
+        av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
+        return AVERROR(EINVAL);
+    }
+
     avci = av_codec_is_decoder(codec) ?
         ff_decode_internal_alloc()    :
         ff_encode_internal_alloc();
@@ -207,14 +215,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
     } else {
         avctx->priv_data = NULL;
     }
-    if ((ret = av_opt_set_dict(avctx, options)) < 0)
-        goto free_and_end;
-
-    if (avctx->codec_whitelist && av_match_list(codec->name, avctx->codec_whitelist, ',') <= 0) {
-        av_log(avctx, AV_LOG_ERROR, "Codec (%s) not on whitelist \'%s\'\n", codec->name, avctx->codec_whitelist);
-        ret = AVERROR(EINVAL);
-        goto free_and_end;
-    }
 
     // only call ff_set_dimensions() for non H.264/VP6F/DXV codecs so as not to overwrite previously setup dimensions
     if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
-- 
2.46.0.rc1.232.g9752f9e123-goog

