annotate libaf/af_stats.c @ 28661:20787bd5c506

Add statistics audio filter that prints information about the audio stream. patch by Nicolas George, nicolas.george normalesup org
author diego
date Sat, 21 Feb 2009 21:27:27 +0000
parents
children 8e9f6dfd7580
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28661
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
1 /*
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
2 * Copyright (C) 2009 Nicolas George <nicolas.george@normalesup.org>
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
3 *
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
4 * This file is part of MPlayer.
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
5 *
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
6 * MPlayer is free software; you can redistribute it and/or modify
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
7 * it under the terms of the GNU General Public License as published by
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
9 * (at your option) any later version.
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
10 *
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
11 * MPlayer is distributed in the hope that it will be useful,
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
14 * GNU General Public License for more details.
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
15 *
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License along
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
19 */
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
20
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
21 #include <stdio.h>
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
22 #include <stdlib.h>
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
23 #include <inttypes.h>
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
24 #include <math.h>
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
25
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
26 #include "af.h"
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
27
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
28 #define MAX_DB 80
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
29 #define MIN_VAL 1E-8
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
30
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
31 struct af_stats
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
32 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
33 long long n_samples;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
34 double tsquare;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
35 int max;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
36 long long histogram[65536];
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
37 };
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
38
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
39 static inline int logdb(double v)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
40 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
41 return v > 1 ? 0 : v <= MIN_VAL ? MAX_DB - 1 :
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
42 log(v) / -0.23025850929940456840179914546843642076;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
43 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
44
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
45 static int stats_init(af_instance_t *af, struct af_stats *s, af_data_t *data)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
46 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
47 int i;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
48
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
49 if (!data)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
50 return AF_ERROR;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
51 *(af->data) = *data;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
52 af->data->format = AF_FORMAT_S16_NE;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
53 af->data->bps = 2;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
54 s->n_samples = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
55 s->tsquare = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
56 s->max = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
57 for (i = 0; i < 65536; i++)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
58 s->histogram[i] = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
59 return af_test_output(af, data);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
60 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
61
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
62 static void stats_print(struct af_stats *s)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
63 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
64 int i;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
65 long long sum;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
66 float v;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
67 long long h[MAX_DB];
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
68
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
69 s->tsquare /= 32768 * 32768;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
70 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: n_samples: %lld\n", s->n_samples);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
71 if (s->n_samples == 0)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
72 return;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
73 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: mean_volume: -%d dB\n",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
74 logdb(s->tsquare / s->n_samples));
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
75 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats: max_volume: -%d dB\n",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
76 logdb(s->max / 32768.0));
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
77 for (i = 0; i < MAX_DB; i++)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
78 h[i] = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
79 for (i = 0; i < 65536; i++) {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
80 v = (i - 32768) / 32768.0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
81 h[logdb(v * v)] += s->histogram[i];
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
82 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
83 for (i = 0; i < MAX_DB; i++)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
84 if (h[i] != 0)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
85 break;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
86 sum = 0;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
87 for (; i < MAX_DB; i++) {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
88 sum += h[i];
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
89 mp_msg(MSGT_AFILTER, MSGL_INFO, "stats:histogram_%ddb: %lld\n",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
90 i, h[i]);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
91 if (sum > s->n_samples / 1000)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
92 break;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
93 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
94 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
95
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
96 static int control(struct af_instance_s *af, int cmd, void *arg)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
97 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
98 struct af_stats *s = (struct af_stats *)af->setup;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
99
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
100 switch(cmd) {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
101 case AF_CONTROL_REINIT:
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
102 return stats_init(af, s, arg);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
103
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
104 case AF_CONTROL_PRE_DESTROY:
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
105 stats_print(s);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
106 return AF_OK;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
107 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
108 return AF_UNKNOWN;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
109 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
110
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
111 static void uninit(struct af_instance_s *af)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
112 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
113 if (af->data)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
114 free(af->data);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
115 if (af->setup)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
116 free(af->setup);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
117 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
118
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
119 static af_data_t *play(struct af_instance_s *af, af_data_t *data)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
120 {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
121 struct af_stats *s = (struct af_stats *)af->setup;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
122 int16_t *a, *aend;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
123 int v, v2;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
124
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
125 a = data->audio;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
126 aend = (int16_t *)((char *)data->audio + data->len);
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
127 s->n_samples += aend - a;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
128 for (; a < aend; a++) {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
129 v = *a;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
130 v2 = v * v;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
131 s->tsquare += v2;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
132 s->histogram[v + 32768]++;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
133 if (v2 > s->max)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
134 s->max = v2;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
135 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
136 return data;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
137 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
138
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
139 static int af_open(af_instance_t* af){
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
140 af->control = control;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
141 af->uninit = uninit;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
142 af->play = play;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
143 af->mul = 1;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
144 af->data = malloc(sizeof(af_data_t));
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
145 af->setup = malloc(sizeof(struct af_stats));
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
146 if (af->data == NULL || af->setup == NULL)
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
147 return AF_ERROR;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
148 return AF_OK;
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
149 }
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
150
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
151 af_info_t af_info_stats = {
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
152 "Statistics audio filter",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
153 "stats",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
154 "Nicolas George",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
155 "",
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
156 0,
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
157 af_open
20787bd5c506 Add statistics audio filter that prints information about the audio stream.
diego
parents:
diff changeset
158 };