File size: 26,593 Bytes
8ead80b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
/*
* Copyright (c) 2020 Paul B Mahol
*
* Speech Normalizer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Speech Normalizer
*/
#include <float.h>
#include "libavutil/avassert.h"
#include "libavutil/channel_layout.h"
#include "libavutil/opt.h"
#define FF_BUFQUEUE_SIZE (1024)
#include "bufferqueue.h"
#include "audio.h"
#include "avfilter.h"
#include "filters.h"
#include "internal.h"
#define MAX_ITEMS 882000
#define MIN_PEAK (1. / 32768.)
typedef struct PeriodItem {
int size;
int type;
double max_peak;
double rms_sum;
} PeriodItem;
typedef struct ChannelContext {
int state;
int bypass;
PeriodItem pi[MAX_ITEMS];
double gain_state;
double pi_max_peak;
double pi_rms_sum;
int pi_start;
int pi_end;
int pi_size;
} ChannelContext;
typedef struct SpeechNormalizerContext {
const AVClass *class;
double rms_value;
double peak_value;
double max_expansion;
double max_compression;
double threshold_value;
double raise_amount;
double fall_amount;
char *ch_layout_str;
AVChannelLayout ch_layout;
int invert;
int link;
ChannelContext *cc;
double prev_gain;
int max_period;
int eof;
int64_t pts;
struct FFBufQueue queue;
void (*analyze_channel)(AVFilterContext *ctx, ChannelContext *cc,
const uint8_t *srcp, int nb_samples);
void (*filter_channels[2])(AVFilterContext *ctx,
AVFrame *in, AVFrame *out, int nb_samples);
} SpeechNormalizerContext;
#define OFFSET(x) offsetof(SpeechNormalizerContext, x)
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption speechnorm_options[] = {
{ "peak", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.0, 1.0, FLAGS },
{ "p", "set the peak value", OFFSET(peak_value), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.0, 1.0, FLAGS },
{ "expansion", "set the max expansion factor", OFFSET(max_expansion), AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1.0, 50.0, FLAGS },
{ "e", "set the max expansion factor", OFFSET(max_expansion), AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1.0, 50.0, FLAGS },
{ "compression", "set the max compression factor", OFFSET(max_compression), AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1.0, 50.0, FLAGS },
{ "c", "set the max compression factor", OFFSET(max_compression), AV_OPT_TYPE_DOUBLE, {.dbl=2.0}, 1.0, 50.0, FLAGS },
{ "threshold", "set the threshold value", OFFSET(threshold_value), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0.0, 1.0, FLAGS },
{ "t", "set the threshold value", OFFSET(threshold_value), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0.0, 1.0, FLAGS },
{ "raise", "set the expansion raising amount", OFFSET(raise_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0.0, 1.0, FLAGS },
{ "r", "set the expansion raising amount", OFFSET(raise_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0.0, 1.0, FLAGS },
{ "fall", "set the compression raising amount", OFFSET(fall_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0.0, 1.0, FLAGS },
{ "f", "set the compression raising amount", OFFSET(fall_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.001}, 0.0, 1.0, FLAGS },
{ "channels", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
{ "h", "set channels to filter", OFFSET(ch_layout_str), AV_OPT_TYPE_STRING, {.str="all"}, 0, 0, FLAGS },
{ "invert", "set inverted filtering", OFFSET(invert), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
{ "i", "set inverted filtering", OFFSET(invert), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
{ "link", "set linked channels filtering", OFFSET(link), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
{ "l", "set linked channels filtering", OFFSET(link), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
{ "rms", "set the RMS value", OFFSET(rms_value), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0.0, 1.0, FLAGS },
{ "m", "set the RMS value", OFFSET(rms_value), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, 0.0, 1.0, FLAGS },
{ NULL }
};
AVFILTER_DEFINE_CLASS(speechnorm);
static int get_pi_samples(PeriodItem *pi, int start, int end, int remain)
{
int sum;
if (pi[start].type == 0)
return remain;
sum = remain;
while (start != end) {
start++;
if (start >= MAX_ITEMS)
start = 0;
if (pi[start].type == 0)
break;
av_assert1(pi[start].size > 0);
sum += pi[start].size;
}
return sum;
}
static int available_samples(AVFilterContext *ctx)
{
SpeechNormalizerContext *s = ctx->priv;
AVFilterLink *inlink = ctx->inputs[0];
int min_pi_nb_samples;
min_pi_nb_samples = get_pi_samples(s->cc[0].pi, s->cc[0].pi_start, s->cc[0].pi_end, s->cc[0].pi_size);
for (int ch = 1; ch < inlink->ch_layout.nb_channels && min_pi_nb_samples > 0; ch++) {
ChannelContext *cc = &s->cc[ch];
min_pi_nb_samples = FFMIN(min_pi_nb_samples, get_pi_samples(cc->pi, cc->pi_start, cc->pi_end, cc->pi_size));
}
return min_pi_nb_samples;
}
static void consume_pi(ChannelContext *cc, int nb_samples)
{
if (cc->pi_size >= nb_samples) {
cc->pi_size -= nb_samples;
} else {
av_assert1(0);
}
}
static double next_gain(AVFilterContext *ctx, double pi_max_peak, int bypass, double state,
double pi_rms_sum, int pi_size)
{
SpeechNormalizerContext *s = ctx->priv;
const double compression = 1. / s->max_compression;
const int type = s->invert ? pi_max_peak <= s->threshold_value : pi_max_peak >= s->threshold_value;
double expansion = FFMIN(s->max_expansion, s->peak_value / pi_max_peak);
if (s->rms_value > DBL_EPSILON)
expansion = FFMIN(expansion, s->rms_value / sqrt(pi_rms_sum / pi_size));
if (bypass) {
return 1.;
} else if (type) {
return FFMIN(expansion, state + s->raise_amount);
} else {
return FFMIN(expansion, FFMAX(compression, state - s->fall_amount));
}
}
static void next_pi(AVFilterContext *ctx, ChannelContext *cc, int bypass)
{
av_assert1(cc->pi_size >= 0);
if (cc->pi_size == 0) {
SpeechNormalizerContext *s = ctx->priv;
int start = cc->pi_start;
av_assert1(cc->pi[start].size > 0);
av_assert0(cc->pi[start].type > 0 || s->eof);
cc->pi_size = cc->pi[start].size;
cc->pi_rms_sum = cc->pi[start].rms_sum;
cc->pi_max_peak = cc->pi[start].max_peak;
av_assert1(cc->pi_start != cc->pi_end || s->eof);
start++;
if (start >= MAX_ITEMS)
start = 0;
cc->pi_start = start;
cc->gain_state = next_gain(ctx, cc->pi_max_peak, bypass, cc->gain_state,
cc->pi_rms_sum, cc->pi_size);
}
}
static double min_gain(AVFilterContext *ctx, ChannelContext *cc, int max_size)
{
SpeechNormalizerContext *s = ctx->priv;
double min_gain = s->max_expansion;
double gain_state = cc->gain_state;
int size = cc->pi_size;
int idx = cc->pi_start;
min_gain = FFMIN(min_gain, gain_state);
while (size <= max_size) {
if (idx == cc->pi_end)
break;
gain_state = next_gain(ctx, cc->pi[idx].max_peak, 0, gain_state,
cc->pi[idx].rms_sum, cc->pi[idx].size);
min_gain = FFMIN(min_gain, gain_state);
size += cc->pi[idx].size;
idx++;
if (idx >= MAX_ITEMS)
idx = 0;
}
return min_gain;
}
#define ANALYZE_CHANNEL(name, ptype, zero, min_peak) \
static void analyze_channel_## name (AVFilterContext *ctx, ChannelContext *cc, \
const uint8_t *srcp, int nb_samples) \
{ \
SpeechNormalizerContext *s = ctx->priv; \
const ptype *src = (const ptype *)srcp; \
const int max_period = s->max_period; \
PeriodItem *pi = (PeriodItem *)&cc->pi; \
int pi_end = cc->pi_end; \
int n = 0; \
\
if (cc->state < 0) \
cc->state = src[0] >= zero; \
\
while (n < nb_samples) { \
ptype new_max_peak; \
ptype new_rms_sum; \
int new_size; \
\
if ((cc->state != (src[n] >= zero)) || \
(pi[pi_end].size > max_period)) { \
ptype max_peak = pi[pi_end].max_peak; \
ptype rms_sum = pi[pi_end].rms_sum; \
int state = cc->state; \
\
cc->state = src[n] >= zero; \
av_assert1(pi[pi_end].size > 0); \
if (max_peak >= min_peak || \
pi[pi_end].size > max_period) { \
pi[pi_end].type = 1; \
pi_end++; \
if (pi_end >= MAX_ITEMS) \
pi_end = 0; \
if (cc->state != state) { \
pi[pi_end].max_peak = DBL_MIN; \
pi[pi_end].rms_sum = 0.0; \
} else { \
pi[pi_end].max_peak = max_peak; \
pi[pi_end].rms_sum = rms_sum; \
} \
pi[pi_end].type = 0; \
pi[pi_end].size = 0; \
av_assert1(pi_end != cc->pi_start); \
} \
} \
\
new_max_peak = pi[pi_end].max_peak; \
new_rms_sum = pi[pi_end].rms_sum; \
new_size = pi[pi_end].size; \
if (cc->state) { \
while (src[n] >= zero) { \
new_max_peak = FFMAX(new_max_peak, src[n]); \
new_rms_sum += src[n] * src[n]; \
new_size++; \
n++; \
if (n >= nb_samples) \
break; \
} \
} else { \
while (src[n] < zero) { \
new_max_peak = FFMAX(new_max_peak, -src[n]); \
new_rms_sum += src[n] * src[n]; \
new_size++; \
n++; \
if (n >= nb_samples) \
break; \
} \
} \
\
pi[pi_end].max_peak = new_max_peak; \
pi[pi_end].rms_sum = new_rms_sum; \
pi[pi_end].size = new_size; \
} \
cc->pi_end = pi_end; \
}
ANALYZE_CHANNEL(dbl, double, 0.0, MIN_PEAK)
ANALYZE_CHANNEL(flt, float, 0.f, (float)MIN_PEAK)
#define FILTER_CHANNELS(name, ptype) \
static void filter_channels_## name (AVFilterContext *ctx, \
AVFrame *in, AVFrame *out, int nb_samples) \
{ \
SpeechNormalizerContext *s = ctx->priv; \
AVFilterLink *inlink = ctx->inputs[0]; \
\
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
ChannelContext *cc = &s->cc[ch]; \
const ptype *src = (const ptype *)in->extended_data[ch]; \
ptype *dst = (ptype *)out->extended_data[ch]; \
enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
const int bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
int n = 0; \
\
while (n < nb_samples) { \
ptype gain; \
int size; \
\
next_pi(ctx, cc, bypass); \
size = FFMIN(nb_samples - n, cc->pi_size); \
av_assert1(size > 0); \
gain = cc->gain_state; \
consume_pi(cc, size); \
for (int i = n; !ctx->is_disabled && i < n + size; i++) \
dst[i] = src[i] * gain; \
n += size; \
} \
} \
}
FILTER_CHANNELS(dbl, double)
FILTER_CHANNELS(flt, float)
static double dlerp(double min, double max, double mix)
{
return min + (max - min) * mix;
}
static float flerp(float min, float max, float mix)
{
return min + (max - min) * mix;
}
#define FILTER_LINK_CHANNELS(name, ptype, tlerp) \
static void filter_link_channels_## name (AVFilterContext *ctx, \
AVFrame *in, AVFrame *out, \
int nb_samples) \
{ \
SpeechNormalizerContext *s = ctx->priv; \
AVFilterLink *inlink = ctx->inputs[0]; \
int n = 0; \
\
while (n < nb_samples) { \
int min_size = nb_samples - n; \
ptype gain = s->max_expansion; \
\
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
ChannelContext *cc = &s->cc[ch]; \
\
enum AVChannel channel = av_channel_layout_channel_from_index(&inlink->ch_layout, ch); \
cc->bypass = av_channel_layout_index_from_channel(&s->ch_layout, channel) < 0; \
\
next_pi(ctx, cc, cc->bypass); \
min_size = FFMIN(min_size, cc->pi_size); \
} \
\
av_assert1(min_size > 0); \
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
ChannelContext *cc = &s->cc[ch]; \
\
if (cc->bypass) \
continue; \
gain = FFMIN(gain, min_gain(ctx, cc, min_size)); \
} \
\
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { \
ChannelContext *cc = &s->cc[ch]; \
const ptype *src = (const ptype *)in->extended_data[ch]; \
ptype *dst = (ptype *)out->extended_data[ch]; \
\
consume_pi(cc, min_size); \
if (cc->bypass) \
continue; \
\
for (int i = n; !ctx->is_disabled && i < n + min_size; i++) { \
ptype g = tlerp(s->prev_gain, gain, (i - n) / (ptype)min_size); \
dst[i] = src[i] * g; \
} \
} \
\
s->prev_gain = gain; \
n += min_size; \
} \
}
FILTER_LINK_CHANNELS(dbl, double, dlerp)
FILTER_LINK_CHANNELS(flt, float, flerp)
static int filter_frame(AVFilterContext *ctx)
{
SpeechNormalizerContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
AVFilterLink *inlink = ctx->inputs[0];
int ret;
while (s->queue.available > 0) {
int min_pi_nb_samples;
AVFrame *in, *out;
in = ff_bufqueue_peek(&s->queue, 0);
if (!in)
break;
min_pi_nb_samples = available_samples(ctx);
if (min_pi_nb_samples < in->nb_samples && !s->eof)
break;
in = ff_bufqueue_get(&s->queue);
if (av_frame_is_writable(in)) {
out = in;
} else {
out = ff_get_audio_buffer(outlink, in->nb_samples);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
av_frame_copy_props(out, in);
}
s->filter_channels[s->link](ctx, in, out, in->nb_samples);
s->pts = in->pts + av_rescale_q(in->nb_samples, av_make_q(1, outlink->sample_rate),
outlink->time_base);
if (out != in)
av_frame_free(&in);
return ff_filter_frame(outlink, out);
}
for (int f = 0; f < ff_inlink_queued_frames(inlink); f++) {
AVFrame *in;
ret = ff_inlink_consume_frame(inlink, &in);
if (ret < 0)
return ret;
if (ret == 0)
break;
ff_bufqueue_add(ctx, &s->queue, in);
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
ChannelContext *cc = &s->cc[ch];
s->analyze_channel(ctx, cc, in->extended_data[ch], in->nb_samples);
}
}
return 1;
}
static int activate(AVFilterContext *ctx)
{
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
SpeechNormalizerContext *s = ctx->priv;
int ret, status;
int64_t pts;
ret = av_channel_layout_copy(&s->ch_layout, &inlink->ch_layout);
if (ret < 0)
return ret;
if (strcmp(s->ch_layout_str, "all"))
av_channel_layout_from_string(&s->ch_layout,
s->ch_layout_str);
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
ret = filter_frame(ctx);
if (ret <= 0)
return ret;
if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
if (status == AVERROR_EOF)
s->eof = 1;
}
if (s->eof && ff_inlink_queued_samples(inlink) == 0 &&
s->queue.available == 0) {
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
return 0;
}
if (s->queue.available > 0) {
AVFrame *in = ff_bufqueue_peek(&s->queue, 0);
const int nb_samples = available_samples(ctx);
if (nb_samples >= in->nb_samples || s->eof) {
ff_filter_set_ready(ctx, 10);
return 0;
}
}
FF_FILTER_FORWARD_WANTED(outlink, inlink);
return FFERROR_NOT_READY;
}
static int config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
SpeechNormalizerContext *s = ctx->priv;
s->max_period = inlink->sample_rate / 10;
s->prev_gain = 1.;
s->cc = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cc));
if (!s->cc)
return AVERROR(ENOMEM);
for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
ChannelContext *cc = &s->cc[ch];
cc->state = -1;
cc->gain_state = s->max_expansion;
}
switch (inlink->format) {
case AV_SAMPLE_FMT_FLTP:
s->analyze_channel = analyze_channel_flt;
s->filter_channels[0] = filter_channels_flt;
s->filter_channels[1] = filter_link_channels_flt;
break;
case AV_SAMPLE_FMT_DBLP:
s->analyze_channel = analyze_channel_dbl;
s->filter_channels[0] = filter_channels_dbl;
s->filter_channels[1] = filter_link_channels_dbl;
break;
default:
av_assert1(0);
}
return 0;
}
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
SpeechNormalizerContext *s = ctx->priv;
int link = s->link;
int ret;
ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
if (ret < 0)
return ret;
if (link != s->link)
s->prev_gain = 1.;
return 0;
}
static av_cold void uninit(AVFilterContext *ctx)
{
SpeechNormalizerContext *s = ctx->priv;
ff_bufqueue_discard_all(&s->queue);
av_channel_layout_uninit(&s->ch_layout);
av_freep(&s->cc);
}
static const AVFilterPad inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_input,
},
};
const AVFilter ff_af_speechnorm = {
.name = "speechnorm",
.description = NULL_IF_CONFIG_SMALL("Speech Normalizer."),
.priv_size = sizeof(SpeechNormalizerContext),
.priv_class = &speechnorm_class,
.activate = activate,
.uninit = uninit,
FILTER_INPUTS(inputs),
FILTER_OUTPUTS(ff_audio_default_filterpad),
FILTER_SAMPLEFMTS(AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP),
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
.process_command = process_command,
};
|