libavfilter/af_earwax.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Mina Nagy Zaki
00003  * Copyright (c) 2000 Edward Beingessner And Sundry Contributors.
00004  * This source code is freely redistributable and may be used for any purpose.
00005  * This copyright notice must be maintained.  Edward Beingessner And Sundry
00006  * Contributors are not responsible for the consequences of using this
00007  * software.
00008  *
00009  * This file is part of FFmpeg.
00010  *
00011  * FFmpeg is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * FFmpeg is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with FFmpeg; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00024  */
00025 
00032 #include "libavutil/audioconvert.h"
00033 #include "avfilter.h"
00034 
00035 #define NUMTAPS 64
00036 
00037 static const int8_t filt[NUMTAPS] = {
00038 /* 30°  330° */
00039     4,   -6,     /* 32 tap stereo FIR filter. */
00040     4,  -11,     /* One side filters as if the */
00041    -1,   -5,     /* signal was from 30 degrees */
00042     3,    3,     /* from the ear, the other as */
00043    -2,    5,     /* if 330 degrees. */
00044    -5,    0,
00045     9,    1,
00046     6,    3,     /*                         Input                         */
00047    -4,   -1,     /*                   Left         Right                  */
00048    -5,   -3,     /*                __________   __________                */
00049    -2,   -5,     /*               |          | |          |               */
00050    -7,    1,     /*           .---|  Hh,0(f) | |  Hh,0(f) |---.           */
00051     6,   -7,     /*          /    |__________| |__________|    \          */
00052    30,  -29,     /*         /                \ /                \         */
00053    12,   -3,     /*        /                  X                  \        */
00054   -11,    4,     /*       /                  / \                  \       */
00055    -3,    7,     /*  ____V_____   __________V   V__________   _____V____  */
00056   -20,   23,     /* |          | |          |   |          | |          | */
00057     2,    0,     /* | Hh,30(f) | | Hh,330(f)|   | Hh,330(f)| | Hh,30(f) | */
00058     1,   -6,     /* |__________| |__________|   |__________| |__________| */
00059   -14,   -5,     /*      \     ___      /           \      ___     /      */
00060    15,  -18,     /*       \   /   \    /    _____    \    /   \   /       */
00061     6,    7,     /*        `->| + |<--'    /     \    `-->| + |<-'        */
00062    15,  -10,     /*           \___/      _/       \_      \___/           */
00063   -14,   22,     /*               \     / \       / \     /               */
00064    -7,   -2,     /*                `--->| |       | |<---'                */
00065    -4,    9,     /*                     \_/       \_/                     */
00066     6,  -12,     /*                                                       */
00067     6,   -6,     /*                       Headphones                      */
00068     0,  -11,
00069     0,   -5,
00070     4,    0};
00071 
00072 typedef struct {
00073     int16_t taps[NUMTAPS * 2];
00074 } EarwaxContext;
00075 
00076 static int query_formats(AVFilterContext *ctx)
00077 {
00078     AVFilterFormats *formats = NULL;
00079     avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
00080     avfilter_set_common_sample_formats(ctx, formats);
00081     formats = NULL;
00082     avfilter_add_format(&formats, AV_CH_LAYOUT_STEREO);
00083     avfilter_set_common_channel_layouts(ctx, formats);
00084     formats = NULL;
00085     avfilter_add_format(&formats, AVFILTER_PACKED);
00086     avfilter_set_common_packing_formats(ctx, formats);
00087 
00088     return 0;
00089 }
00090 
00091 static int config_input(AVFilterLink *inlink)
00092 {
00093     if (inlink->sample_rate != 44100) {
00094         av_log(inlink->dst, AV_LOG_ERROR,
00095                "The earwax filter only works for 44.1kHz audio. Insert "
00096                "a resample filter before this\n");
00097         return AVERROR(EINVAL);
00098     }
00099     return 0;
00100 }
00101 
00102 //FIXME: replace with DSPContext.scalarproduct_int16
00103 static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out)
00104 {
00105     int32_t sample;
00106     int16_t j;
00107 
00108     while (in < endin) {
00109         sample = 32;
00110         for (j = 0; j < NUMTAPS; j++)
00111             sample += in[j] * filt[j];
00112         *out = sample >> 6;
00113         out++;
00114         in++;
00115     }
00116 
00117     return out;
00118 }
00119 
00120 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
00121 {
00122     AVFilterLink *outlink = inlink->dst->outputs[0];
00123     int16_t *taps, *endin, *in, *out;
00124     AVFilterBufferRef *outsamples =
00125         avfilter_get_audio_buffer(inlink, AV_PERM_WRITE,
00126                                   insamples->audio->nb_samples);
00127     avfilter_copy_buffer_ref_props(outsamples, insamples);
00128 
00129     taps  = ((EarwaxContext *)inlink->dst->priv)->taps;
00130     out   = (int16_t *)outsamples->data[0];
00131     in    = (int16_t *)insamples ->data[0];
00132 
00133     // copy part of new input and process with saved input
00134     memcpy(taps+NUMTAPS, in, NUMTAPS * sizeof(*taps));
00135     out   = scalarproduct(taps, taps + NUMTAPS, out);
00136 
00137     // process current input
00138     endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
00139     out   = scalarproduct(in, endin, out);
00140 
00141     // save part of input for next round
00142     memcpy(taps, endin, NUMTAPS * sizeof(*taps));
00143 
00144     avfilter_filter_samples(outlink, outsamples);
00145     avfilter_unref_buffer(insamples);
00146 }
00147 
00148 AVFilter avfilter_af_earwax = {
00149     .name           = "earwax",
00150     .description    = NULL_IF_CONFIG_SMALL("Widen the stereo image."),
00151     .query_formats  = query_formats,
00152     .priv_size      = sizeof(EarwaxContext),
00153     .inputs  = (const AVFilterPad[])  {{  .name     = "default",
00154                                     .type           = AVMEDIA_TYPE_AUDIO,
00155                                     .filter_samples = filter_samples,
00156                                     .config_props   = config_input,
00157                                     .min_perms      = AV_PERM_READ, },
00158                                  {  .name = NULL}},
00159 
00160     .outputs = (const AVFilterPad[])  {{  .name     = "default",
00161                                     .type           = AVMEDIA_TYPE_AUDIO, },
00162                                  {  .name = NULL}},
00163 };