[FFmpeg-devel] [PATCH 01/22] lavc/encode: print separate messages for unknown and unsupported formats

Anton Khirnov anton at khirnov.net
Fri Jul 7 12:48:26 EEST 2023


The former is typically a bug in the calling program, while the latter
will more often be a user configuration issue.
---
 libavcodec/encode.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index ab5f889615..2da9f6ba23 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -24,6 +24,7 @@
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/pixdesc.h"
 #include "libavutil/samplefmt.h"
 
 #include "avcodec.h"
@@ -552,15 +553,19 @@ static int encode_preinit_video(AVCodecContext *avctx)
     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->pix_fmt);
     int i;
 
+    if (!av_get_pix_fmt_name(avctx->pix_fmt)) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid video pixel format: %d\n",
+               avctx->pix_fmt);
+        return AVERROR(EINVAL);
+    }
+
     if (avctx->codec->pix_fmts) {
         for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
             if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
                 break;
         if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
-            char buf[128];
-            snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
-            av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
-                   (char *)av_x_if_null(av_get_pix_fmt_name(avctx->pix_fmt), buf));
+            av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is not supported\n",
+                   av_get_pix_fmt_name(avctx->pix_fmt));
             return AVERROR(EINVAL);
         }
         if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_YUVJ420P ||
@@ -622,6 +627,12 @@ static int encode_preinit_audio(AVCodecContext *avctx)
 {
     int i;
 
+    if (!av_get_sample_fmt_name(avctx->sample_fmt)) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid audio sample format: %d\n",
+               avctx->sample_fmt);
+        return AVERROR(EINVAL);
+    }
+
     if (avctx->codec->sample_fmts) {
         for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++) {
             if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
@@ -634,10 +645,8 @@ static int encode_preinit_audio(AVCodecContext *avctx)
             }
         }
         if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
-            char buf[128];
-            snprintf(buf, sizeof(buf), "%d", avctx->sample_fmt);
-            av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is invalid or not supported\n",
-                   (char *)av_x_if_null(av_get_sample_fmt_name(avctx->sample_fmt), buf));
+            av_log(avctx, AV_LOG_ERROR, "Specified sample format %s is not supported\n",
+                   av_get_sample_fmt_name(avctx->sample_fmt));
             return AVERROR(EINVAL);
         }
     }
-- 
2.40.1



More information about the ffmpeg-devel mailing list