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