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/avstring.h"
00032 #include "libavutil/channel_layout.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 goto fail;
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 ret = AVERROR(EINVAL);
00128 goto fail;
00129 }
00130 if (named) {
00131 if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
00132 av_log(ctx, AV_LOG_ERROR,
00133 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
00134 ret = AVERROR(EINVAL);
00135 goto fail;
00136 }
00137
00138
00139
00140
00141 out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
00142 }
00143 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
00144 av_log(ctx, AV_LOG_ERROR,
00145 "Invalid out channel name \"%.8s\"\n", arg0);
00146 ret = AVERROR(EINVAL);
00147 goto fail;
00148 }
00149 skip_spaces(&arg);
00150 if (*arg == '=') {
00151 arg++;
00152 } else if (*arg == '<') {
00153 pan->need_renorm |= (int64_t)1 << out_ch_id;
00154 arg++;
00155 } else {
00156 av_log(ctx, AV_LOG_ERROR,
00157 "Syntax error after channel name in \"%.8s\"\n", arg0);
00158 ret = AVERROR(EINVAL);
00159 goto fail;
00160 }
00161
00162 while (1) {
00163 gain = 1;
00164 if (sscanf(arg, "%lf%n *%n", &gain, &len, &len))
00165 arg += len;
00166 if (parse_channel_name(&arg, &in_ch_id, &named)){
00167 av_log(ctx, AV_LOG_ERROR,
00168 "Expected in channel name, got \"%.8s\"\n", arg);
00169 ret = AVERROR(EINVAL);
00170 goto fail;
00171 }
00172 nb_in_channels[named]++;
00173 if (nb_in_channels[!named]) {
00174 av_log(ctx, AV_LOG_ERROR,
00175 "Can not mix named and numbered channels\n");
00176 ret = AVERROR(EINVAL);
00177 goto fail;
00178 }
00179 pan->gain[out_ch_id][in_ch_id] = gain;
00180 skip_spaces(&arg);
00181 if (!*arg)
00182 break;
00183 if (*arg != '+') {
00184 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
00185 ret = AVERROR(EINVAL);
00186 goto fail;
00187 }
00188 arg++;
00189 }
00190 }
00191 pan->need_renumber = !!nb_in_channels[1];
00192
00193 ret = 0;
00194 fail:
00195 av_free(args);
00196 return ret;
00197 }
00198
00199 static int are_gains_pure(const PanContext *pan)
00200 {
00201 int i, j;
00202
00203 for (i = 0; i < MAX_CHANNELS; i++) {
00204 int nb_gain = 0;
00205
00206 for (j = 0; j < MAX_CHANNELS; j++) {
00207 double gain = pan->gain[i][j];
00208
00209
00210
00211 if (gain != 0. && gain != 1.)
00212 return 0;
00213
00214 if (gain && nb_gain++)
00215 return 0;
00216 }
00217 }
00218 return 1;
00219 }
00220
00221 static int query_formats(AVFilterContext *ctx)
00222 {
00223 PanContext *pan = ctx->priv;
00224 AVFilterLink *inlink = ctx->inputs[0];
00225 AVFilterLink *outlink = ctx->outputs[0];
00226 AVFilterFormats *formats = NULL;
00227 AVFilterChannelLayouts *layouts;
00228
00229 pan->pure_gains = are_gains_pure(pan);
00230
00231 ff_set_common_formats(ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
00232
00233 formats = ff_all_samplerates();
00234 if (!formats)
00235 return AVERROR(ENOMEM);
00236 ff_set_common_samplerates(ctx, formats);
00237
00238
00239 layouts = ff_all_channel_layouts();
00240 ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
00241
00242
00243 layouts = NULL;
00244 ff_add_channel_layout(&layouts, pan->out_channel_layout);
00245 ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
00246 return 0;
00247 }
00248
00249 static int config_props(AVFilterLink *link)
00250 {
00251 AVFilterContext *ctx = link->dst;
00252 PanContext *pan = ctx->priv;
00253 char buf[1024], *cur;
00254 int i, j, k, r;
00255 double t;
00256
00257 pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00258 if (pan->need_renumber) {
00259
00260 for (i = j = 0; i < MAX_CHANNELS; i++) {
00261 if ((link->channel_layout >> i) & 1) {
00262 for (k = 0; k < pan->nb_output_channels; k++)
00263 pan->gain[k][j] = pan->gain[k][i];
00264 j++;
00265 }
00266 }
00267 }
00268
00269
00270
00271 if (pan->nb_input_channels > SWR_CH_MAX ||
00272 pan->nb_output_channels > SWR_CH_MAX) {
00273 av_log(ctx, AV_LOG_ERROR,
00274 "libswresample support a maximum of %d channels. "
00275 "Feel free to ask for a higher limit.\n", SWR_CH_MAX);
00276 return AVERROR_PATCHWELCOME;
00277 }
00278
00279
00280 pan->swr = swr_alloc_set_opts(pan->swr,
00281 pan->out_channel_layout, link->format, link->sample_rate,
00282 link->channel_layout, link->format, link->sample_rate,
00283 0, ctx);
00284 if (!pan->swr)
00285 return AVERROR(ENOMEM);
00286
00287
00288 if (pan->pure_gains) {
00289
00290
00291 for (i = 0; i < pan->nb_output_channels; i++) {
00292 int ch_id = -1;
00293 for (j = 0; j < pan->nb_input_channels; j++) {
00294 if (pan->gain[i][j]) {
00295 ch_id = j;
00296 break;
00297 }
00298 }
00299 pan->channel_map[i] = ch_id;
00300 }
00301
00302 av_opt_set_int(pan->swr, "icl", pan->out_channel_layout, 0);
00303 av_opt_set_int(pan->swr, "uch", pan->nb_output_channels, 0);
00304 swr_set_channel_mapping(pan->swr, pan->channel_map);
00305 } else {
00306
00307 for (i = 0; i < pan->nb_output_channels; i++) {
00308 if (!((pan->need_renorm >> i) & 1))
00309 continue;
00310 t = 0;
00311 for (j = 0; j < pan->nb_input_channels; j++)
00312 t += pan->gain[i][j];
00313 if (t > -1E-5 && t < 1E-5) {
00314
00315 if (t)
00316 av_log(ctx, AV_LOG_WARNING,
00317 "Degenerate coefficients while renormalizing\n");
00318 continue;
00319 }
00320 for (j = 0; j < pan->nb_input_channels; j++)
00321 pan->gain[i][j] /= t;
00322 }
00323 av_opt_set_int(pan->swr, "icl", link->channel_layout, 0);
00324 av_opt_set_int(pan->swr, "ocl", pan->out_channel_layout, 0);
00325 swr_set_matrix(pan->swr, pan->gain[0], pan->gain[1] - pan->gain[0]);
00326 }
00327
00328 r = swr_init(pan->swr);
00329 if (r < 0)
00330 return r;
00331
00332
00333 for (i = 0; i < pan->nb_output_channels; i++) {
00334 cur = buf;
00335 for (j = 0; j < pan->nb_input_channels; j++) {
00336 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
00337 j ? " + " : "", pan->gain[i][j], j);
00338 cur += FFMIN(buf + sizeof(buf) - cur, r);
00339 }
00340 av_log(ctx, AV_LOG_VERBOSE, "o%d = %s\n", i, buf);
00341 }
00342
00343 if (pan->pure_gains) {
00344 av_log(ctx, AV_LOG_INFO, "Pure channel mapping detected:");
00345 for (i = 0; i < pan->nb_output_channels; i++)
00346 if (pan->channel_map[i] < 0)
00347 av_log(ctx, AV_LOG_INFO, " M");
00348 else
00349 av_log(ctx, AV_LOG_INFO, " %d", pan->channel_map[i]);
00350 av_log(ctx, AV_LOG_INFO, "\n");
00351 return 0;
00352 }
00353 return 0;
00354 }
00355
00356 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00357 {
00358 int ret;
00359 int n = insamples->audio->nb_samples;
00360 AVFilterLink *const outlink = inlink->dst->outputs[0];
00361 AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
00362 PanContext *pan = inlink->dst->priv;
00363
00364 swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
00365 avfilter_copy_buffer_ref_props(outsamples, insamples);
00366 outsamples->audio->channel_layout = outlink->channel_layout;
00367
00368 ret = ff_filter_frame(outlink, outsamples);
00369 avfilter_unref_buffer(insamples);
00370 return ret;
00371 }
00372
00373 static av_cold void uninit(AVFilterContext *ctx)
00374 {
00375 PanContext *pan = ctx->priv;
00376 swr_free(&pan->swr);
00377 }
00378
00379 static const AVFilterPad pan_inputs[] = {
00380 {
00381 .name = "default",
00382 .type = AVMEDIA_TYPE_AUDIO,
00383 .config_props = config_props,
00384 .filter_frame = filter_frame,
00385 .min_perms = AV_PERM_READ,
00386 },
00387 { NULL }
00388 };
00389
00390 static const AVFilterPad pan_outputs[] = {
00391 {
00392 .name = "default",
00393 .type = AVMEDIA_TYPE_AUDIO,
00394 },
00395 { NULL }
00396 };
00397
00398 AVFilter avfilter_af_pan = {
00399 .name = "pan",
00400 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
00401 .priv_size = sizeof(PanContext),
00402 .init = init,
00403 .uninit = uninit,
00404 .query_formats = query_formats,
00405 .inputs = pan_inputs,
00406 .outputs = pan_outputs,
00407 };