fix compiler warnings
[flac.git] / src / flac / analyze.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "FLAC/all.h"
29 #include "analyze.h"
30
31 typedef struct {
32         FLAC__int32 residual;
33         unsigned count;
34 } pair_t;
35
36 typedef struct {
37         pair_t buckets[FLAC__MAX_BLOCK_SIZE];
38         int peak_index;
39         unsigned nbuckets;
40         unsigned nsamples;
41         double sum, sos;
42         double variance;
43         double mean;
44         double stddev;
45 } subframe_stats_t;
46
47 static subframe_stats_t all_;
48
49 static void init_stats(subframe_stats_t *stats);
50 static void update_stats(subframe_stats_t *stats, FLAC__int32 residual, unsigned incr);
51 static void compute_stats(subframe_stats_t *stats);
52 static FLAC__bool dump_stats(const subframe_stats_t *stats, const char *filename);
53
54 void flac__analyze_init(analysis_options aopts)
55 {
56         if(aopts.do_residual_gnuplot) {
57                 init_stats(&all_);
58         }
59 }
60
61 void flac__analyze_frame(const FLAC__Frame *frame, unsigned frame_number, unsigned frame_bytes, analysis_options aopts, FILE *fout)
62 {
63         const unsigned channels = frame->header.channels;
64         char outfilename[1024];
65         subframe_stats_t stats;
66         unsigned i, channel;
67
68         /* do the human-readable part first */
69         fprintf(fout, "frame=%u\tbits=%u\tblocksize=%u\tsample_rate=%u\tchannels=%u\tchannel_assignment=%s\n", frame_number, frame_bytes*8, frame->header.blocksize, frame->header.sample_rate, channels, FLAC__ChannelAssignmentString[frame->header.channel_assignment]);
70         for(channel = 0; channel < channels; channel++) {
71                 const FLAC__Subframe *subframe = frame->subframes+channel;
72                 fprintf(fout, "\tsubframe=%u\twasted_bits=%u\ttype=%s", channel, subframe->wasted_bits, FLAC__SubframeTypeString[subframe->type]);
73                 switch(subframe->type) {
74                         case FLAC__SUBFRAME_TYPE_CONSTANT:
75                                 fprintf(fout, "\tvalue=%d\n", subframe->data.constant.value);
76                                 break;
77                         case FLAC__SUBFRAME_TYPE_FIXED:
78                                 FLAC__ASSERT(subframe->data.fixed.entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE);
79                                 fprintf(fout, "\torder=%u\tpartition_order=%u\n", subframe->data.fixed.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order);
80                                 for(i = 0; i < subframe->data.fixed.order; i++)
81                                         fprintf(fout, "\t\twarmup[%u]=%d\n", i, subframe->data.fixed.warmup[i]);
82                                 if(aopts.do_residual_text) {
83                                         const unsigned partitions = (1u << subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order);
84                                         for(i = 0; i < partitions; i++) {
85                                                 unsigned parameter = subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents->parameters[i];
86                                                 if(parameter == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
87                                                         fprintf(fout, "\t\tparameter[%u]=ESCAPE, raw_bits=%u\n", i, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.contents->raw_bits[i]);
88                                                 else
89                                                         fprintf(fout, "\t\tparameter[%u]=%u\n", i, parameter);
90                                         }
91                                         for(i = 0; i < frame->header.blocksize-subframe->data.fixed.order; i++)
92                                                 fprintf(fout, "\t\tresidual[%u]=%d\n", i, subframe->data.fixed.residual[i]);
93                                 }
94                                 break;
95                         case FLAC__SUBFRAME_TYPE_LPC:
96                                 FLAC__ASSERT(subframe->data.lpc.entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE);
97                                 fprintf(fout, "\torder=%u\tpartition_order=%u\tqlp_coeff_precision=%u\tquantization_level=%d\n", subframe->data.lpc.order, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order, subframe->data.lpc.qlp_coeff_precision, subframe->data.lpc.quantization_level);
98                                 for(i = 0; i < subframe->data.lpc.order; i++)
99                                         fprintf(fout, "\t\tqlp_coeff[%u]=%d\n", i, subframe->data.lpc.qlp_coeff[i]);
100                                 for(i = 0; i < subframe->data.lpc.order; i++)
101                                         fprintf(fout, "\t\twarmup[%u]=%d\n", i, subframe->data.lpc.warmup[i]);
102                                 if(aopts.do_residual_text) {
103                                         const unsigned partitions = (1u << subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order);
104                                         for(i = 0; i < partitions; i++) {
105                                                 unsigned parameter = subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents->parameters[i];
106                                                 if(parameter == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER)
107                                                         fprintf(fout, "\t\tparameter[%u]=ESCAPE, raw_bits=%u\n", i, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.contents->raw_bits[i]);
108                                                 else
109                                                         fprintf(fout, "\t\tparameter[%u]=%u\n", i, parameter);
110                                         }
111                                         for(i = 0; i < frame->header.blocksize-subframe->data.lpc.order; i++)
112                                                 fprintf(fout, "\t\tresidual[%u]=%d\n", i, subframe->data.lpc.residual[i]);
113                                 }
114                                 break;
115                         case FLAC__SUBFRAME_TYPE_VERBATIM:
116                                 fprintf(fout, "\n");
117                                 break;
118                 }
119         }
120
121         /* now do the residual distributions if requested */
122         if(aopts.do_residual_gnuplot) {
123                 for(channel = 0; channel < channels; channel++) {
124                         const FLAC__Subframe *subframe = frame->subframes+channel;
125                         unsigned residual_samples;
126
127                         init_stats(&stats);
128
129                         switch(subframe->type) {
130                                 case FLAC__SUBFRAME_TYPE_FIXED:
131                                         residual_samples = frame->header.blocksize - subframe->data.fixed.order;
132                                         for(i = 0; i < residual_samples; i++)
133                                                 update_stats(&stats, subframe->data.fixed.residual[i], 1);
134                                         break;
135                                 case FLAC__SUBFRAME_TYPE_LPC:
136                                         residual_samples = frame->header.blocksize - subframe->data.lpc.order;
137                                         for(i = 0; i < residual_samples; i++)
138                                                 update_stats(&stats, subframe->data.lpc.residual[i], 1);
139                                         break;
140                                 default:
141                                         break;
142                         }
143
144                         /* update all_ */
145                         for(i = 0; i < stats.nbuckets; i++) {
146                                 update_stats(&all_, stats.buckets[i].residual, stats.buckets[i].count);
147                         }
148
149                         /* write the subframe */
150                         sprintf(outfilename, "f%06u.s%u.gp", frame_number, channel);
151                         compute_stats(&stats);
152
153                         (void)dump_stats(&stats, outfilename);
154                 }
155         }
156 }
157
158 void flac__analyze_finish(analysis_options aopts)
159 {
160         if(aopts.do_residual_gnuplot) {
161                 compute_stats(&all_);
162                 (void)dump_stats(&all_, "all");
163         }
164 }
165
166 void init_stats(subframe_stats_t *stats)
167 {
168         stats->peak_index = -1;
169         stats->nbuckets = 0;
170         stats->nsamples = 0;
171         stats->sum = 0.0;
172         stats->sos = 0.0;
173 }
174
175 void update_stats(subframe_stats_t *stats, FLAC__int32 residual, unsigned incr)
176 {
177         unsigned i;
178         const double r = (double)residual, a = r*incr;
179
180         stats->nsamples += incr;
181         stats->sum += a;
182         stats->sos += (a*r);
183
184         for(i = 0; i < stats->nbuckets; i++) {
185                 if(stats->buckets[i].residual == residual) {
186                         stats->buckets[i].count += incr;
187                         goto find_peak;
188                 }
189         }
190         /* not found, make a new bucket */
191         i = stats->nbuckets;
192         stats->buckets[i].residual = residual;
193         stats->buckets[i].count = incr;
194         stats->nbuckets++;
195 find_peak:
196         if(stats->peak_index < 0 || stats->buckets[i].count > stats->buckets[stats->peak_index].count)
197                 stats->peak_index = i;
198 }
199
200 void compute_stats(subframe_stats_t *stats)
201 {
202         stats->mean = stats->sum / (double)stats->nsamples;
203         stats->variance = (stats->sos - (stats->sum * stats->sum / stats->nsamples)) / stats->nsamples;
204         stats->stddev = sqrt(stats->variance);
205 }
206
207 FLAC__bool dump_stats(const subframe_stats_t *stats, const char *filename)
208 {
209         FILE *outfile;
210         unsigned i;
211         const double m = stats->mean;
212         const double s1 = stats->stddev, s2 = s1*2, s3 = s1*3, s4 = s1*4, s5 = s1*5, s6 = s1*6;
213         const double p = stats->buckets[stats->peak_index].count;
214
215         outfile = fopen(filename, "w");
216
217         if(0 == outfile) {
218                 fprintf(stderr, "ERROR opening %s: %s\n", filename, strerror(errno));
219                 return false;
220         }
221
222         fprintf(outfile, "plot '-' title 'PDF', '-' title 'mean' with impulses, '-' title '1-stddev' with histeps, '-' title '2-stddev' with histeps, '-' title '3-stddev' with histeps, '-' title '4-stddev' with histeps, '-' title '5-stddev' with histeps, '-' title '6-stddev' with histeps\n");
223
224         for(i = 0; i < stats->nbuckets; i++) {
225                 fprintf(outfile, "%d %u\n", stats->buckets[i].residual, stats->buckets[i].count);
226         }
227         fprintf(outfile, "e\n");
228
229         fprintf(outfile, "%f %f\ne\n", stats->mean, p);
230         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s1, p*0.8, m+s1, p*0.8);
231         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s2, p*0.7, m+s2, p*0.7);
232         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s3, p*0.6, m+s3, p*0.6);
233         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s4, p*0.5, m+s4, p*0.5);
234         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s5, p*0.4, m+s5, p*0.4);
235         fprintf(outfile, "%f %f\n%f %f\ne\n", m-s6, p*0.3, m+s6, p*0.3);
236
237         fprintf(outfile, "pause -1 'waiting...'\n");
238
239         fclose(outfile);
240         return true;
241 }