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 "avfilter.h"
00034 #include "internal.h"
00035
00036 #define MAX_CHANNELS 63
00037
00038 typedef struct {
00039 int64_t out_channel_layout;
00040 union {
00041 double d[MAX_CHANNELS][MAX_CHANNELS];
00042
00043 int i[MAX_CHANNELS][MAX_CHANNELS];
00044 } gain;
00045 int64_t need_renorm;
00046 int need_renumber;
00047 int nb_input_channels;
00048 int nb_output_channels;
00049 } PanContext;
00050
00051 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
00052 {
00053 char buf[8];
00054 int len, i, channel_id;
00055 int64_t layout, layout0;
00056
00057 if (sscanf(*arg, " %7[A-Z] %n", buf, &len)) {
00058 layout0 = layout = av_get_channel_layout(buf);
00059 for (i = 32; i > 0; i >>= 1) {
00060 if (layout >= (int64_t)1 << i) {
00061 channel_id += i;
00062 layout >>= i;
00063 }
00064 }
00065 if (channel_id >= MAX_CHANNELS || layout0 != (int64_t)1 << channel_id)
00066 return AVERROR(EINVAL);
00067 *rchannel = channel_id;
00068 *rnamed = 1;
00069 *arg += len;
00070 return 0;
00071 }
00072 if (sscanf(*arg, " c%d %n", &channel_id, &len) &&
00073 channel_id >= 0 && channel_id < MAX_CHANNELS) {
00074 *rchannel = channel_id;
00075 *rnamed = 0;
00076 *arg += len;
00077 return 0;
00078 }
00079 return AVERROR(EINVAL);
00080 }
00081
00082 static void skip_spaces(char **arg)
00083 {
00084 int len = 0;
00085
00086 sscanf(*arg, " %n", &len);
00087 *arg += len;
00088 }
00089
00090 static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
00091 {
00092 PanContext *const pan = ctx->priv;
00093 char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
00094 int out_ch_id, in_ch_id, len, named;
00095 int nb_in_channels[2] = { 0, 0 };
00096 double gain;
00097
00098 if (!args)
00099 return AVERROR(ENOMEM);
00100 arg = av_strtok(args, ":", &tokenizer);
00101 pan->out_channel_layout = av_get_channel_layout(arg);
00102 if (!pan->out_channel_layout) {
00103 av_log(ctx, AV_LOG_ERROR, "Unknown channel layout \"%s\"\n", arg);
00104 return AVERROR(EINVAL);
00105 }
00106 pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
00107
00108
00109 while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
00110
00111 if (parse_channel_name(&arg, &out_ch_id, &named)) {
00112 av_log(ctx, AV_LOG_ERROR,
00113 "Expected out channel name, got \"%.8s\"\n", arg);
00114 return AVERROR(EINVAL);
00115 }
00116 if (named) {
00117 if (!((pan->out_channel_layout >> out_ch_id) & 1)) {
00118 av_log(ctx, AV_LOG_ERROR,
00119 "Channel \"%.8s\" does not exist in the chosen layout\n", arg0);
00120 return AVERROR(EINVAL);
00121 }
00122
00123
00124
00125
00126 out_ch_id = av_get_channel_layout_nb_channels(pan->out_channel_layout & (((int64_t)1 << out_ch_id) - 1));
00127 }
00128 if (out_ch_id < 0 || out_ch_id >= pan->nb_output_channels) {
00129 av_log(ctx, AV_LOG_ERROR,
00130 "Invalid out channel name \"%.8s\"\n", arg0);
00131 return AVERROR(EINVAL);
00132 }
00133 if (*arg == '=') {
00134 arg++;
00135 } else if (*arg == '<') {
00136 pan->need_renorm |= (int64_t)1 << out_ch_id;
00137 arg++;
00138 } else {
00139 av_log(ctx, AV_LOG_ERROR,
00140 "Syntax error after channel name in \"%.8s\"\n", arg0);
00141 return AVERROR(EINVAL);
00142 }
00143
00144 while (1) {
00145 gain = 1;
00146 if (sscanf(arg, " %lf %n* %n", &gain, &len, &len))
00147 arg += len;
00148 if (parse_channel_name(&arg, &in_ch_id, &named)){
00149 av_log(ctx, AV_LOG_ERROR,
00150 "Expected in channel name, got \"%.8s\"\n", arg);
00151 return AVERROR(EINVAL);
00152 }
00153 nb_in_channels[named]++;
00154 if (nb_in_channels[!named]) {
00155 av_log(ctx, AV_LOG_ERROR,
00156 "Can not mix named and numbered channels\n");
00157 return AVERROR(EINVAL);
00158 }
00159 pan->gain.d[out_ch_id][in_ch_id] = gain;
00160 if (!*arg)
00161 break;
00162 if (*arg != '+') {
00163 av_log(ctx, AV_LOG_ERROR, "Syntax error near \"%.8s\"\n", arg);
00164 return AVERROR(EINVAL);
00165 }
00166 arg++;
00167 skip_spaces(&arg);
00168 }
00169 }
00170 pan->need_renumber = !!nb_in_channels[1];
00171
00172 av_free(args);
00173 return 0;
00174 }
00175
00176 static int query_formats(AVFilterContext *ctx)
00177 {
00178 PanContext *pan = ctx->priv;
00179 AVFilterLink *inlink = ctx->inputs[0];
00180 AVFilterLink *outlink = ctx->outputs[0];
00181 AVFilterFormats *formats;
00182
00183 const enum AVSampleFormat sample_fmts[] = {AV_SAMPLE_FMT_S16, -1};
00184 const int packing_fmts[] = {AVFILTER_PACKED, -1};
00185
00186 avfilter_set_common_sample_formats (ctx, avfilter_make_format_list(sample_fmts));
00187 avfilter_set_common_packing_formats(ctx, avfilter_make_format_list(packing_fmts));
00188
00189
00190 formats = avfilter_make_all_channel_layouts();
00191 avfilter_formats_ref(formats, &inlink->out_chlayouts);
00192
00193
00194 formats = NULL;
00195 avfilter_add_format(&formats, pan->out_channel_layout);
00196 avfilter_formats_ref(formats, &outlink->in_chlayouts);
00197 return 0;
00198 }
00199
00200 static int config_props(AVFilterLink *link)
00201 {
00202 AVFilterContext *ctx = link->dst;
00203 PanContext *pan = ctx->priv;
00204 char buf[1024], *cur;
00205 int i, j, k, r;
00206 double t;
00207
00208 pan->nb_input_channels = av_get_channel_layout_nb_channels(link->channel_layout);
00209 if (pan->need_renumber) {
00210
00211 for (i = j = 0; i < MAX_CHANNELS; i++) {
00212 if ((link->channel_layout >> i) & 1) {
00213 for (k = 0; k < pan->nb_output_channels; k++)
00214 pan->gain.d[k][j] = pan->gain.d[k][i];
00215 j++;
00216 }
00217 }
00218 }
00219
00220 for (i = 0; i < pan->nb_output_channels; i++) {
00221 if (!((pan->need_renorm >> i) & 1))
00222 continue;
00223 t = 0;
00224 for (j = 0; j < pan->nb_input_channels; j++)
00225 t += pan->gain.d[i][j];
00226 if (t > -1E-5 && t < 1E-5) {
00227
00228 if (t)
00229 av_log(ctx, AV_LOG_WARNING,
00230 "Degenerate coefficients while renormalizing\n");
00231 continue;
00232 }
00233 for (j = 0; j < pan->nb_input_channels; j++)
00234 pan->gain.d[i][j] /= t;
00235 }
00236
00237 for (i = 0; i < pan->nb_output_channels; i++) {
00238 cur = buf;
00239 for (j = 0; j < pan->nb_input_channels; j++) {
00240 r = snprintf(cur, buf + sizeof(buf) - cur, "%s%.3g i%d",
00241 j ? " + " : "", pan->gain.d[i][j], j);
00242 cur += FFMIN(buf + sizeof(buf) - cur, r);
00243 }
00244 av_log(ctx, AV_LOG_INFO, "o%d = %s\n", i, buf);
00245 }
00246
00247 for (i = 0; i < pan->nb_output_channels; i++) {
00248 for (j = 0; j < pan->nb_input_channels; j++) {
00249 if (pan->gain.d[i][j] < -128 || pan->gain.d[i][j] > 128)
00250 av_log(ctx, AV_LOG_WARNING,
00251 "Gain #%d->#%d too large, clamped\n", j, i);
00252 pan->gain.i[i][j] = av_clipf(pan->gain.d[i][j], -128, 128) * 256.0;
00253 }
00254 }
00255 return 0;
00256 }
00257
00258
00259 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00260 {
00261 PanContext *const pan = inlink->dst->priv;
00262 int i, o, n = insamples->audio->nb_samples;
00263
00264
00265 const int16_t *in = (int16_t *)insamples->data[0];
00266 const int16_t *in_end = in + n * pan->nb_input_channels;
00267
00268
00269 AVFilterLink *const outlink = inlink->dst->outputs[0];
00270 AVFilterBufferRef *outsamples = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n);
00271 int16_t *out = (int16_t *)outsamples->data[0];
00272
00273 for (; in < in_end; in += pan->nb_input_channels) {
00274 for (o = 0; o < pan->nb_output_channels; o++) {
00275 int v = 0;
00276 for (i = 0; i < pan->nb_input_channels; i++)
00277 v += pan->gain.i[o][i] * in[i];
00278 *(out++) = v >> 8;
00279 }
00280 }
00281
00282 avfilter_filter_samples(outlink, outsamples);
00283 avfilter_unref_buffer(insamples);
00284 }
00285
00286 AVFilter avfilter_af_pan = {
00287 .name = "pan",
00288 .description = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)"),
00289 .priv_size = sizeof(PanContext),
00290 .init = init,
00291 .query_formats = query_formats,
00292
00293 .inputs = (const AVFilterPad[]) {
00294 { .name = "default",
00295 .type = AVMEDIA_TYPE_AUDIO,
00296 .config_props = config_props,
00297 .filter_samples = filter_samples,
00298 .min_perms = AV_PERM_READ, },
00299 { .name = NULL}
00300 },
00301 .outputs = (const AVFilterPad[]) {
00302 { .name = "default",
00303 .type = AVMEDIA_TYPE_AUDIO, },
00304 { .name = NULL}
00305 },
00306 };