2 * Copyright (c) 2002 Anders Johansson <ajh@atri.curtin.edu.au>
3 * Copyright (c) 2011 Clément Bœsch <ubitux@gmail.com>
4 * Copyright (c) 2011 Nicolas George <nicolas.george@normalesup.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Audio panning filter (channels mixing)
26 * Original code written by Anders Johansson for MPlayer,
27 * reimplemented for FFmpeg.
31 #include "libavutil/audioconvert.h"
32 #include "libavutil/avstring.h"
36 #define MAX_CHANNELS 63
39 int64_t out_channel_layout;
41 double d[MAX_CHANNELS][MAX_CHANNELS];
42 // i is 1:7:8 fixed-point, i.e. in [-128*256; +128*256[
43 int i[MAX_CHANNELS][MAX_CHANNELS];
47 int nb_input_channels;
48 int nb_output_channels;
51 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
54 int len, i, channel_id;
55 int64_t layout, layout0;
57 if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
58 layout0 = layout = av_get_channel_layout(buf);
59 for (i = 32; i > 0; i >>= 1) {
60 if (layout >= (int64_t)1 << i) {
65 if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
66 return AVERROR(EINVAL);
67 *rchannel = channel_id;
72 if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
73 channel_id >= 0 && channel_id < MAX_CHANNELS) {
74 *rchannel = channel_id;
79 return AVERROR(EINVAL);
82 static void skip_spaces(char **arg)
86 sscanf(*arg, " %n", &len);
90 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
92 PanContext *const pan = ctx->priv;
93 char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
94 int out_ch_id, in_ch_id, len, named;
95 int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
99 return AVERROR(ENOMEM);
100 arg = av_strtok(args, ":", &tokenizer);
101 pan->out_channel_layout = av_get_channel_layout(arg);
102 if (!pan->out_channel_layout) {
103 av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
104 return AVERROR(EINVAL);
106 pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
108 /* parse channel specifications */
109 while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
111 if (parse_channel_name(&arg, &out_ch_id, &named)) {
112 av_log(ctx, AV_LOG_ERROR,
113 "Expected out channel name, got \"%.8s\"\n", arg);
114 return AVERROR(EINVAL);
117 if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
118 av_log(ctx, AV_LOG_ERROR,
119 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
120 return AVERROR(EINVAL);
122 /* get the channel number in the output channel layout:
123 * out_channel_layout & ((1 << out_ch_id) - 1) are all the
124 * channels that come before out_ch_id,
125 * so their count is the index of out_ch_id */
126 out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
128 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
129 av_log(ctx, AV_LOG_ERROR,
130 "Invalid out channel name \"%.8s\"\n", arg0);
131 return AVERROR(EINVAL);
135 } else if (*arg == '<') {
136 pan->need_renorm |= (int64_t)1 << out_ch_id;
139 av_log(ctx, AV_LOG_ERROR,
140 "Syntax error after channel name in \"%.8s\"\n", arg0);
141 return AVERROR(EINVAL);
146 if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
148 if (parse_channel_name(&arg, &in_ch_id, &named)){
149 av_log(ctx, AV_LOG_ERROR,
150 "Expected in channel name, got \"%.8s\"\n", arg);
151 return AVERROR(EINVAL);
153 nb_in_channels[named]++;
154 if (nb_in_channels[!named]) {
155 av_log(ctx, AV_LOG_ERROR,
156 "Can not mix named and numbered channels\n");
157 return AVERROR(EINVAL);
159 pan->gain.d[out_ch_id][in_ch_id] = gain;
163 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
164 return AVERROR(EINVAL);
170 pan->need_renumber = !!nb_in_channels[1];
176 static int query_formats(AVFilterContext *ctx)
178 PanContext *pan = ctx->priv;
179 AVFilterLink *inlink = ctx->inputs[0];
180 AVFilterLink *outlink = ctx->outputs[0];
181 AVFilterFormats *formats;
183 const enum AVSampleFormat sample_fmts[] = {AV_SAMPLE_FMT_S16, -1};
184 const int packing_fmts[] = {AVFILTER_PACKED, -1};
186 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
187 avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
189 // inlink supports any channel layout
190 formats = avfilter_make_all_channel_layouts();
191 avfilter_formats_ref(formats, &inlink->out_chlayouts);
193 // outlink supports only requested output channel layout
195 avfilter_add_format(&formats, pan->out_channel_layout);
196 avfilter_formats_ref(formats, &outlink->in_chlayouts);
200 static int config_props(AVFilterLink *link)
202 AVFilterContext *ctx = link->dst;
203 PanContext *pan = ctx->priv;
204 char buf[1024], *cur;
208 pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
209 if (pan->need_renumber) {
210 // input channels were given by their name: renumber them
211 for (i = j = 0; i < MAX_CHANNELS; i++) {
212 if ((link->channel_layout >> i) & 1) {
213 for (k = 0; k < pan->nb_output_channels; k++)
214 pan->gain.d[k][j] = pan->gain.d[k][i];
220 for (i = 0; i < pan->nb_output_channels; i++) {
221 if (!((pan->need_renorm >> i) & 1))
224 for (j = 0; j < pan->nb_input_channels; j++)
225 t += pan->gain.d[i][j];
226 if (t > -1E-5 && t < 1E-5) {
227 // t is almost 0 but not exactly, this is probably a mistake
229 av_log(ctx, AV_LOG_WARNING,
230 "Degenerate coefficients while renormalizing\n");
233 for (j = 0; j < pan->nb_input_channels; j++)
234 pan->gain.d[i][j] /= t;
237 for (i = 0; i < pan->nb_output_channels; i++) {
239 for (j = 0; j < pan->nb_input_channels; j++) {
240 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
241 j ? " + " : "", pan->gain.d[i][j], j);
242 cur += FFMIN(buf + sizeof(buf) - cur, r);
244 av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
246 // convert to integer
247 for (i = 0; i < pan->nb_output_channels; i++) {
248 for (j = 0; j < pan->nb_input_channels; j++) {
249 if (pan->gain.d[i][j] < -128 || pan->gain.d[i][j] > 128)
250 av_log(ctx, AV_LOG_WARNING,
251 "Gain #%d->#%d too large, clamped\n", j, i);
252 pan->gain.i[i][j] = av_clipf(pan->gain.d[i][j], -128, 128) * 256.0;
259 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
261 PanContext *const pan = inlink->dst->priv;
262 int i, o, n = insamples->audio->nb_samples;
265 const int16_t *in = (int16_t *)insamples->data[0];
266 const int16_t *in_end = in + n * pan->nb_input_channels;
269 AVFilterLink *const outlink = inlink->dst->outputs[0];
270 AVFilterBufferRef *outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
271 int16_t *out = (int16_t *)outsamples->data[0];
273 for (; in < in_end; in += pan->nb_input_channels) {
274 for (o = 0; o < pan->nb_output_channels; o++) {
276 for (i = 0; i < pan->nb_input_channels; i++)
277 v += pan->gain.i[o][i] * in[i];
282 avfilter_filter_samples(outlink, outsamples);
283 avfilter_unref_buffer(insamples);
286 AVFilter avfilter_af_pan = {
288 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
289 .priv_size = sizeof(PanContext),
291 .query_formats = query_formats,
293 .inputs = (const AVFilterPad[]) {
295 .type = AVMEDIA_TYPE_AUDIO,
296 .config_props = config_props,
297 .filter_samples = filter_samples,
298 .min_perms = AV_PERM_READ, },
301 .outputs = (const AVFilterPad[]) {
303 .type = AVMEDIA_TYPE_AUDIO, },