ao_pulse: support native mute control
[mplayer.git] / libaf / af_stats.c
blob3d78f72faa2aed3ff6cc32dc2fc3e21846bb51f4
1 /*
2 * Copyright (C) 2009 Nicolas George <nicolas.george@normalesup.org>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include <math.h>
26 #include "af.h"
28 #define MAX_DB 80
29 #define MIN_VAL 1E-8
31 struct af_stats {
32 long long n_samples;
33 double tsquare;
34 int max;
35 long long histogram[65536];
38 static inline int logdb(double v)
40 if (v > 1)
41 return 0;
42 if (v <= MIN_VAL)
43 return MAX_DB - 1;
44 return log(v) / -0.23025850929940456840179914546843642076;
47 static int stats_init(af_instance_t *af, struct af_stats *s, af_data_t *data)
49 int i;
51 if (!data)
52 return AF_ERROR;
53 *(af->data) = *data;
54 af->data->format = AF_FORMAT_S16_NE;
55 af->data->bps = 2;
56 s->n_samples = 0;
57 s->tsquare = 0;
58 s->max = 0;
59 for (i = 0; i < 65536; i++)
60 s->histogram[i] = 0;
61 return af_test_output(af, data);
64 static void stats_print(struct af_stats *s)
66 int i;
67 long long sum;
68 float v;
69 long long h[MAX_DB];
71 s->tsquare /= 32768 * 32768;
72 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: n_samples: %lld\n", s->n_samples);
73 if (s->n_samples == 0)
74 return;
75 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: mean_volume: -%d dB\n",
76 logdb(s->tsquare / s->n_samples));
77 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: max_volume: -%d dB\n",
78 logdb(s->max / (32768.0 * 32768.0)));
79 for (i = 0; i < MAX_DB; i++)
80 h[i] = 0;
81 for (i = 0; i < 65536; i++) {
82 v = (i - 32768) / 32768.0;
83 h[logdb(v * v)] += s->histogram[i];
85 for (i = 0; i < MAX_DB; i++)
86 if (h[i] != 0)
87 break;
88 sum = 0;
89 for (; i < MAX_DB; i++) {
90 sum += h[i];
91 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: histogram_%ddb: %lld\n",
92 i, h[i]);
93 if (sum > s->n_samples / 1000)
94 break;
98 static int control(struct af_instance_s *af, int cmd, void *arg)
100 struct af_stats *s = af->setup;
102 switch(cmd) {
103 case AF_CONTROL_REINIT:
104 return stats_init(af, s, arg);
106 case AF_CONTROL_PRE_DESTROY:
107 stats_print(s);
108 return AF_OK;
110 return AF_UNKNOWN;
113 static void uninit(struct af_instance_s *af)
115 free(af->data);
116 free(af->setup);
119 static af_data_t *play(struct af_instance_s *af, af_data_t *data)
121 struct af_stats *s = af->setup;
122 int16_t *a, *aend;
123 int v, v2;
125 a = data->audio;
126 aend = (int16_t *)((char *)data->audio + data->len);
127 s->n_samples += aend - a;
128 for (; a < aend; a++) {
129 v = *a;
130 v2 = v * v;
131 s->tsquare += v2;
132 s->histogram[v + 32768]++;
133 if (v2 > s->max)
134 s->max = v2;
136 return data;
139 static int af_open(af_instance_t *af)
141 af->control = control;
142 af->uninit = uninit;
143 af->play = play;
144 af->mul = 1;
145 af->data = malloc(sizeof(af_data_t));
146 af->setup = malloc(sizeof(struct af_stats));
147 if (af->data == NULL || af->setup == NULL)
148 return AF_ERROR;
149 return AF_OK;
152 af_info_t af_info_stats = {
153 "Statistics audio filter",
154 "stats",
155 "Nicolas George",
158 af_open