diff mbox series

[FFmpeg-devel,4/6] avfilter/af_channelmap: factorize checking indexes against a channel layout

Message ID 20240319211504.8342-4-cus@passwd.hu
State Accepted
Commit bba6dd391f4e9fd8cbe91a202b8f5be04bf9f133
Headers show
Series [FFmpeg-devel,1/6] avfilter/af_channelmap: fix error message if FL source channel was missing | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Marton Balint March 19, 2024, 9:15 p.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavfilter/af_channelmap.c | 41 ++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/af_channelmap.c b/libavfilter/af_channelmap.c
index 1ecbddd462..62e11103ad 100644
--- a/libavfilter/af_channelmap.c
+++ b/libavfilter/af_channelmap.c
@@ -116,6 +116,30 @@  static int get_channel(char **map, int *ch, char delim)
     return 0;
 }
 
+static int check_idx_and_id(AVFilterContext *ctx, int channel_idx, int channel, AVChannelLayout *ch_layout, const char *io)
+{
+    char channel_name[64];
+    char layout_name[256];
+    int nb_channels = ch_layout->nb_channels;
+
+    if (channel_idx < 0 || channel_idx >= nb_channels) {
+        av_channel_layout_describe(ch_layout, layout_name, sizeof(layout_name));
+        if (channel >= 0) {
+            av_channel_name(channel_name, sizeof(channel_name), channel);
+            av_log(ctx, AV_LOG_ERROR,
+                   "%sput channel '%s' not available from %sput layout '%s'\n",
+                   io, channel_name, io, layout_name);
+        } else {
+            av_log(ctx, AV_LOG_ERROR,
+                   "%sput channel #%d not available from %sput layout '%s'\n",
+                   io, channel_idx, io, layout_name);
+        }
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
 static av_cold int channelmap_init(AVFilterContext *ctx)
 {
     ChannelMapContext *s = ctx->priv;
@@ -334,10 +358,7 @@  static int channelmap_config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
     ChannelMapContext *s = ctx->priv;
-    int nb_channels = inlink->ch_layout.nb_channels;
     int i, err = 0;
-    char channel_name[64];
-    char layout_name[256];
 
     for (i = 0; i < s->nch; i++) {
         struct ChannelMap *m = &s->map[i];
@@ -347,20 +368,8 @@  static int channelmap_config_input(AVFilterLink *inlink)
                 &inlink->ch_layout, m->in_channel);
         }
 
-        if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
-            av_channel_layout_describe(&inlink->ch_layout, layout_name, sizeof(layout_name));
-            if (m->in_channel >= 0) {
-                av_channel_name(channel_name, sizeof(channel_name), m->in_channel);
-                av_log(ctx, AV_LOG_ERROR,
-                       "input channel '%s' not available from input layout '%s'\n",
-                       channel_name, layout_name);
-            } else {
-                av_log(ctx, AV_LOG_ERROR,
-                       "input channel #%d not available from input layout '%s'\n",
-                       m->in_channel_idx, layout_name);
-            }
+        if (check_idx_and_id(ctx, m->in_channel_idx, m->in_channel, &inlink->ch_layout, "in") < 0)
             err = AVERROR(EINVAL);
-        }
     }
 
     return err;