]> git.sesse.net Git - ffmpeg/blob - libavfilter/af_channelsplit.c
aacdec: Fix popping channel layouts.
[ffmpeg] / libavfilter / af_channelsplit.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /**
20  * @file
21  * Channel split filter
22  *
23  * Split an audio stream into per-channel streams.
24  */
25
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/opt.h"
28
29 #include "audio.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 typedef struct ChannelSplitContext {
35     const AVClass *class;
36
37     uint64_t channel_layout;
38     char    *channel_layout_str;
39 } ChannelSplitContext;
40
41 #define OFFSET(x) offsetof(ChannelSplitContext, x)
42 #define A AV_OPT_FLAG_AUDIO_PARAM
43 static const AVOption options[] = {
44     { "channel_layout", "Input channel layout.", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, { .str = "stereo" }, .flags = A },
45     { NULL },
46 };
47
48 static const AVClass channelsplit_class = {
49     .class_name = "channelsplit filter",
50     .item_name  = av_default_item_name,
51     .option     = options,
52     .version    = LIBAVUTIL_VERSION_INT,
53 };
54
55 static int init(AVFilterContext *ctx, const char *arg, void *opaque)
56 {
57     ChannelSplitContext *s = ctx->priv;
58     int nb_channels;
59     int ret = 0, i;
60
61     s->class = &channelsplit_class;
62     av_opt_set_defaults(s);
63     if ((ret = av_set_options_string(s, arg, "=", ":")) < 0) {
64         av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", arg);
65         return ret;
66     }
67     if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
68         av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
69                s->channel_layout_str);
70         ret = AVERROR(EINVAL);
71         goto fail;
72     }
73
74     nb_channels = av_get_channel_layout_nb_channels(s->channel_layout);
75     for (i = 0; i < nb_channels; i++) {
76         uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
77         AVFilterPad pad  = { 0 };
78
79         pad.type = AVMEDIA_TYPE_AUDIO;
80         pad.name = av_get_channel_name(channel);
81
82         ff_insert_outpad(ctx, i, &pad);
83     }
84
85 fail:
86     av_opt_free(s);
87     return ret;
88 }
89
90 static int query_formats(AVFilterContext *ctx)
91 {
92     ChannelSplitContext *s = ctx->priv;
93     AVFilterChannelLayouts *in_layouts = NULL;
94     int i;
95
96     ff_set_common_formats    (ctx, ff_planar_sample_fmts());
97     ff_set_common_samplerates(ctx, ff_all_samplerates());
98
99     ff_add_channel_layout(&in_layouts, s->channel_layout);
100     ff_channel_layouts_ref(in_layouts, &ctx->inputs[0]->out_channel_layouts);
101
102     for (i = 0; i < ctx->nb_outputs; i++) {
103         AVFilterChannelLayouts *out_layouts = NULL;
104         uint64_t channel = av_channel_layout_extract_channel(s->channel_layout, i);
105
106         ff_add_channel_layout(&out_layouts, channel);
107         ff_channel_layouts_ref(out_layouts, &ctx->outputs[i]->in_channel_layouts);
108     }
109
110     return 0;
111 }
112
113 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
114 {
115     AVFilterContext *ctx = inlink->dst;
116     int i;
117
118     for (i = 0; i < ctx->nb_outputs; i++) {
119         AVFilterBufferRef *buf_out = avfilter_ref_buffer(buf, ~AV_PERM_WRITE);
120
121         if (!buf_out)
122             return;
123
124         buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
125         buf_out->audio->channel_layout =
126             av_channel_layout_extract_channel(buf->audio->channel_layout, i);
127
128         ff_filter_samples(ctx->outputs[i], buf_out);
129     }
130     avfilter_unref_buffer(buf);
131 }
132
133 AVFilter avfilter_af_channelsplit = {
134     .name           = "channelsplit",
135     .description    = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams"),
136     .priv_size      = sizeof(ChannelSplitContext),
137
138     .init           = init,
139     .query_formats  = query_formats,
140
141     .inputs  = (const AVFilterPad[]){{ .name           = "default",
142                                        .type           = AVMEDIA_TYPE_AUDIO,
143                                        .filter_samples = filter_samples, },
144                                      { NULL }},
145     .outputs = (const AVFilterPad[]){{ NULL }},
146 };