Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -67,6 +67,8 @@ typedef struct AudioCrossoverContext { · 66 unmodified lines | ||
| 67 | 67 | |
| 68 | 68 | AVFrame *input_frame; |
| 69 | 69 | AVFrame *frames[MAX_BANDS]; |
| 70 | + | |
| 71 | + int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); | |
| 70 | 72 | } AudioCrossoverContext; |
| 71 | 73 | |
| 72 | 74 | #define OFFSET(x) offsetof(AudioCrossoverContext, x) |
| @@ -228,58 +230,12 @@ static void calc_q_factors(int order, double *q) · 155 unmodified lines | ||
| 228 | 230 | q[i] = 1. / (-2. * cos(M_PI * (2. * (i + 1) + n - 1.) / (2. * n))); |
| 229 | 231 | } |
| 230 | 232 | |
| 231 | −static int config_input(AVFilterLink *inlink) | |
| 232 | −{ | |
| 233 | − AVFilterContext *ctx = inlink->dst; | |
| 234 | − AudioCrossoverContext *s = ctx->priv; | |
| 235 | − int sample_rate = inlink->sample_rate; | |
| 236 | − double q[16]; | |
| 237 | − | |
| 238 | − s->xover = av_calloc(inlink->channels, sizeof(*s->xover)); | |
| 239 | − if (!s->xover) | |
| 240 | − return AVERROR(ENOMEM); | |
| 241 | − | |
| 242 | − s->order = (s->order_opt + 1) * 2; | |
| 243 | − s->filter_count = s->order / 2; | |
| 244 | − s->first_order = s->filter_count & 1; | |
| 245 | − s->ap_filter_count = s->filter_count / 2 + s->first_order; | |
| 246 | − calc_q_factors(s->order, q); | |
| 247 | − | |
| 248 | − for (int ch = 0; ch < inlink->channels; ch++) { | |
| 249 | − for (int band = 0; band <= s->nb_splits; band++) { | |
| 250 | − if (s->first_order) { | |
| 251 | − set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate); | |
| 252 | − set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate); | |
| 253 | − } | |
| 254 | − | |
| 255 | − for (int n = s->first_order; n < s->filter_count; n++) { | |
| 256 | − const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1; | |
| 257 | − | |
| 258 | − set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate); | |
| 259 | − set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate); | |
| 260 | − } | |
| 261 | − | |
| 262 | − for (int x = 0; x <= s->nb_splits && s->first_order; x++) | |
| 263 | − set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate); | |
| 264 | − | |
| 265 | − for (int n = s->first_order; n < s->ap_filter_count; n++) { | |
| 266 | − const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1); | |
| 267 | − | |
| 268 | − for (int x = 0; x <= s->nb_splits; x++) | |
| 269 | − set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate); | |
| 270 | − } | |
| 271 | − } | |
| 272 | − } | |
| 273 | − | |
| 274 | − return 0; | |
| 275 | −} | |
| 276 | − | |
| 277 | 233 | static int query_formats(AVFilterContext *ctx) |
| 278 | 234 | { |
| 279 | 235 | AVFilterFormats *formats; |
| 280 | 236 | AVFilterChannelLayouts *layouts; |
| 281 | 237 | static const enum AVSampleFormat sample_fmts[] = { |
| 282 | − AV_SAMPLE_FMT_DBLP, | |
| 238 | + AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, | |
| 283 | 239 | AV_SAMPLE_FMT_NONE |
| 284 | 240 | }; |
| 285 | 241 | int ret; |
| @@ -304,90 +260,149 @@ static int query_formats(AVFilterContext *ctx) · 18 unmodified lines | ||
| 304 | 260 | return ff_set_common_samplerates(ctx, formats); |
| 305 | 261 | } |
| 306 | 262 | |
| 307 | −static void biquad_process(BiquadContext *b, | |
| 308 | − double *dst, const double *src, | |
| 309 | − int nb_samples) | |
| 310 | −{ | |
| 311 | − const double b0 = b->b0; | |
| 312 | − const double b1 = b->b1; | |
| 313 | − const double b2 = b->b2; | |
| 314 | − const double a1 = b->a1; | |
| 315 | − const double a2 = b->a2; | |
| 316 | − double z1 = b->z1; | |
| 317 | − double z2 = b->z2; | |
| 318 | − | |
| 319 | − for (int n = 0; n < nb_samples; n++) { | |
| 320 | − const double in = src[n]; | |
| 321 | − double out; | |
| 322 | − | |
| 323 | − out = in * b0 + z1; | |
| 324 | − z1 = b1 * in + z2 + a1 * out; | |
| 325 | − z2 = b2 * in + a2 * out; | |
| 326 | − dst[n] = out; | |
| 327 | − } | |
| 263 | +#define BIQUAD_PROCESS(name, type) \ | |
| 264 | +static void biquad_process_## name(BiquadContext *b, \ | |
| 265 | + type *dst, const type *src, \ | |
| 266 | + int nb_samples) \ | |
| 267 | +{ \ | |
| 268 | + const type b0 = b->b0; \ | |
| 269 | + const type b1 = b->b1; \ | |
| 270 | + const type b2 = b->b2; \ | |
| 271 | + const type a1 = b->a1; \ | |
| 272 | + const type a2 = b->a2; \ | |
| 273 | + type z1 = b->z1; \ | |
| 274 | + type z2 = b->z2; \ | |
| 275 | + \ | |
| 276 | + for (int n = 0; n < nb_samples; n++) { \ | |
| 277 | + const type in = src[n]; \ | |
| 278 | + type out; \ | |
| 279 | + \ | |
| 280 | + out = in * b0 + z1; \ | |
| 281 | + z1 = b1 * in + z2 + a1 * out; \ | |
| 282 | + z2 = b2 * in + a2 * out; \ | |
| 283 | + dst[n] = out; \ | |
| 284 | + } \ | |
| 285 | + \ | |
| 286 | + b->z1 = z1; \ | |
| 287 | + b->z2 = z2; \ | |
| 288 | +} | |
| 328 | 289 | |
| 329 | − b->z1 = z1; | |
| 330 | − b->z2 = z2; | |
| 290 | +BIQUAD_PROCESS(fltp, float) | |
| 291 | +BIQUAD_PROCESS(dblp, double) | |
| 292 | + | |
| 293 | +#define XOVER_PROCESS(name, type, one) \ | |
| 294 | +static int filter_channels_## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ | |
| 295 | +{ \ | |
| 296 | + AudioCrossoverContext *s = ctx->priv; \ | |
| 297 | + AVFrame *in = s->input_frame; \ | |
| 298 | + AVFrame **frames = s->frames; \ | |
| 299 | + const int start = (in->channels * jobnr) / nb_jobs; \ | |
| 300 | + const int end = (in->channels * (jobnr+1)) / nb_jobs; \ | |
| 301 | + const int nb_samples = in->nb_samples; \ | |
| 302 | + \ | |
| 303 | + for (int ch = start; ch < end; ch++) { \ | |
| 304 | + CrossoverChannel *xover = &s->xover[ch]; \ | |
| 305 | + \ | |
| 306 | + for (int band = 0; band < ctx->nb_outputs; band++) { \ | |
| 307 | + for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \ | |
| 308 | + const type *src = band == 0 ? (const type *)in->extended_data[ch] : (const type *)frames[band]->extended_data[ch]; \ | |
| 309 | + type *dst = (type *)frames[band + 1]->extended_data[ch]; \ | |
| 310 | + const type *hsrc = f == 0 ? src : dst; \ | |
| 311 | + BiquadContext *hp = &xover->hp[band][f]; \ | |
| 312 | + \ | |
| 313 | + biquad_process_## name(hp, dst, hsrc, nb_samples); \ | |
| 314 | + } \ | |
| 315 | + \ | |
| 316 | + for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { \ | |
| 317 | + const type *src = band == 0 ? (const type *)in->extended_data[ch] : (const type *)frames[band]->extended_data[ch]; \ | |
| 318 | + type *dst = (type *)frames[band]->extended_data[ch]; \ | |
| 319 | + const type *lsrc = f == 0 ? src : dst; \ | |
| 320 | + BiquadContext *lp = &xover->lp[band][f]; \ | |
| 321 | + \ | |
| 322 | + biquad_process_## name(lp, dst, lsrc, nb_samples); \ | |
| 323 | + } \ | |
| 324 | + \ | |
| 325 | + for (int aband = band + 1; aband < ctx->nb_outputs; aband++) { \ | |
| 326 | + if (s->first_order) { \ | |
| 327 | + const type *src = (const type *)frames[band]->extended_data[ch]; \ | |
| 328 | + type *dst = (type *)frames[band]->extended_data[ch]; \ | |
| 329 | + BiquadContext *ap = &xover->ap[band][aband][0]; \ | |
| 330 | + \ | |
| 331 | + biquad_process_## name(ap, dst, src, nb_samples); \ | |
| 332 | + } \ | |
| 333 | + \ | |
| 334 | + for (int f = s->first_order; f < s->ap_filter_count; f++) { \ | |
| 335 | + const type *src = (const type *)frames[band]->extended_data[ch]; \ | |
| 336 | + type *dst = (type *)frames[band]->extended_data[ch]; \ | |
| 337 | + BiquadContext *ap = &xover->ap[band][aband][f]; \ | |
| 338 | + \ | |
| 339 | + biquad_process_## name(ap, dst, src, nb_samples); \ | |
| 340 | + } \ | |
| 341 | + } \ | |
| 342 | + } \ | |
| 343 | + \ | |
| 344 | + for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { \ | |
| 345 | + if (band & 1) { \ | |
| 346 | + type *dst = (type *)frames[band]->extended_data[ch]; \ | |
| 347 | + \ | |
| 348 | + for (int n = 0; n < nb_samples; n++) \ | |
| 349 | + dst[n] *= -one; \ | |
| 350 | + } \ | |
| 351 | + } \ | |
| 352 | + } \ | |
| 353 | + \ | |
| 354 | + return 0; \ | |
| 331 | 355 | } |
| 332 | 356 | |
| 333 | −static int filter_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 357 | +XOVER_PROCESS(fltp, float, 1.f) | |
| 358 | +XOVER_PROCESS(dblp, double, 1.0) | |
| 359 | + | |
| 360 | +static int config_input(AVFilterLink *inlink) | |
| 334 | 361 | { |
| 362 | + AVFilterContext *ctx = inlink->dst; | |
| 335 | 363 | AudioCrossoverContext *s = ctx->priv; |
| 336 | − AVFrame *in = s->input_frame; | |
| 337 | − AVFrame **frames = s->frames; | |
| 338 | − const int start = (in->channels * jobnr) / nb_jobs; | |
| 339 | − const int end = (in->channels * (jobnr+1)) / nb_jobs; | |
| 340 | − const int nb_samples = in->nb_samples; | |
| 364 | + int sample_rate = inlink->sample_rate; | |
| 365 | + double q[16]; | |
| 341 | 366 | |
| 342 | − for (int ch = start; ch < end; ch++) { | |
| 343 | − CrossoverChannel *xover = &s->xover[ch]; | |
| 367 | + s->xover = av_calloc(inlink->channels, sizeof(*s->xover)); | |
| 368 | + if (!s->xover) | |
| 369 | + return AVERROR(ENOMEM); | |
| 344 | 370 | |
| 345 | − for (int band = 0; band < ctx->nb_outputs; band++) { | |
| 346 | − for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { | |
| 347 | − const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch]; | |
| 348 | − double *dst = (double *)frames[band + 1]->extended_data[ch]; | |
| 349 | − const double *hsrc = f == 0 ? src : dst; | |
| 350 | − BiquadContext *hp = &xover->hp[band][f]; | |
| 371 | + s->order = (s->order_opt + 1) * 2; | |
| 372 | + s->filter_count = s->order / 2; | |
| 373 | + s->first_order = s->filter_count & 1; | |
| 374 | + s->ap_filter_count = s->filter_count / 2 + s->first_order; | |
| 375 | + calc_q_factors(s->order, q); | |
| 351 | 376 | |
| 352 | − biquad_process(hp, dst, hsrc, nb_samples); | |
| 377 | + for (int ch = 0; ch < inlink->channels; ch++) { | |
| 378 | + for (int band = 0; band <= s->nb_splits; band++) { | |
| 379 | + if (s->first_order) { | |
| 380 | + set_lp(&s->xover[ch].lp[band][0], s->splits[band], 0.5, sample_rate); | |
| 381 | + set_hp(&s->xover[ch].hp[band][0], s->splits[band], 0.5, sample_rate); | |
| 353 | 382 | } |
| 354 | 383 | |
| 355 | − for (int f = 0; band + 1 < ctx->nb_outputs && f < s->filter_count; f++) { | |
| 356 | − const double *src = band == 0 ? (const double *)in->extended_data[ch] : (const double *)frames[band]->extended_data[ch]; | |
| 357 | − double *dst = (double *)frames[band]->extended_data[ch]; | |
| 358 | − const double *lsrc = f == 0 ? src : dst; | |
| 359 | − BiquadContext *lp = &xover->lp[band][f]; | |
| 384 | + for (int n = s->first_order; n < s->filter_count; n++) { | |
| 385 | + const int idx = s->filter_count / 2 - ((n + s->first_order) / 2 - s->first_order) - 1; | |
| 360 | 386 | |
| 361 | − biquad_process(lp, dst, lsrc, nb_samples); | |
| 387 | + set_lp(&s->xover[ch].lp[band][n], s->splits[band], q[idx], sample_rate); | |
| 388 | + set_hp(&s->xover[ch].hp[band][n], s->splits[band], q[idx], sample_rate); | |
| 362 | 389 | } |
| 363 | 390 | |
| 364 | − for (int aband = band + 1; aband < ctx->nb_outputs; aband++) { | |
| 365 | − if (s->first_order) { | |
| 366 | − const double *src = (const double *)frames[band]->extended_data[ch]; | |
| 367 | − double *dst = (double *)frames[band]->extended_data[ch]; | |
| 368 | − BiquadContext *ap = &xover->ap[band][aband][0]; | |
| 369 | − | |
| 370 | − biquad_process(ap, dst, src, nb_samples); | |
| 371 | − } | |
| 391 | + for (int x = 0; x <= s->nb_splits && s->first_order; x++) | |
| 392 | + set_ap1(&s->xover[ch].ap[x][band][0], s->splits[band], sample_rate); | |
| 372 | 393 | |
| 373 | − for (int f = s->first_order; f < s->ap_filter_count; f++) { | |
| 374 | − const double *src = (const double *)frames[band]->extended_data[ch]; | |
| 375 | − double *dst = (double *)frames[band]->extended_data[ch]; | |
| 376 | − BiquadContext *ap = &xover->ap[band][aband][f]; | |
| 394 | + for (int n = s->first_order; n < s->ap_filter_count; n++) { | |
| 395 | + const int idx = (s->filter_count / 2 - ((n * 2 + s->first_order) / 2 - s->first_order) - 1); | |
| 377 | 396 | |
| 378 | − biquad_process(ap, dst, src, nb_samples); | |
| 379 | − } | |
| 397 | + for (int x = 0; x <= s->nb_splits; x++) | |
| 398 | + set_ap(&s->xover[ch].ap[x][band][n], s->splits[band], q[idx], sample_rate); | |
| 380 | 399 | } |
| 381 | 400 | } |
| 401 | + } | |
| 382 | 402 | |
| 383 | − for (int band = 0; band < ctx->nb_outputs && s->first_order; band++) { | |
| 384 | − if (band & 1) { | |
| 385 | − double *dst = (double *)frames[band]->extended_data[ch]; | |
| 386 | − | |
| 387 | − for (int n = 0; n < nb_samples; n++) | |
| 388 | − dst[n] *= -1.; | |
| 389 | − } | |
| 390 | − } | |
| 403 | + switch (inlink->format) { | |
| 404 | + case AV_SAMPLE_FMT_FLTP: s->filter_channels = filter_channels_fltp; break; | |
| 405 | + case AV_SAMPLE_FMT_DBLP: s->filter_channels = filter_channels_dblp; break; | |
| 391 | 406 | } |
| 392 | 407 | |
| 393 | 408 | return 0; |
| @@ -415,8 +430,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 21 unmodified lines | ||
| 415 | 430 | goto fail; |
| 416 | 431 | |
| 417 | 432 | s->input_frame = in; |
| 418 | − ctx->internal->execute(ctx, filter_channels, NULL, NULL, FFMIN(inlink->channels, | |
| 419 | − ff_filter_get_nb_threads(ctx))); | |
| 433 | + ctx->internal->execute(ctx, s->filter_channels, NULL, NULL, FFMIN(inlink->channels, | |
| 434 | + ff_filter_get_nb_threads(ctx))); | |
| 420 | 435 | |
| 421 | 436 | for (i = 0; i < ctx->nb_outputs; i++) { |
| 422 | 437 | ret = ff_filter_frame(ctx->outputs[i], frames[i]); |