| @@ -34,9 +34,9 @@ · 33 unmodified lines |
| 34 | 34 | #include "audio.h" |
| 35 | 35 | #include "formats.h" |
| 36 | 36 | |
| 37 | | −#define NUMTAPS 64 |
| 37 | +#define NUMTAPS 32 |
| 38 | 38 | |
| 39 | | −static const int8_t filt[NUMTAPS] = { |
| 39 | +static const int8_t filt[NUMTAPS * 2] = { |
| 40 | 40 | /* 30° 330° */ |
| 41 | 41 | 4, -6, /* 32 tap stereo FIR filter. */ |
| 42 | 42 | 4, -11, /* One side filters as if the */ |
| @@ -72,7 +72,10 @@ static const int8_t filt[NUMTAPS] = { · 29 unmodified lines |
| 72 | 72 | 4, 0}; |
| 73 | 73 | |
| 74 | 74 | typedef struct EarwaxContext { |
| 75 | | − int16_t taps[NUMTAPS * 2]; |
| 75 | + int16_t filter[2][NUMTAPS]; |
| 76 | + int16_t taps[4][NUMTAPS * 2]; |
| 77 | + |
| 78 | + AVFrame *frame[2]; |
| 76 | 79 | } EarwaxContext; |
| 77 | 80 | |
| 78 | 81 | static int query_formats(AVFilterContext *ctx) |
| @@ -83,7 +86,7 @@ static int query_formats(AVFilterContext *ctx) · 4 unmodified lines |
| 83 | 86 | AVFilterFormats *formats = NULL; |
| 84 | 87 | AVFilterChannelLayouts *layout = NULL; |
| 85 | 88 | |
| 86 | | − if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16 )) < 0 || |
| 89 | + if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_S16P )) < 0 || |
| 87 | 90 | (ret = ff_set_common_formats (ctx , formats )) < 0 || |
| 88 | 91 | (ret = ff_add_channel_layout (&layout , AV_CH_LAYOUT_STEREO )) < 0 || |
| 89 | 92 | (ret = ff_set_common_channel_layouts (ctx , layout )) < 0 || |
| @@ -94,7 +97,8 @@ static int query_formats(AVFilterContext *ctx) · 4 unmodified lines |
| 94 | 97 | } |
| 95 | 98 | |
| 96 | 99 | //FIXME: replace with DSPContext.scalarproduct_int16 |
| 97 | | −static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, int16_t *out) |
| 100 | +static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, |
| 101 | + const int16_t *filt, int16_t *out) |
| 98 | 102 | { |
| 99 | 103 | int32_t sample; |
| 100 | 104 | int16_t j; |
| @@ -103,7 +107,7 @@ static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, in · 2 unmodified lines |
| 103 | 107 | sample = 0; |
| 104 | 108 | for (j = 0; j < NUMTAPS; j++) |
| 105 | 109 | sample += in[j] * filt[j]; |
| 106 | | − *out = av_clip_int16(sample >> 6); |
| 110 | + *out = av_clip_int16(sample >> 7); |
| 107 | 111 | out++; |
| 108 | 112 | in++; |
| 109 | 113 | } |
| @@ -111,40 +115,102 @@ static inline int16_t *scalarproduct(const int16_t *in, const int16_t *endin, in · 1 unmodified line |
| 111 | 115 | return out; |
| 112 | 116 | } |
| 113 | 117 | |
| 114 | | −static int filter_frame(AVFilterLink *inlink, AVFrame *insamples) |
| 118 | +static int config_input(AVFilterLink *inlink) |
| 115 | 119 | { |
| 116 | | − AVFilterLink *outlink = inlink->dst->outputs[0]; |
| 117 | | − int16_t *taps, *endin, *in, *out; |
| 118 | | − AVFrame *outsamples = ff_get_audio_buffer(outlink, insamples->nb_samples); |
| 119 | | − int len; |
| 120 | + EarwaxContext *s = inlink->dst->priv; |
| 120 | 121 | |
| 121 | | − if (!outsamples) { |
| 122 | | − av_frame_free(&insamples); |
| 123 | | − return AVERROR(ENOMEM); |
| 122 | + for (int i = 0; i < NUMTAPS; i++) { |
| 123 | + s->filter[0][i] = filt[i * 2]; |
| 124 | + s->filter[1][i] = filt[i * 2 + 1]; |
| 124 | 125 | } |
| 125 | | − av_frame_copy_props(outsamples, insamples); |
| 126 | 126 | |
| 127 | | − taps = ((EarwaxContext *)inlink->dst->priv)->taps; |
| 128 | | − out = (int16_t *)outsamples->data[0]; |
| 129 | | − in = (int16_t *)insamples ->data[0]; |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static void convolve(AVFilterContext *ctx, AVFrame *in, |
| 131 | + int input_ch, int output_ch, |
| 132 | + int filter_ch, int tap_ch) |
| 133 | +{ |
| 134 | + EarwaxContext *s = ctx->priv; |
| 135 | + int16_t *taps, *endin, *dst, *src; |
| 136 | + int len; |
| 137 | + |
| 138 | + taps = s->taps[tap_ch]; |
| 139 | + dst = (int16_t *)s->frame[input_ch]->data[output_ch]; |
| 140 | + src = (int16_t *)in->data[input_ch]; |
| 130 | 141 | |
| 131 | | − len = FFMIN(NUMTAPS, 2*insamples->nb_samples); |
| 142 | + len = FFMIN(NUMTAPS, in->nb_samples); |
| 132 | 143 | // copy part of new input and process with saved input |
| 133 | | − memcpy(taps+NUMTAPS, in, len * sizeof(*taps)); |
| 134 | | − out = scalarproduct(taps, taps + len, out); |
| 144 | + memcpy(taps+NUMTAPS, src, len * sizeof(*taps)); |
| 145 | + dst = scalarproduct(taps, taps + len, s->filter[filter_ch], dst); |
| 135 | 146 | |
| 136 | 147 | // process current input |
| 137 | | − if (2*insamples->nb_samples >= NUMTAPS ){ |
| 138 | | − endin = in + insamples->nb_samples * 2 - NUMTAPS; |
| 139 | | − scalarproduct(in, endin, out); |
| 148 | + if (2*in->nb_samples >= NUMTAPS ){ |
| 149 | + endin = src + in->nb_samples - NUMTAPS; |
| 150 | + scalarproduct(src, endin, s->filter[filter_ch], dst); |
| 140 | 151 | |
| 141 | 152 | // save part of input for next round |
| 142 | 153 | memcpy(taps, endin, NUMTAPS * sizeof(*taps)); |
| 143 | | − } else |
| 144 | | − memmove(taps, taps + 2*insamples->nb_samples, NUMTAPS * sizeof(*taps)); |
| 154 | + } else { |
| 155 | + memmove(taps, taps + in->nb_samples, NUMTAPS * sizeof(*taps)); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +static void mix(AVFilterContext *ctx, AVFrame *out, |
| 160 | + int output_ch, int f0, int f1, int i0, int i1) |
| 161 | +{ |
| 162 | + EarwaxContext *s = ctx->priv; |
| 163 | + const int16_t *srcl = (const int16_t *)s->frame[f0]->data[i0]; |
| 164 | + const int16_t *srcr = (const int16_t *)s->frame[f1]->data[i1]; |
| 165 | + int16_t *dst = (int16_t *)out->data[output_ch]; |
| 166 | + |
| 167 | + for (int n = 0; n < out->nb_samples; n++) |
| 168 | + dst[n] = av_clip_int16(srcl[n] + srcr[n]); |
| 169 | +} |
| 170 | + |
| 171 | +static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
| 172 | +{ |
| 173 | + AVFilterContext *ctx = inlink->dst; |
| 174 | + EarwaxContext *s = ctx->priv; |
| 175 | + AVFilterLink *outlink = ctx->outputs[0]; |
| 176 | + AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples); |
| 177 | + |
| 178 | + for (int ch = 0; ch < 2; ch++) { |
| 179 | + if (!s->frame[ch] || s->frame[ch]->nb_samples < in->nb_samples) { |
| 180 | + av_frame_free(&s->frame[ch]); |
| 181 | + s->frame[ch] = ff_get_audio_buffer(outlink, in->nb_samples); |
| 182 | + if (!s->frame[ch]) { |
| 183 | + av_frame_free(&in); |
| 184 | + av_frame_free(&out); |
| 185 | + return AVERROR(ENOMEM); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (!out) { |
| 191 | + av_frame_free(&in); |
| 192 | + return AVERROR(ENOMEM); |
| 193 | + } |
| 194 | + av_frame_copy_props(out, in); |
| 195 | + |
| 196 | + convolve(ctx, in, 0, 0, 0, 0); |
| 197 | + convolve(ctx, in, 0, 1, 1, 1); |
| 198 | + convolve(ctx, in, 1, 0, 0, 2); |
| 199 | + convolve(ctx, in, 1, 1, 1, 3); |
| 200 | + |
| 201 | + mix(ctx, out, 0, 0, 1, 1, 0); |
| 202 | + mix(ctx, out, 1, 0, 1, 0, 1); |
| 203 | + |
| 204 | + av_frame_free(&in); |
| 205 | + return ff_filter_frame(outlink, out); |
| 206 | +} |
| 207 | + |
| 208 | +static av_cold void uninit(AVFilterContext *ctx) |
| 209 | +{ |
| 210 | + EarwaxContext *s = ctx->priv; |
| 145 | 211 | |
| 146 | | − av_frame_free(&insamples); |
| 147 | | − return ff_filter_frame(outlink, outsamples); |
| 212 | + av_frame_free(&s->frame[0]); |
| 213 | + av_frame_free(&s->frame[1]); |
| 148 | 214 | } |
| 149 | 215 | |
| 150 | 216 | static const AVFilterPad earwax_inputs[] = { |
| @@ -152,6 +218,7 @@ static const AVFilterPad earwax_inputs[] = { · 1 unmodified line |
| 152 | 218 | .name = "default", |
| 153 | 219 | .type = AVMEDIA_TYPE_AUDIO, |
| 154 | 220 | .filter_frame = filter_frame, |
| 221 | + .config_props = config_input, |
| 155 | 222 | }, |
| 156 | 223 | { NULL } |
| 157 | 224 | }; |
| @@ -169,6 +236,7 @@ AVFilter ff_af_earwax = { · 11 unmodified lines |
| 169 | 236 | .description = NULL_IF_CONFIG_SMALL("Widen the stereo image."), |
| 170 | 237 | .query_formats = query_formats, |
| 171 | 238 | .priv_size = sizeof(EarwaxContext), |
| 239 | + .uninit = uninit, |
| 172 | 240 | .inputs = earwax_inputs, |
| 173 | 241 | .outputs = earwax_outputs, |
| 174 | 242 | }; |