diff mbox series

[FFmpeg-devel,2/6] avcodec/utils: Perform sub_charenc/iconv checks before AVCodec.init()

Message ID 20210316203100.528950-2-andreas.rheinhardt@gmail.com
State Accepted
Headers show
Series [FFmpeg-devel,1/6] avcodec/utils: Check earlier for codec id/type mismatch
Related show

Checks

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

Commit Message

Andreas Rheinhardt March 16, 2021, 8:30 p.m. UTC
Also move them to ff_decode_preinit().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/decode.c | 35 +++++++++++++++++++++++++++++++++++
 libavcodec/utils.c  | 40 ----------------------------------------
 2 files changed, 35 insertions(+), 40 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 5a00aeedae..cbd41c8cc8 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -2031,6 +2031,41 @@  FF_ENABLE_DEPRECATION_WARNINGS
                avctx->codec->max_lowres);
         avctx->lowres = avctx->codec->max_lowres;
     }
+    if (avctx->sub_charenc) {
+        if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
+            av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
+                   "supported with subtitles codecs\n");
+            return AVERROR(EINVAL);
+        } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
+            av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
+                   "subtitles character encoding will be ignored\n",
+                   avctx->codec_descriptor->name);
+            avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
+        } else {
+            /* input character encoding is set for a text based subtitle
+             * codec at this point */
+            if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
+                avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
+
+            if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
+#if CONFIG_ICONV
+                iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
+                if (cd == (iconv_t)-1) {
+                    ret = AVERROR(errno);
+                    av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
+                           "with input character encoding \"%s\"\n", avctx->sub_charenc);
+                    return ret;
+                }
+                iconv_close(cd);
+#else
+                av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
+                       "conversion needs a libavcodec built with iconv support "
+                       "for this codec\n");
+                return AVERROR(ENOSYS);
+#endif
+            }
+        }
+    }
 
     avctx->pts_correction_num_faulty_pts =
     avctx->pts_correction_num_faulty_dts = 0;
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 2bc556c1d9..3629813387 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -60,9 +60,6 @@ 
 #include <stdatomic.h>
 #include <limits.h>
 #include <float.h>
-#if CONFIG_ICONV
-# include <iconv.h>
-#endif
 
 #include "libavutil/ffversion.h"
 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
@@ -786,43 +783,6 @@  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
             ret = AVERROR(EINVAL);
             goto free_and_end;
         }
-        if (avctx->sub_charenc) {
-            if (avctx->codec_type != AVMEDIA_TYPE_SUBTITLE) {
-                av_log(avctx, AV_LOG_ERROR, "Character encoding is only "
-                       "supported with subtitles codecs\n");
-                ret = AVERROR(EINVAL);
-                goto free_and_end;
-            } else if (avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB) {
-                av_log(avctx, AV_LOG_WARNING, "Codec '%s' is bitmap-based, "
-                       "subtitles character encoding will be ignored\n",
-                       avctx->codec_descriptor->name);
-                avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_DO_NOTHING;
-            } else {
-                /* input character encoding is set for a text based subtitle
-                 * codec at this point */
-                if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
-                    avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
-
-                if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
-#if CONFIG_ICONV
-                    iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
-                    if (cd == (iconv_t)-1) {
-                        ret = AVERROR(errno);
-                        av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
-                               "with input character encoding \"%s\"\n", avctx->sub_charenc);
-                        goto free_and_end;
-                    }
-                    iconv_close(cd);
-#else
-                    av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
-                           "conversion needs a libavcodec built with iconv support "
-                           "for this codec\n");
-                    ret = AVERROR(ENOSYS);
-                    goto free_and_end;
-#endif
-                }
-            }
-        }
 
 #if FF_API_AVCTX_TIMEBASE
         if (avctx->framerate.num > 0 && avctx->framerate.den > 0)