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