2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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/channel_layout.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
35 typedef struct ChannelSplitContext {
38 uint64_t channel_layout;
39 char *channel_layout_str;
40 } ChannelSplitContext;
42 #define OFFSET(x) offsetof(ChannelSplitContext, x)
43 #define A AV_OPT_FLAG_AUDIO_PARAM
44 #define F AV_OPT_FLAG_FILTERING_PARAM
45 static const AVOption channelsplit_options[] = {
46 { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A|F },
50 AVFILTER_DEFINE_CLASS(channelsplit);
52 static int init(AVFilterContext *ctx)
54 ChannelSplitContext *s = ctx->priv;
58 if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
59 av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
60 s->channel_layout_str);
61 ret = AVERROR(EINVAL);
65 nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
66 for (i = 0; i < nb_channels; i++) {
67 uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
68 AVFilterPad pad = { 0 };
70 pad.type = AVMEDIA_TYPE_AUDIO;
71 pad.name = av_get_channel_name(channel);
73 ff_insert_outpad(ctx, i, &pad);
80 static int query_formats(AVFilterContext *ctx)
82 ChannelSplitContext *s = ctx->priv;
83 AVFilterChannelLayouts *in_layouts = NULL;
86 ff_set_common_formats (ctx, ff_planar_sample_fmts());
87 ff_set_common_samplerates(ctx, ff_all_samplerates());
89 ff_add_channel_layout(&in_layouts, s->channel_layout);
90 ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
92 for (i = 0; i < ctx->nb_outputs; i++) {
93 AVFilterChannelLayouts *out_layouts = NULL;
94 uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
96 ff_add_channel_layout(&out_layouts, channel);
97 ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
103 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
105 AVFilterContext *ctx = inlink->dst;
108 for (i = 0; i < ctx->nb_outputs; i++) {
109 AVFrame *buf_out = av_frame_clone(buf);
112 ret = AVERROR(ENOMEM);
116 buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
117 buf_out->channel_layout =
118 av_channel_layout_extract_channel(buf->channel_layout, i);
119 av_frame_set_channels(buf_out, 1);
121 ret = ff_filter_frame(ctx->outputs[i], buf_out);
129 static const AVFilterPad avfilter_af_channelsplit_inputs[] = {
132 .type = AVMEDIA_TYPE_AUDIO,
133 .filter_frame = filter_frame,
138 AVFilter avfilter_af_channelsplit = {
139 .name = "channelsplit",
140 .description = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
141 .priv_size = sizeof(ChannelSplitContext),
142 .priv_class = &channelsplit_class,
145 .query_formats = query_formats,
147 .inputs = avfilter_af_channelsplit_inputs,
150 .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS,