Unified · Split
| @@ -41,7 +41,7 @@ typedef struct DRMeterContext { · 40 unmodified lines | ||
| 41 | 41 | const AVClass *class; |
| 42 | 42 | ChannelStats *chstats; |
| 43 | 43 | int nb_channels; |
| 44 | − uint64_t tc_samples; | |
| 44 | + int64_t tc_samples; | |
| 45 | 45 | double time_constant; |
| 46 | 46 | } DRMeterContext; |
| 47 | 47 | |
| @@ -59,11 +59,11 @@ static int config_output(AVFilterLink *outlink) · 11 unmodified lines | ||
| 59 | 59 | { |
| 60 | 60 | DRMeterContext *s = outlink->src->priv; |
| 61 | 61 | |
| 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)); | |
| 63 | 63 | if (!s->chstats) |
| 64 | 64 | return AVERROR(ENOMEM); |
| 65 | 65 | 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); | |
| 67 | 67 | |
| 68 | 68 | return 0; |
| 69 | 69 | } |
| @@ -73,7 +73,7 @@ static void finish_block(ChannelStats *p) · 3 unmodified lines | ||
| 73 | 73 | int peak_bin, rms_bin; |
| 74 | 74 | float peak, rms; |
| 75 | 75 | |
| 76 | − rms = sqrt(2 * p->sum / p->nb_samples); | |
| 76 | + rms = sqrtf(2.f * p->sum / p->nb_samples); | |
| 77 | 77 | peak = p->peak; |
| 78 | 78 | rms_bin = av_clip(lrintf(rms * BINS), 0, BINS); |
| 79 | 79 | peak_bin = av_clip(lrintf(peak * BINS), 0, BINS); |
| @@ -88,36 +88,33 @@ static void finish_block(ChannelStats *p) · 8 unmodified lines | ||
| 88 | 88 | |
| 89 | 89 | static void update_stat(DRMeterContext *s, ChannelStats *p, float sample) |
| 90 | 90 | { |
| 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); | |
| 96 | 92 | p->sum += sample * sample; |
| 97 | 93 | p->nb_samples++; |
| 94 | + if (p->nb_samples >= s->tc_samples) | |
| 95 | + finish_block(p); | |
| 98 | 96 | } |
| 99 | 97 | |
| 100 | 98 | static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
| 101 | 99 | { |
| 102 | 100 | DRMeterContext *s = inlink->dst->priv; |
| 103 | 101 | const int channels = s->nb_channels; |
| 104 | − int i, c; | |
| 105 | 102 | |
| 106 | 103 | switch (inlink->format) { |
| 107 | 104 | case AV_SAMPLE_FMT_FLTP: |
| 108 | − for (c = 0; c < channels; c++) { | |
| 105 | + for (int c = 0; c < channels; c++) { | |
| 109 | 106 | ChannelStats *p = &s->chstats[c]; |
| 110 | 107 | const float *src = (const float *)buf->extended_data[c]; |
| 111 | 108 | |
| 112 | − for (i = 0; i < buf->nb_samples; i++, src++) | |
| 109 | + for (int i = 0; i < buf->nb_samples; i++, src++) | |
| 113 | 110 | update_stat(s, p, *src); |
| 114 | 111 | } |
| 115 | 112 | break; |
| 116 | 113 | case AV_SAMPLE_FMT_FLT: { |
| 117 | 114 | const float *src = (const float *)buf->extended_data[0]; |
| 118 | 115 | |
| 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++) | |
| 121 | 118 | update_stat(s, &s->chstats[c], *src); |
| 122 | 119 | }} |
| 123 | 120 | break; |
| @@ -131,39 +128,42 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *buf) · 7 unmodified lines | ||
| 131 | 128 | static void print_stats(AVFilterContext *ctx) |
| 132 | 129 | { |
| 133 | 130 | DRMeterContext *s = ctx->priv; |
| 134 | − float dr = 0; | |
| 135 | − int ch; | |
| 131 | + float dr = 0.f; | |
| 136 | 132 | |
| 137 | − for (ch = 0; ch < s->nb_channels; ch++) { | |
| 133 | + for (int ch = 0; ch < s->nb_channels; ch++) { | |
| 138 | 134 | 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; | |
| 141 | 138 | |
| 142 | 139 | if (!p->nb_samples) { |
| 143 | 140 | av_log(ctx, AV_LOG_INFO, "No data, dynamic range not meassurable\n"); |
| 144 | 141 | return; |
| 145 | 142 | } |
| 146 | 143 | |
| 147 | − finish_block(p); | |
| 144 | + if (p->nb_samples) | |
| 145 | + finish_block(p); | |
| 148 | 146 | |
| 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; | |
| 152 | 151 | break; |
| 152 | + } | |
| 153 | 153 | first = 1; |
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | − secondpeak = (BINS - i) / (double)BINS; | |
| 157 | + secondpeak = peak_bin / (float)BINS; | |
| 158 | 158 | |
| 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--) { | |
| 160 | 160 | if (p->rms[i]) { |
| 161 | − rmssum += SQR(i / (double)BINS); | |
| 161 | + rmssum += SQR(i / (float)BINS) * p->rms[i]; | |
| 162 | 162 | j += p->rms[i]; |
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | − chdr = 20 * log10(secondpeak / sqrt(rmssum / (0.2 * p->blknum))); | |
| 166 | + chdr = 20.f * log10f(secondpeak / sqrtf(rmssum / (float)last)); | |
| 167 | 167 | dr += chdr; |
| 168 | 168 | av_log(ctx, AV_LOG_INFO, "Channel %d: DR: %g\n", ch + 1, chdr); |
| 169 | 169 | } |