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