typedefstructVolDetectContext { /** * Number of samples at each PCM value. * histogram[0x8000 + i] is the number of samples at value i. * The extra element is there for symmetry. */ // S16 范围是 -32768 ~ 32767,即 65536 个数。histogram 统计每个采样的数量,为了和数组的索引匹配,会将所有采样都加 32768(0x8000)。 // histogram 是采样值与其数量的关系。 uint64_t histogram[0x10001]; } VolDetectContext;
staticintfilter_frame(AVFilterLink *inlink, AVFrame *samples) { AVFilterContext *ctx = inlink->dst; VolDetectContext *vd = ctx->priv; int nb_samples = samples->nb_samples; int nb_channels = samples->channels; int nb_planes = nb_channels; int plane, i; int16_t *pcm;
if (!av_sample_fmt_is_planar(samples->format)) { nb_samples *= nb_channels; nb_planes = 1; } // 统计每个采样值的采样数。 for (plane = 0; plane < nb_planes; plane++) { pcm = (int16_t *)samples->extended_data[plane]; for (i = 0; i < nb_samples; i++) vd->histogram[pcm[i] + 0x8000]++; }
staticinlinedoublelogdb(uint64_t v) { // 由于传入的 v 是 Amplitude 值加了 0x8000 再进行了平方,这里做相关逆运算。 double d = v / (double)(0x8000 * 0x8000); if (!v) return MAX_DB; return -log10(d) * 10; }
staticvoidprint_stats(AVFilterContext *ctx) { VolDetectContext *vd = ctx->priv; int i, max_volume, shift; uint64_t nb_samples = 0, power = 0, nb_samples_shift = 0, sum = 0; uint64_t histdb[MAX_DB + 1] = { 0 };
// 其实总的采样数 nb_samples 可以定义在 VolDetectContext 中,在 filter_frame 进行计算以避免本次循环。 for (i = 0; i < 0x10000; i++) nb_samples += vd->histogram[i]; av_log(ctx, AV_LOG_INFO, "n_samples: %"PRId64"\n", nb_samples); if (!nb_samples) return;
/* If nb_samples > 1<<34, there is a risk of overflow in the multiplication or the sum: shift all histogram values to avoid that. The total number of samples must be recomputed to avoid rounding errors. */ shift = av_log2(nb_samples >> 33); for (i = 0; i < 0x10000; i++) { nb_samples_shift += vd->histogram[i] >> shift; power += (i - 0x8000) * (i - 0x8000) * (vd->histogram[i] >> shift); } if (!nb_samples_shift) return; power = (power + nb_samples_shift / 2) / nb_samples_shift; av_assert0(power <= 0x8000 * 0x8000); av_log(ctx, AV_LOG_INFO, "mean_volume: %.1f dB\n", -logdb(power));
// histdb: dB 直方图。用于保存 0dB ~ 91dB 的采样数。 for (i = 0; i < 0x10000; i++) histdb[(int)logdb((i - 0x8000) * (i - 0x8000))] += vd->histogram[i]; // 不输出整个直方图,并且忽略采样数为 0 的条目。 for (i = 0; i <= MAX_DB && !histdb[i]; i++); for (; i <= MAX_DB && sum < nb_samples / 1000; i++) { av_log(ctx, AV_LOG_INFO, "histogram_%ddb: %"PRId64"\n", i, histdb[i]); sum += histdb[i]; } }
// 统计每个采样的数量。 ulong nb_samples = length / sizeof(short); for (var i = offset; i < nb_samples; i++) { var sample = BitConverter.ToInt16(raw, i * sizeof(short)); histogram[sample + 0x8000]++; }
ulong power = 0, nb_samples_shift = 0;
/* If nb_samples > 1<<34, there is a risk of overflow in the multiplication or the sum: shift all histogram values to avoid that. The total number of samples must be recomputed to avoid rounding errors. */ int shift = (int)Math.Log(nb_samples >> 33, 2); for (var i = 0; i < 0x10000; i++) { nb_samples_shift += histogram[i] >> shift; power += (ulong)(i - 0x8000) * (ulong)(i - 0x8000) * (histogram[i] >> shift); } if (nb_samples_shift == 0) { maxVolume = 0; meanVolume = 0; returnnew List<KeyValuePair<int, ulong>>(0); }
power = (power + nb_samples_shift / 2) / nb_samples_shift;