History

avfilter/af_drmeter: misc cleanups

Paul B Mahol committed 3 years ago

Commit 494ebfd1 parent 238ec0c1 file · +27 −27Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_drmeter.c+27−27
@@ -41,7 +41,7 @@ typedef struct DRMeterContext { · 40 unmodified lines
4141 const AVClass *class;
4242 ChannelStats *chstats;
4343 int nb_channels;
44− uint64_t tc_samples;
44+ int64_t tc_samples;
4545 double time_constant;
4646 } DRMeterContext;
4747
@@ -59,11 +59,11 @@ static int config_output(AVFilterLink *outlink) · 11 unmodified lines
5959 {
6060 DRMeterContext *s = outlink->src->priv;
6161
62− s->chstats = av_calloc(sizeof(*s->chstats), outlink->ch_layout.nb_channels);
62+ s->chstats = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->chstats));
6363 if (!s->chstats)
6464 return AVERROR(ENOMEM);
6565 s->nb_channels = outlink->ch_layout.nb_channels;
66− s->tc_samples = s->time_constant * outlink->sample_rate + .5;
66+ s->tc_samples = lrint(s->time_constant * outlink->sample_rate);
6767
6868 return 0;
6969 }
@@ -73,7 +73,7 @@ static void finish_block(ChannelStats *p) · 3 unmodified lines
7373 int peak_bin, rms_bin;
7474 float peak, rms;
7575
76− rms = sqrt(2 * p->sum / p->nb_samples);
76+ rms = sqrtf(2.f * p->sum / p->nb_samples);
7777 peak = p->peak;
7878 rms_bin = av_clip(lrintf(rms * BINS), 0, BINS);
7979 peak_bin = av_clip(lrintf(peak * BINS), 0, BINS);
@@ -88,36 +88,33 @@ static void finish_block(ChannelStats *p) · 8 unmodified lines
8888
8989 static void update_stat(DRMeterContext *s, ChannelStats *p, float sample)
9090 {
91− if (p->nb_samples >= s->tc_samples) {
92− finish_block(p);
93− }
94−
95− p->peak = FFMAX(FFABS(sample), p->peak);
91+ p->peak = fmaxf(fabsf(sample), p->peak);
9692 p->sum += sample * sample;
9793 p->nb_samples++;
94+ if (p->nb_samples >= s->tc_samples)
95+ finish_block(p);
9896 }
9997
10098 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
10199 {
102100 DRMeterContext *s = inlink->dst->priv;
103101 const int channels = s->nb_channels;
104− int i, c;
105102
106103 switch (inlink->format) {
107104 case AV_SAMPLE_FMT_FLTP:
108− for (c = 0; c < channels; c++) {
105+ for (int c = 0; c < channels; c++) {
109106 ChannelStats *p = &s->chstats[c];
110107 const float *src = (const float *)buf->extended_data[c];
111108
112− for (i = 0; i < buf->nb_samples; i++, src++)
109+ for (int i = 0; i < buf->nb_samples; i++, src++)
113110 update_stat(s, p, *src);
114111 }
115112 break;
116113 case AV_SAMPLE_FMT_FLT: {
117114 const float *src = (const float *)buf->extended_data[0];
118115
119− for (i = 0; i < buf->nb_samples; i++) {
120− for (c = 0; c < channels; c++, src++)
116+ for (int i = 0; i < buf->nb_samples; i++) {
117+ for (int c = 0; c < channels; c++, src++)
121118 update_stat(s, &s->chstats[c], *src);
122119 }}
123120 break;
@@ -131,39 +128,42 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) · 7 unmodified lines
131128 static void print_stats(AVFilterContext *ctx)
132129 {
133130 DRMeterContext *s = ctx->priv;
134− float dr = 0;
135− int ch;
131+ float dr = 0.f;
136132
137− for (ch = 0; ch < s->nb_channels; ch++) {
133+ for (int ch = 0; ch < s->nb_channels; ch++) {
138134 ChannelStats *p = &s->chstats[ch];
139− float chdr, secondpeak, rmssum = 0;
140− int i, j, first = 0;
135+ float chdr, secondpeak, rmssum = 0.f;
136+ int first = 0, last = lrintf(0.2f * p->blknum);
137+ int peak_bin = BINS;
141138
142139 if (!p->nb_samples) {
143140 av_log(ctx, AV_LOG_INFO, "No data, dynamic range not meassurable\n");
144141 return;
145142 }
146143
147− finish_block(p);
144+ if (p->nb_samples)
145+ finish_block(p);
148146
149− for (i = 0; i <= BINS; i++) {
150− if (p->peaks[BINS - i]) {
151− if (first || p->peaks[BINS - i] > 1)
147+ for (int i = BINS; i >= 0; i--) {
148+ if (p->peaks[i]) {
149+ if (first || p->peaks[i] > 1) {
150+ peak_bin = i;
152151 break;
152+ }
153153 first = 1;
154154 }
155155 }
156156
157− secondpeak = (BINS - i) / (double)BINS;
157+ secondpeak = peak_bin / (float)BINS;
158158
159− for (i = BINS, j = 0; i >= 0 && j < 0.2 * p->blknum; i--) {
159+ for (int64_t i = BINS, j = 0; i >= 0 && j < last; i--) {
160160 if (p->rms[i]) {
161− rmssum += SQR(i / (double)BINS);
161+ rmssum += SQR(i / (float)BINS) * p->rms[i];
162162 j += p->rms[i];
163163 }
164164 }
165165
166− chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum)));
166+ chdr = 20.f * log10f(secondpeak / sqrtf(rmssum / (float)last));
167167 dr += chdr;
168168 av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %g\n", ch + 1, chdr);
169169 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t