2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Channel split filter
23 * Split an audio stream into per-channel streams.
26 #include "libavutil/attributes.h"
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/opt.h"
36 typedef struct ChannelSplitContext {
39 uint64_t channel_layout;
40 char *channel_layout_str;
44 } ChannelSplitContext;
46 #define OFFSET(x) offsetof(ChannelSplitContext, x)
47 #define A AV_OPT_FLAG_AUDIO_PARAM
48 #define F AV_OPT_FLAG_FILTERING_PARAM
49 static const AVOption channelsplit_options[] = {
50 { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
51 { "channels", "Channels to extract.", OFFSET(channels_str), AV_OPT_TYPE_STRING, { .str = "all" }, .flags = A|F },
55 AVFILTER_DEFINE_CLASS(channelsplit);
57 static av_cold int init(AVFilterContext *ctx)
59 ChannelSplitContext *s = ctx->priv;
60 uint64_t channel_layout;
62 int all = 0, ret = 0, i;
64 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
65 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
66 s->channel_layout_str);
67 ret = AVERROR(EINVAL);
72 if (!strcmp(s->channels_str, "all")) {
73 nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
74 channel_layout = s->channel_layout;
77 if ((ret = av_get_extended_channel_layout(s->channels_str, &channel_layout, &nb_channels)) < 0)
81 for (i = 0; i < nb_channels; i++) {
82 uint64_t channel = av_channel_layout_extract_channel(channel_layout, i);
83 AVFilterPad pad = { 0 };
85 pad.type = AVMEDIA_TYPE_AUDIO;
86 pad.name = av_get_channel_name(channel);
91 if ((ret = av_get_channel_layout_channel_index(s->channel_layout, channel)) < 0) {
92 av_log(ctx, AV_LOG_ERROR, "Channel name '%s' not present in channel layout '%s'.\n",
93 av_get_channel_name(channel), s->channel_layout_str);
100 if ((ret = ff_insert_outpad(ctx, i, &pad)) < 0) {
109 static int query_formats(AVFilterContext *ctx)
111 ChannelSplitContext *s = ctx->priv;
112 AVFilterChannelLayouts *in_layouts = NULL;
115 if ((ret = ff_set_common_formats(ctx, ff_planar_sample_fmts())) < 0 ||
116 (ret = ff_set_common_samplerates(ctx, ff_all_samplerates())) < 0)
119 if ((ret = ff_add_channel_layout(&in_layouts, s->channel_layout)) < 0 ||
120 (ret = ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->outcfg.channel_layouts)) < 0)
123 for (i = 0; i < ctx->nb_outputs; i++) {
124 AVFilterChannelLayouts *out_layouts = NULL;
125 uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, s->map[i]);
127 if ((ret = ff_add_channel_layout(&out_layouts, channel)) < 0 ||
128 (ret = ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->incfg.channel_layouts)) < 0)
135 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
137 AVFilterContext *ctx = inlink->dst;
138 ChannelSplitContext *s = ctx->priv;
141 for (i = 0; i < ctx->nb_outputs; i++) {
142 AVFrame *buf_out = av_frame_clone(buf);
145 ret = AVERROR(ENOMEM);
149 buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[s->map[i]];
150 buf_out->channel_layout =
151 av_channel_layout_extract_channel(buf->channel_layout, s->map[i]);
152 buf_out->channels = 1;
154 ret = ff_filter_frame(ctx->outputs[i], buf_out);
162 static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
165 .type = AVMEDIA_TYPE_AUDIO,
166 .filter_frame = filter_frame,
171 AVFilter ff_af_channelsplit = {
172 .name = "channelsplit",
173 .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
174 .priv_size = sizeof(ChannelSplitContext),
175 .priv_class = &channelsplit_class,
177 .query_formats = query_formats,
178 .inputs = avfilter_af_channelsplit_inputs,
180 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,