]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_channelmap.c
screenpresso: Add extended pixel format support
[ffmpeg] / libavfilter / af_channelmap.c
index c626a21fd636f5863b904bbf7a8e8a6d0c8b6ae0..572549808f94551fa1fdea79061bcf3f94bc7086 100644 (file)
@@ -57,7 +57,6 @@ enum MappingMode {
 #define MAX_CH 64
 typedef struct ChannelMapContext {
     const AVClass *class;
-    AVFilterChannelLayouts *channel_layouts;
     char *mapping_str;
     char *channel_layout_str;
     uint64_t output_layout;
@@ -122,7 +121,6 @@ static int get_channel(char **map, uint64_t *ch, char delim)
 static av_cold int channelmap_init(AVFilterContext *ctx)
 {
     ChannelMapContext *s = ctx->priv;
-    int ret = 0;
     char *mapping, separator = '|';
     int map_entries = 0;
     char buf[256];
@@ -172,8 +170,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
 
     if (map_entries > MAX_CH) {
         av_log(ctx, AV_LOG_ERROR, "Too many channels mapped: '%d'.\n", map_entries);
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
     for (i = 0; i < map_entries; i++) {
@@ -183,18 +180,16 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
         switch (mode) {
         case MAP_ONE_INT:
             if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
-                ret = AVERROR(EINVAL);
                 av_log(ctx, AV_LOG_ERROR, err);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel_idx  = in_ch_idx;
             s->map[i].out_channel_idx = i;
             break;
         case MAP_ONE_STR:
-            if (!get_channel(&mapping, &in_ch, separator)) {
+            if (get_channel(&mapping, &in_ch, separator) < 0) {
                 av_log(ctx, AV_LOG_ERROR, err);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel      = in_ch;
             s->map[i].out_channel_idx = i;
@@ -203,8 +198,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
             if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
                 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
                 av_log(ctx, AV_LOG_ERROR, err);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel_idx  = in_ch_idx;
             s->map[i].out_channel_idx = out_ch_idx;
@@ -214,8 +208,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
                 get_channel(&mapping, &out_ch, separator) < 0 ||
                 out_ch & out_ch_mask) {
                 av_log(ctx, AV_LOG_ERROR, err);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel_idx  = in_ch_idx;
             s->map[i].out_channel     = out_ch;
@@ -225,8 +218,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
             if (get_channel(&mapping, &in_ch, '-') < 0 ||
                 get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
                 av_log(ctx, AV_LOG_ERROR, err);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel      = in_ch;
             s->map[i].out_channel_idx = out_ch_idx;
@@ -236,8 +228,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
                 get_channel(&mapping, &out_ch, separator) < 0 ||
                 out_ch & out_ch_mask) {
                 av_log(ctx, AV_LOG_ERROR, err);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
             s->map[i].in_channel = in_ch;
             s->map[i].out_channel = out_ch;
@@ -255,8 +246,7 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
         if ((fmt = av_get_channel_layout(s->channel_layout_str)) == 0) {
             av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout: '%s'.\n",
                    s->channel_layout_str);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
         if (mode == MAP_NONE) {
             int i;
@@ -270,18 +260,20 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
             av_log(ctx, AV_LOG_ERROR,
                    "Output channel layout '%s' does not match the list of channel mapped: '%s'.\n",
                    s->channel_layout_str, buf);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         } else if (s->nch != av_get_channel_layout_nb_channels(fmt)) {
             av_log(ctx, AV_LOG_ERROR,
                    "Output channel layout %s does not match the number of channels mapped %d.\n",
                    s->channel_layout_str, s->nch);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
         s->output_layout = fmt;
     }
-    ff_add_channel_layout(&s->channel_layouts, s->output_layout);
+    if (!s->output_layout) {
+        av_log(ctx, AV_LOG_ERROR, "Output channel layout is not set and "
+               "cannot be guessed from the maps.\n");
+        return AVERROR(EINVAL);
+    }
 
     if (mode == MAP_PAIR_INT_STR || mode == MAP_PAIR_STR_STR) {
         for (i = 0; i < s->nch; i++) {
@@ -290,19 +282,20 @@ static av_cold int channelmap_init(AVFilterContext *ctx)
         }
     }
 
-fail:
-    av_opt_free(s);
-    return ret;
+    return 0;
 }
 
 static int channelmap_query_formats(AVFilterContext *ctx)
 {
     ChannelMapContext *s = ctx->priv;
+    AVFilterChannelLayouts *channel_layouts = NULL;
+
+    ff_add_channel_layout(&channel_layouts, s->output_layout);
 
     ff_set_common_formats(ctx, ff_planar_sample_fmts());
     ff_set_common_samplerates(ctx, ff_all_samplerates());
     ff_channel_layouts_ref(ff_all_channel_layouts(), &ctx->inputs[0]->out_channel_layouts);
-    ff_channel_layouts_ref(s->channel_layouts,       &ctx->outputs[0]->in_channel_layouts);
+    ff_channel_layouts_ref(channel_layouts,          &ctx->outputs[0]->in_channel_layouts);
 
     return 0;
 }
@@ -349,6 +342,8 @@ static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
         memcpy(buf->data, buf->extended_data,
            FFMIN(FF_ARRAY_ELEMS(buf->data), nch_out) * sizeof(buf->data[0]));
 
+    buf->channel_layout = outlink->channel_layout;
+
     return ff_filter_frame(outlink, buf);
 }
 
@@ -362,24 +357,25 @@ static int channelmap_config_input(AVFilterLink *inlink)
     char layout_name[256];
 
     for (i = 0; i < s->nch; i++) {
+        struct ChannelMap *m = &s->map[i];
+
         if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
-            s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
-                inlink->channel_layout, s->map[i].in_channel);
+            m->in_channel_idx = av_get_channel_layout_channel_index(
+                inlink->channel_layout, m->in_channel);
         }
 
-        if (s->map[i].in_channel_idx < 0 ||
-            s->map[i].in_channel_idx >= nb_channels) {
+        if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
             av_get_channel_layout_string(layout_name, sizeof(layout_name),
                                          0, inlink->channel_layout);
-            if (s->map[i].in_channel) {
-                channel_name = av_get_channel_name(s->map[i].in_channel);
+            if (m->in_channel) {
+                channel_name = av_get_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",
-                       s->map[i].in_channel_idx, layout_name);
+                       m->in_channel_idx, layout_name);
             }
             err = AVERROR(EINVAL);
         }
@@ -406,7 +402,7 @@ static const AVFilterPad avfilter_af_channelmap_outputs[] = {
     { NULL }
 };
 
-AVFilter avfilter_af_channelmap = {
+AVFilter ff_af_channelmap = {
     .name          = "channelmap",
     .description   = NULL_IF_CONFIG_SMALL("Remap audio channels."),
     .init          = channelmap_init,