[FFmpeg-devel] [PATCH] lavc/utils: create dedicated function for checking encoder constraints

Stefano Sabatini stefasab at gmail.com
Sun Oct 21 21:43:46 CEST 2012


Allow to reduce the bulk size of avcodec_open2(), improve readability.
---
 libavcodec/utils.c |  147 +++++++++++++++++++++++++++++----------------------
 1 files changed, 84 insertions(+), 63 deletions(-)

diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 2fd7b01..2e84678 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -821,6 +821,85 @@ int attribute_align_arg ff_codec_open2_recursive(AVCodecContext *avctx, const AV
     return ret;
 }
 
+static int check_encoder_constraints(AVCodecContext *avctx)
+{
+    int i, is_first;
+
+    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])
+                break;
+
+        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));
+
+            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
+            && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
+                 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
+            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));
+
+            return AVERROR(EINVAL);
+        }
+    }
+
+    if (avctx->codec->supported_samplerates) {
+        for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
+            if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
+                break;
+        if (avctx->codec->supported_samplerates[i] == 0) {
+            av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
+                   avctx->sample_rate);
+
+            return AVERROR(EINVAL);
+        }
+    }
+
+    if (avctx->codec->channel_layouts) {
+        if (!avctx->channel_layout) {
+            av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
+        } else {
+            for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
+                if (avctx->channel_layout == avctx->codec->channel_layouts[i])
+                    break;
+            if (avctx->codec->channel_layouts[i] == 0) {
+                char buf[512];
+                av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
+                av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
+
+                return AVERROR(EINVAL);
+            }
+        }
+    }
+
+    if (avctx->channel_layout && avctx->channels) {
+        int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
+        if (channels != avctx->channels) {
+            char buf[512];
+            av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
+            av_log(avctx, AV_LOG_ERROR,
+                   "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
+                   buf, channels, avctx->channels);
+            return AVERROR(EINVAL);
+        }
+    }
+
+    return 0;
+}
+
 int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
 {
     int ret = 0;
@@ -984,71 +1063,13 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
                     break;
                 }
             }
-            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));
-                ret = AVERROR(EINVAL);
-                goto free_and_end;
-            }
-        }
-        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
-                && !((avctx->codec_id == AV_CODEC_ID_MJPEG || avctx->codec_id == AV_CODEC_ID_LJPEG)
-                     && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
-                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));
-                ret = AVERROR(EINVAL);
-                goto free_and_end;
-            }
         }
-        if (avctx->codec->supported_samplerates) {
-            for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
-                if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
-                    break;
-            if (avctx->codec->supported_samplerates[i] == 0) {
-                av_log(avctx, AV_LOG_ERROR, "Specified sample rate %d is not supported\n",
-                       avctx->sample_rate);
-                ret = AVERROR(EINVAL);
-                goto free_and_end;
-            }
-        }
-        if (avctx->codec->channel_layouts) {
-            if (!avctx->channel_layout) {
-                av_log(avctx, AV_LOG_WARNING, "Channel layout not specified\n");
-            } else {
-                for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
-                    if (avctx->channel_layout == avctx->codec->channel_layouts[i])
-                        break;
-                if (avctx->codec->channel_layouts[i] == 0) {
-                    char buf[512];
-                    av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
-                    av_log(avctx, AV_LOG_ERROR, "Specified channel layout '%s' is not supported\n", buf);
-                    ret = AVERROR(EINVAL);
-                    goto free_and_end;
-                }
-            }
-        }
-        if (avctx->channel_layout && avctx->channels) {
-            int channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
-            if (channels != avctx->channels) {
-                char buf[512];
-                av_get_channel_layout_string(buf, sizeof(buf), -1, avctx->channel_layout);
-                av_log(avctx, AV_LOG_ERROR,
-                       "Channel layout '%s' with %d channels does not match number of specified channels %d\n",
-                       buf, channels, avctx->channels);
-                ret = AVERROR(EINVAL);
-                goto free_and_end;
-            }
-        } else if (avctx->channel_layout) {
+
+        if ((ret = check_encoder_constraints(avctx)) < 0)
+            goto free_and_end;
+
+        if (avctx->channel_layout && !avctx->channels)
             avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
-        }
     }
 
     avctx->pts_correction_num_faulty_pts =
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list