Showing the changes to libavfilter (1 of 2 files). Show all
Unified · Split
| @@ -49,6 +49,7 @@ typedef struct IIRChannel { · 48 unmodified lines | |||
| 49 | double *ab[2]; | 49 | double *ab[2]; |
| 50 | double g; | 50 | double g; |
| 51 | double *cache[2]; | 51 | double *cache[2]; |
| 52 | double fir; | ||
| 52 | BiquadContext *biquads; | 53 | BiquadContext *biquads; |
| 53 | int clippings; | 54 | int clippings; |
| 54 | } IIRChannel; | 55 | } IIRChannel; |
| @@ -183,6 +184,7 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb · 128 unmodified lines | |||
| 183 | const double ig = s->dry_gain; \ | 184 | const double ig = s->dry_gain; \ |
| 184 | const double og = s->wet_gain; \ | 185 | const double og = s->wet_gain; \ |
| 185 | const double mix = s->mix; \ | 186 | const double mix = s->mix; \ |
| 187 | const double imix = 1. - mix; \ | ||
| 186 | ThreadData *td = arg; \ | 188 | ThreadData *td = arg; \ |
| 187 | AVFrame *in = td->in, *out = td->out; \ | 189 | AVFrame *in = td->in, *out = td->out; \ |
| 188 | const type *src = (const type *)in->extended_data[ch]; \ | 190 | const type *src = (const type *)in->extended_data[ch]; \ |
| @@ -205,16 +207,16 @@ static int iir_ch_serial_## name(AVFilterContext *ctx, void *arg, int ch, int nb · 16 unmodified lines | |||
| 205 | double o2 = iir->biquads[i].o2; \ | 207 | double o2 = iir->biquads[i].o2; \ |
| 206 | \ | 208 | \ |
| 207 | for (n = 0; n < in->nb_samples; n++) { \ | 209 | for (n = 0; n < in->nb_samples; n++) { \ |
| 208 | double sample = ig * (i ? dst[n] : src[n]); \ | 210 | double i0 = ig * (i ? dst[n] : src[n]); \ |
| 209 | double o0 = sample * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \ | 211 | double o0 = i0 * b0 + i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \ |
| 210 | \ | 212 | \ |
| 211 | i2 = i1; \ | 213 | i2 = i1; \ |
| 212 | i1 = src[n]; \ | 214 | i1 = i0; \ |
| 213 | o2 = o1; \ | 215 | o2 = o1; \ |
| 214 | o1 = o0; \ | 216 | o1 = o0; \ |
| 215 | o0 *= og * g; \ | 217 | o0 *= og * g; \ |
| 216 | \ | 218 | \ |
| 217 | o0 = o0 * mix + (1. - mix) * sample; \ | 219 | o0 = o0 * mix + imix * i0; \ |
| 218 | if (need_clipping && o0 < min) { \ | 220 | if (need_clipping && o0 < min) { \ |
| 219 | (*clippings)++; \ | 221 | (*clippings)++; \ |
| 220 | dst[n] = min; \ | 222 | dst[n] = min; \ |
| @@ -239,6 +241,76 @@ SERIAL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1) · 18 unmodified lines | |||
| 239 | SERIAL_IIR_CH(fltp, float, -1., 1., 0) | 241 | SERIAL_IIR_CH(fltp, float, -1., 1., 0) |
| 240 | SERIAL_IIR_CH(dblp, double, -1., 1., 0) | 242 | SERIAL_IIR_CH(dblp, double, -1., 1., 0) |
| 241 | 243 | ||
| 244 | #define PARALLEL_IIR_CH(name, type, min, max, need_clipping) \ | ||
| 245 | static int iir_ch_parallel_## name(AVFilterContext *ctx, void *arg, \ | ||
| 246 | int ch, int nb_jobs) \ | ||
| 247 | { \ | ||
| 248 | AudioIIRContext *s = ctx->priv; \ | ||
| 249 | const double ig = s->dry_gain; \ | ||
| 250 | const double og = s->wet_gain; \ | ||
| 251 | const double mix = s->mix; \ | ||
| 252 | const double imix = 1. - mix; \ | ||
| 253 | ThreadData *td = arg; \ | ||
| 254 | AVFrame *in = td->in, *out = td->out; \ | ||
| 255 | const type *src = (const type *)in->extended_data[ch]; \ | ||
| 256 | type *dst = (type *)out->extended_data[ch]; \ | ||
| 257 | IIRChannel *iir = &s->iir[ch]; \ | ||
| 258 | const double g = iir->g; \ | ||
| 259 | const double fir = iir->fir; \ | ||
| 260 | int *clippings = &iir->clippings; \ | ||
| 261 | int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; \ | ||
| 262 | int n, i; \ | ||
| 263 | \ | ||
| 264 | for (i = 0; i < nb_biquads; i++) { \ | ||
| 265 | const double a1 = -iir->biquads[i].a[1]; \ | ||
| 266 | const double a2 = -iir->biquads[i].a[2]; \ | ||
| 267 | const double b1 = iir->biquads[i].b[1]; \ | ||
| 268 | const double b2 = iir->biquads[i].b[2]; \ | ||
| 269 | double i1 = iir->biquads[i].i1; \ | ||
| 270 | double i2 = iir->biquads[i].i2; \ | ||
| 271 | double o1 = iir->biquads[i].o1; \ | ||
| 272 | double o2 = iir->biquads[i].o2; \ | ||
| 273 | \ | ||
| 274 | for (n = 0; n < in->nb_samples; n++) { \ | ||
| 275 | double i0 = ig * src[n]; \ | ||
| 276 | double o0 = i1 * b1 + i2 * b2 + o1 * a1 + o2 * a2; \ | ||
| 277 | \ | ||
| 278 | i2 = i1; \ | ||
| 279 | i1 = i0; \ | ||
| 280 | o2 = o1; \ | ||
| 281 | o1 = o0; \ | ||
| 282 | o0 *= og * g; \ | ||
| 283 | o0 += dst[n]; \ | ||
| 284 | \ | ||
| 285 | if (need_clipping && o0 < min) { \ | ||
| 286 | (*clippings)++; \ | ||
| 287 | dst[n] = min; \ | ||
| 288 | } else if (need_clipping && o0 > max) { \ | ||
| 289 | (*clippings)++; \ | ||
| 290 | dst[n] = max; \ | ||
| 291 | } else { \ | ||
| 292 | dst[n] = o0; \ | ||
| 293 | } \ | ||
| 294 | } \ | ||
| 295 | iir->biquads[i].i1 = i1; \ | ||
| 296 | iir->biquads[i].i2 = i2; \ | ||
| 297 | iir->biquads[i].o1 = o1; \ | ||
| 298 | iir->biquads[i].o2 = o2; \ | ||
| 299 | } \ | ||
| 300 | \ | ||
| 301 | for (n = 0; n < in->nb_samples; n++) { \ | ||
| 302 | dst[n] += fir * src[n]; \ | ||
| 303 | dst[n] = dst[n] * mix + imix * src[n]; \ | ||
| 304 | } \ | ||
| 305 | \ | ||
| 306 | return 0; \ | ||
| 307 | } | ||
| 308 | |||
| 309 | PARALLEL_IIR_CH(s16p, int16_t, INT16_MIN, INT16_MAX, 1) | ||
| 310 | PARALLEL_IIR_CH(s32p, int32_t, INT32_MIN, INT32_MAX, 1) | ||
| 311 | PARALLEL_IIR_CH(fltp, float, -1., 1., 0) | ||
| 312 | PARALLEL_IIR_CH(dblp, double, -1., 1., 0) | ||
| 313 | |||
| 242 | static void count_coefficients(char *item_str, int *nb_items) | 314 | static void count_coefficients(char *item_str, int *nb_items) |
| 243 | { | 315 | { |
| 244 | char *p; | 316 | char *p; |
| @@ -656,6 +728,128 @@ static int decompose_zp2biquads(AVFilterContext *ctx, int channels) · 411 unmodified lines | |||
| 656 | return 0; | 728 | return 0; |
| 657 | } | 729 | } |
| 658 | 730 | ||
| 731 | static void biquad_process(double *x, double *y, int length, | ||
| 732 | double b0, double b1, double b2, | ||
| 733 | double a1, double a2) | ||
| 734 | { | ||
| 735 | double w1 = 0., w2 = 0.; | ||
| 736 | |||
| 737 | a1 = -a1; | ||
| 738 | a2 = -a2; | ||
| 739 | |||
| 740 | for (int n = 0; n < length; n++) { | ||
| 741 | double out, in = x[n]; | ||
| 742 | |||
| 743 | y[n] = out = in * b0 + w1; | ||
| 744 | w1 = b1 * in + w2 + a1 * out; | ||
| 745 | w2 = b2 * in + a2 * out; | ||
| 746 | } | ||
| 747 | } | ||
| 748 | |||
| 749 | static void solve(double *matrix, double *vector, int n, double *y, double *x, double *lu) | ||
| 750 | { | ||
| 751 | double sum = 0.; | ||
| 752 | |||
| 753 | for (int i = 0; i < n; i++) { | ||
| 754 | for (int j = i; j < n; j++) { | ||
| 755 | sum = 0.; | ||
| 756 | for (int k = 0; k < i; k++) | ||
| 757 | sum += lu[i * n + k] * lu[k * n + j]; | ||
| 758 | lu[i * n + j] = matrix[j * n + i] - sum; | ||
| 759 | } | ||
| 760 | for (int j = i + 1; j < n; j++) { | ||
| 761 | sum = 0.; | ||
| 762 | for (int k = 0; k < i; k++) | ||
| 763 | sum += lu[j * n + k] * lu[k * n + i]; | ||
| 764 | lu[j * n + i] = (1. / lu[i * n + i]) * (matrix[i * n + j] - sum); | ||
| 765 | } | ||
| 766 | } | ||
| 767 | |||
| 768 | for (int i = 0; i < n; i++) { | ||
| 769 | sum = 0.; | ||
| 770 | for (int k = 0; k < i; k++) | ||
| 771 | sum += lu[i * n + k] * y[k]; | ||
| 772 | y[i] = vector[i] - sum; | ||
| 773 | } | ||
| 774 | |||
| 775 | for (int i = n - 1; i >= 0; i--) { | ||
| 776 | sum = 0.; | ||
| 777 | for (int k = i + 1; k < n; k++) | ||
| 778 | sum += lu[i * n + k] * x[k]; | ||
| 779 | x[i] = (1 / lu[i * n + i]) * (y[i] - sum); | ||
| 780 | } | ||
| 781 | } | ||
| 782 | |||
| 783 | static int convert_serial2parallel(AVFilterContext *ctx, int channels) | ||
| 784 | { | ||
| 785 | AudioIIRContext *s = ctx->priv; | ||
| 786 | int ret = 0; | ||
| 787 | |||
| 788 | for (int ch = 0; ch < channels; ch++) { | ||
| 789 | IIRChannel *iir = &s->iir[ch]; | ||
| 790 | int nb_biquads = (FFMAX(iir->nb_ab[0], iir->nb_ab[1]) + 1) / 2; | ||
| 791 | int length = nb_biquads * 2 + 1; | ||
| 792 | double *impulse = av_calloc(length, sizeof(*impulse)); | ||
| 793 | double *y = av_calloc(length, sizeof(*y)); | ||
| 794 | double *resp = av_calloc(length, sizeof(*resp)); | ||
| 795 | double *M = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*M)); | ||
| 796 | double *W = av_calloc((length - 1) * 2 * nb_biquads, sizeof(*W)); | ||
| 797 | |||
| 798 | if (!impulse || !y || !resp || !M) { | ||
| 799 | av_free(impulse); | ||
| 800 | av_free(y); | ||
| 801 | av_free(resp); | ||
| 802 | av_free(M); | ||
| 803 | av_free(W); | ||
| 804 | return AVERROR(ENOMEM); | ||
| 805 | } | ||
| 806 | |||
| 807 | impulse[0] = 1.; | ||
| 808 | |||
| 809 | for (int n = 0; n < nb_biquads; n++) { | ||
| 810 | BiquadContext *biquad = &iir->biquads[n]; | ||
| 811 | |||
| 812 | biquad_process(n ? y : impulse, y, length, | ||
| 813 | biquad->b[0], biquad->b[1], biquad->b[2], | ||
| 814 | biquad->a[1], biquad->a[2]); | ||
| 815 | } | ||
| 816 | |||
| 817 | for (int n = 0; n < nb_biquads; n++) { | ||
| 818 | BiquadContext *biquad = &iir->biquads[n]; | ||
| 819 | |||
| 820 | biquad_process(impulse, resp, length - 1, | ||
| 821 | 1., 0., 0., biquad->a[1], biquad->a[2]); | ||
| 822 | |||
| 823 | memcpy(M + n * 2 * (length - 1), resp, sizeof(*resp) * (length - 1)); | ||
| 824 | memcpy(M + n * 2 * (length - 1) + length, resp, sizeof(*resp) * (length - 2)); | ||
| 825 | memset(resp, 0, length * sizeof(*resp)); | ||
| 826 | } | ||
| 827 | |||
| 828 | solve(M, &y[1], length - 1, &impulse[1], resp, W); | ||
| 829 | |||
| 830 | iir->fir = y[0]; | ||
| 831 | |||
| 832 | for (int n = 0; n < nb_biquads; n++) { | ||
| 833 | BiquadContext *biquad = &iir->biquads[n]; | ||
| 834 | |||
| 835 | biquad->b[0] = 0.; | ||
| 836 | biquad->b[1] = resp[n * 2 + 0]; | ||
| 837 | biquad->b[2] = resp[n * 2 + 1]; | ||
| 838 | } | ||
| 839 | |||
| 840 | av_free(impulse); | ||
| 841 | av_free(y); | ||
| 842 | av_free(resp); | ||
| 843 | av_free(M); | ||
| 844 | av_free(W); | ||
| 845 | |||
| 846 | if (ret < 0) | ||
| 847 | return ret; | ||
| 848 | } | ||
| 849 | |||
| 850 | return 0; | ||
| 851 | } | ||
| 852 | |||
| 659 | static void convert_pr2zp(AVFilterContext *ctx, int channels) | 853 | static void convert_pr2zp(AVFilterContext *ctx, int channels) |
| 660 | { | 854 | { |
| 661 | AudioIIRContext *s = ctx->priv; | 855 | AudioIIRContext *s = ctx->priv; |
| @@ -1034,15 +1228,22 @@ static int config_output(AVFilterLink *outlink) · 372 unmodified lines | |||
| 1034 | if (ret < 0) | 1228 | if (ret < 0) |
| 1035 | return ret; | 1229 | return ret; |
| 1036 | } else if (s->format == 0 && s->process == 1) { | 1230 | } else if (s->format == 0 && s->process == 1) { |
| 1037 | av_log(ctx, AV_LOG_ERROR, "Serial cascading is not implemented for transfer function.\n"); | 1231 | av_log(ctx, AV_LOG_ERROR, "Serial processing is not implemented for transfer function.\n"); |
| 1232 | return AVERROR_PATCHWELCOME; | ||
| 1233 | } else if (s->format == 0 && s->process == 2) { | ||
| 1234 | av_log(ctx, AV_LOG_ERROR, "Parallel processing is not implemented for transfer function.\n"); | ||
| 1038 | return AVERROR_PATCHWELCOME; | 1235 | return AVERROR_PATCHWELCOME; |
| 1039 | } else if (s->format > 0 && s->process == 1) { | 1236 | } else if (s->format > 0 && s->process == 1) { |
| 1040 | if (inlink->format == AV_SAMPLE_FMT_S16P) | ||
| 1041 | av_log(ctx, AV_LOG_WARNING, "Serial cascading is not recommended for i16 precision.\n"); | ||
| 1042 | |||
| 1043 | ret = decompose_zp2biquads(ctx, inlink->channels); | 1237 | ret = decompose_zp2biquads(ctx, inlink->channels); |
| 1044 | if (ret < 0) | 1238 | if (ret < 0) |
| 1045 | return ret; | 1239 | return ret; |
| 1240 | } else if (s->format > 0 && s->process == 2) { | ||
| 1241 | ret = decompose_zp2biquads(ctx, inlink->channels); | ||
| 1242 | if (ret < 0) | ||
| 1243 | return ret; | ||
| 1244 | ret = convert_serial2parallel(ctx, inlink->channels); | ||
| 1245 | if (ret < 0) | ||
| 1246 | return ret; | ||
| 1046 | } | 1247 | } |
| 1047 | 1248 | ||
| 1048 | for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) { | 1249 | for (ch = 0; s->format == 0 && ch < inlink->channels; ch++) { |
| @@ -1061,10 +1262,10 @@ static int config_output(AVFilterLink *outlink) · 12 unmodified lines | |||
| 1061 | } | 1262 | } |
| 1062 | 1263 | ||
| 1063 | switch (inlink->format) { | 1264 | switch (inlink->format) { |
| 1064 | case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break; | 1265 | case AV_SAMPLE_FMT_DBLP: s->iir_channel = s->process == 2 ? iir_ch_parallel_dblp : s->process == 1 ? iir_ch_serial_dblp : iir_ch_dblp; break; |
| 1065 | case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break; | 1266 | case AV_SAMPLE_FMT_FLTP: s->iir_channel = s->process == 2 ? iir_ch_parallel_fltp : s->process == 1 ? iir_ch_serial_fltp : iir_ch_fltp; break; |
| 1066 | case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break; | 1267 | case AV_SAMPLE_FMT_S32P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s32p : s->process == 1 ? iir_ch_serial_s32p : iir_ch_s32p; break; |
| 1067 | case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break; | 1268 | case AV_SAMPLE_FMT_S16P: s->iir_channel = s->process == 2 ? iir_ch_parallel_s16p : s->process == 1 ? iir_ch_serial_s16p : iir_ch_s16p; break; |
| 1068 | } | 1269 | } |
| 1069 | 1270 | ||
| 1070 | return 0; | 1271 | return 0; |
| @@ -1079,7 +1280,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 8 unmodified lines | |||
| 1079 | AVFrame *out; | 1280 | AVFrame *out; |
| 1080 | int ch, ret; | 1281 | int ch, ret; |
| 1081 | 1282 | ||
| 1082 | if (av_frame_is_writable(in)) { | 1283 | if (av_frame_is_writable(in) && s->process != 2) { |
| 1083 | out = in; | 1284 | out = in; |
| 1084 | } else { | 1285 | } else { |
| 1085 | out = ff_get_audio_buffer(outlink, in->nb_samples); | 1286 | out = ff_get_audio_buffer(outlink, in->nb_samples); |
| @@ -1232,10 +1433,11 @@ static const AVOption aiir_options[] = { · 146 unmodified lines | |||
| 1232 | { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" }, | 1433 | { "pr", "Z-plane zeros/poles (polar radians)", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "format" }, |
| 1233 | { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" }, | 1434 | { "pd", "Z-plane zeros/poles (polar degrees)", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, AF, "format" }, |
| 1234 | { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" }, | 1435 | { "sp", "S-plane zeros/poles", 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, AF, "format" }, |
| 1235 | { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" }, | 1436 | { "process", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" }, |
| 1236 | { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AF, "process" }, | 1437 | { "r", "set kind of processing", OFFSET(process), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, AF, "process" }, |
| 1237 | { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" }, | 1438 | { "d", "direct", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "process" }, |
| 1238 | { "s", "serial cascading", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" }, | 1439 | { "s", "serial", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, "process" }, |
| 1440 | { "p", "parallel", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, "process" }, | ||
| 1239 | { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" }, | 1441 | { "precision", "set filtering precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" }, |
| 1240 | { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" }, | 1442 | { "e", "set precision", OFFSET(precision),AV_OPT_TYPE_INT, {.i64=0}, 0, 3, AF, "precision" }, |
| 1241 | { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" }, | 1443 | { "dbl", "double-precision floating-point", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, "precision" }, |