00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030 #include <stdio.h>
00031 #include "libavutil/audioconvert.h"
00032 #include "libavutil/avstring.h"
00033 #include "libavutil/opt.h"
00034 #include "libswresample/swresample.h"
00035 #include "audio.h"
00036 #include "avfilter.h"
00037 #include "formats.h"
00038 #include "internal.h"
00039
00040 #define MAX_CHANNELS 63
00041
00042 typedef struct PanContext {
00043 int64_t out_channel_layout;
00044 double gain[MAX_CHANNELS][MAX_CHANNELS];
00045 int64_t need_renorm;
00046 int need_renumber;
00047 int nb_input_channels;
00048 int nb_output_channels;
00049
00050 int pure_gains;
00051
00052 int channel_map[SWR_CH_MAX];
00053 struct SwrContext *swr;
00054 } PanContext;
00055
00056 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
00057 {
00058 char buf[8];
00059 int len, i, channel_id = 0;
00060 int64_t layout, layout0;
00061
00062
00063 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
00064 layout0 = layout = av_get_channel_layout(buf);
00065
00066 for (i = 32; i > 0; i >>= 1) {
00067 if (layout >= (int64_t)1 << i) {
00068 channel_id += i;
00069 layout >>= i;
00070 }
00071 }
00072
00073 if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
00074 return AVERROR(EINVAL);
00075 *rchannel = channel_id;
00076 *rnamed = 1;
00077 *arg += len;
00078 return 0;
00079 }
00080
00081 if (sscanf(*arg, "c%d%n", &channel_id, &len) &&
00082 channel_id >= 0 && channel_id < MAX_CHANNELS) {
00083 *rchannel = channel_id;
00084 *rnamed = 0;
00085 *arg += len;
00086 return 0;
00087 }
00088 return AVERROR(EINVAL);
00089 }
00090
00091 static void skip_spaces(char **arg)
00092 {
00093 int len = 0;
00094
00095 sscanf(*arg, " %n", &len);
00096 *arg += len;
00097 }
00098
00099 static av_cold int init(AVFilterContext *ctx, const char *args0)
00100 {
00101 PanContext *const pan = ctx->priv;
00102 char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
00103 int out_ch_id, in_ch_id, len, named, ret;
00104 int nb_in_channels[2] = { 0, 0 };
00105 double gain;
00106
00107 if (!args0) {
00108 av_log(ctx, AV_LOG_ERROR,
00109 "pan filter needs a channel layout and a set "
00110 "of channels definitions as parameter\n");
00111 return AVERROR(EINVAL);
00112 }
00113 if (!args)
00114 return AVERROR(ENOMEM);
00115 arg = av_strtok(args, ":", &tokenizer);
00116 ret = ff_parse_channel_layout(&pan->out_channel_layout, arg, ctx);
00117 if (ret < 0)
00118 return ret;
00119 pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
00120
00121
00122 while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
00123
00124 if (parse_channel_name(&arg, &out_ch_id, &named)) {
00125 av_log(ctx, AV_LOG_ERROR,
00126 "Expected out channel name, got \"%.8s\"\n", arg);
00127 return AVERROR(EINVAL);
00128 }
00129 if (named) {
00130 if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
00131 av_log(ctx, AV_LOG_ERROR,
00132 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
00133 return AVERROR(EINVAL);
00134 }
00135
00136
00137
00138
00139 out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
00140 }
00141 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
00142 av_log(ctx, AV_LOG_ERROR,
00143 "Invalid out channel name \"%.8s\"\n", arg0);
00144 return AVERROR(EINVAL);
00145 }
00146 skip_spaces(&arg);
00147 if (*arg == '=') {
00148 arg++;
00149 } else if (*arg == '<') {
00150 pan->need_renorm |= (int64_t)1 << out_ch_id;
00151 arg++;
00152 } else {
00153 av_log(ctx, AV_LOG_ERROR,
00154 "Syntax error after channel name in \"%.8s\"\n", arg0);
00155 return AVERROR(EINVAL);
00156 }
00157
00158 while (1) {
00159 gain = 1;
00160 if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
00161 arg += len;
00162 if (parse_channel_name(&arg, &in_ch_id, &named)){
00163 av_log(ctx, AV_LOG_ERROR,
00164 "Expected in channel name, got \"%.8s\"\n", arg);
00165 return AVERROR(EINVAL);
00166 }
00167 nb_in_channels[named]++;
00168 if (nb_in_channels[!named]) {
00169 av_log(ctx, AV_LOG_ERROR,
00170 "Can not mix named and numbered channels\n");
00171 return AVERROR(EINVAL);
00172 }
00173 pan->gain[out_ch_id][in_ch_id] = gain;
00174 skip_spaces(&arg);
00175 if (!*arg)
00176 break;
00177 if (*arg != '+') {
00178 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
00179 return AVERROR(EINVAL);
00180 }
00181 arg++;
00182 }
00183 }
00184 pan->need_renumber = !!nb_in_channels[1];
00185
00186 av_free(args);
00187 return 0;
00188 }
00189
00190 static int are_gains_pure(const PanContext *pan)
00191 {
00192 int i, j;
00193
00194 for (i = 0; i < MAX_CHANNELS; i++) {
00195 int nb_gain = 0;
00196
00197 for (j = 0; j < MAX_CHANNELS; j++) {
00198 double gain = pan->gain[i][j];
00199
00200
00201
00202 if (gain != 0. && gain != 1.)
00203 return 0;
00204
00205 if (gain && nb_gain++)
00206 return 0;
00207 }
00208 }
00209 return 1;
00210 }
00211
00212 static int query_formats(AVFilterContext *ctx)
00213 {
00214 PanContext *pan = ctx->priv;
00215 AVFilterLink *inlink = ctx->inputs[0];
00216 AVFilterLink *outlink = ctx->outputs[0];
00217 AVFilterFormats *formats = NULL;
00218 AVFilterChannelLayouts *layouts;
00219
00220 pan->pure_gains = are_gains_pure(pan);
00221
00222 ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
00223
00224 formats = ff_all_samplerates();
00225 if (!formats)
00226 return AVERROR(ENOMEM);
00227 ff_set_common_samplerates(ctx, formats);
00228
00229
00230 layouts = ff_all_channel_layouts();
00231 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
00232
00233
00234 layouts = NULL;
00235 ff_add_channel_layout(&layouts, pan->out_channel_layout);
00236 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
00237 return 0;
00238 }
00239
00240 static int config_props(AVFilterLink *link)
00241 {
00242 AVFilterContext *ctx = link->dst;
00243 PanContext *pan = ctx->priv;
00244 char buf[1024], *cur;
00245 int i, j, k, r;
00246 double t;
00247
00248 pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00249 if (pan->need_renumber) {
00250
00251 for (i = j = 0; i < MAX_CHANNELS; i++) {
00252 if ((link->channel_layout >> i) & 1) {
00253 for (k = 0; k < pan->nb_output_channels; k++)
00254 pan->gain[k][j] = pan->gain[k][i];
00255 j++;
00256 }
00257 }
00258 }
00259
00260
00261
00262 if (pan->nb_input_channels > SWR_CH_MAX ||
00263 pan->nb_output_channels > SWR_CH_MAX) {
00264 av_log(ctx, AV_LOG_ERROR,
00265 "libswresample support a maximum of %d channels. "
00266 "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
00267 return AVERROR_PATCHWELCOME;
00268 }
00269
00270
00271 pan->swr = swr_alloc_set_opts(pan->swr,
00272 pan->out_channel_layout, link->format, link->sample_rate,
00273 link->channel_layout, link->format, link->sample_rate,
00274 0, ctx);
00275 if (!pan->swr)
00276 return AVERROR(ENOMEM);
00277
00278
00279 if (pan->pure_gains) {
00280
00281
00282 for (i = 0; i < pan->nb_output_channels; i++) {
00283 int ch_id = -1;
00284 for (j = 0; j < pan->nb_input_channels; j++) {
00285 if (pan->gain[i][j]) {
00286 ch_id = j;
00287 break;
00288 }
00289 }
00290 pan->channel_map[i] = ch_id;
00291 }
00292
00293 av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
00294 av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
00295 swr_set_channel_mapping(pan->swr, pan->channel_map);
00296 } else {
00297
00298 for (i = 0; i < pan->nb_output_channels; i++) {
00299 if (!((pan->need_renorm >> i) & 1))
00300 continue;
00301 t = 0;
00302 for (j = 0; j < pan->nb_input_channels; j++)
00303 t += pan->gain[i][j];
00304 if (t > -1E-5 && t < 1E-5) {
00305
00306 if (t)
00307 av_log(ctx, AV_LOG_WARNING,
00308 "Degenerate coefficients while renormalizing\n");
00309 continue;
00310 }
00311 for (j = 0; j < pan->nb_input_channels; j++)
00312 pan->gain[i][j] /= t;
00313 }
00314 av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
00315 av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
00316 swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
00317 }
00318
00319 r = swr_init(pan->swr);
00320 if (r < 0)
00321 return r;
00322
00323
00324 for (i = 0; i < pan->nb_output_channels; i++) {
00325 cur = buf;
00326 for (j = 0; j < pan->nb_input_channels; j++) {
00327 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
00328 j ? " + " : "", pan->gain[i][j], j);
00329 cur += FFMIN(buf + sizeof(buf) - cur, r);
00330 }
00331 av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
00332 }
00333
00334 if (pan->pure_gains) {
00335 av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
00336 for (i = 0; i < pan->nb_output_channels; i++)
00337 if (pan->channel_map[i] < 0)
00338 av_log(ctx, AV_LOG_INFO, " M");
00339 else
00340 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
00341 av_log(ctx, AV_LOG_INFO, "\n");
00342 return 0;
00343 }
00344 return 0;
00345 }
00346
00347 static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00348 {
00349 int ret;
00350 int n = insamples->audio->nb_samples;
00351 AVFilterLink *const outlink = inlink->dst->outputs[0];
00352 AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
00353 PanContext *pan = inlink->dst->priv;
00354
00355 swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
00356 avfilter_copy_buffer_ref_props(outsamples, insamples);
00357 outsamples->audio->channel_layout = outlink->channel_layout;
00358
00359 ret = ff_filter_samples(outlink, outsamples);
00360 avfilter_unref_buffer(insamples);
00361 return ret;
00362 }
00363
00364 static av_cold void uninit(AVFilterContext *ctx)
00365 {
00366 PanContext *pan = ctx->priv;
00367 swr_free(&pan->swr);
00368 }
00369
00370 AVFilter avfilter_af_pan = {
00371 .name = "pan",
00372 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
00373 .priv_size = sizeof(PanContext),
00374 .init = init,
00375 .uninit = uninit,
00376 .query_formats = query_formats,
00377
00378 .inputs = (const AVFilterPad[]) {
00379 { .name = "default",
00380 .type = AVMEDIA_TYPE_AUDIO,
00381 .config_props = config_props,
00382 .filter_samples = filter_samples,
00383 .min_perms = AV_PERM_READ, },
00384 { .name = NULL}
00385 },
00386 .outputs = (const AVFilterPad[]) {
00387 { .name = "default",
00388 .type = AVMEDIA_TYPE_AUDIO, },
00389 { .name = NULL}
00390 },
00391 };