[FFmpeg-cvslog] lavc/encode: print separate messages for unknown and unsupported formats

Anton Khirnov git at videolan.org
Tue Jul 11 20:28:49 EEST 2023


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Thu Jul  6 17:58:19 2023 +0200| [160fefacd3feb6107a42f030fa16b00fc95fae13] | committer: Anton Khirnov

lavc/encode: print separate messages for unknown and unsupported formats

The former is typically a bug in the calling program, while the latter
will more often be a user configuration issue.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=160fefacd3feb6107a42f030fa16b00fc95fae13
---

 libavcodec/encode.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index 58eab5b001..15f6eb11d0 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"
@@ -576,15 +577,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 ||
@@ -646,6 +651,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])
@@ -658,10 +669,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);
         }
     }



More information about the ffmpeg-cvslog mailing list