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.
35 long long histogram
[65536];
38 static inline int logdb(double v
)
44 return log(v
) / -0.23025850929940456840179914546843642076;
47 static int stats_init(af_instance_t
*af
, struct af_stats
*s
, af_data_t
*data
)
54 af
->data
->format
= AF_FORMAT_S16_NE
;
59 for (i
= 0; i
< 65536; i
++)
61 return af_test_output(af
, data
);
64 static void stats_print(struct af_stats
*s
)
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)
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
++)
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
++)
89 for (; i
< MAX_DB
; i
++) {
91 mp_msg(MSGT_AFILTER
, MSGL_INFO
, "stats: histogram_%ddb: %lld\n",
93 if (sum
> s
->n_samples
/ 1000)
98 static int control(struct af_instance_s
*af
, int cmd
, void *arg
)
100 struct af_stats
*s
= af
->setup
;
103 case AF_CONTROL_REINIT
:
104 return stats_init(af
, s
, arg
);
106 case AF_CONTROL_PRE_DESTROY
:
113 static void uninit(struct af_instance_s
*af
)
119 static af_data_t
*play(struct af_instance_s
*af
, af_data_t
*data
)
121 struct af_stats
*s
= af
->setup
;
126 aend
= (int16_t *)((char *)data
->audio
+ data
->len
);
127 s
->n_samples
+= aend
- a
;
128 for (; a
< aend
; a
++) {
132 s
->histogram
[v
+ 32768]++;
139 static int af_open(af_instance_t
*af
)
141 af
->control
= control
;
145 af
->data
= malloc(sizeof(af_data_t
));
146 af
->setup
= malloc(sizeof(struct af_stats
));
147 if (af
->data
== NULL
|| af
->setup
== NULL
)
152 af_info_t af_info_stats
= {
153 "Statistics audio filter",