1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001 Josh Coalson
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.
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.
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.
32 pair_t buckets[FLAC__MAX_BLOCK_SIZE];
42 static subframe_stats_t all_;
44 static void init_stats(subframe_stats_t *stats);
45 static void update_stats(subframe_stats_t *stats, FLAC__int32 residual, unsigned incr);
46 static void compute_stats(subframe_stats_t *stats);
47 static FLAC__bool dump_stats(const subframe_stats_t *stats, const char *filename);
49 void flac__analyze_init(analysis_options aopts)
51 if(aopts.do_residual_gnuplot) {
56 void flac__analyze_frame(const FLAC__Frame *frame, unsigned frame_number, analysis_options aopts, FILE *fout)
58 const unsigned channels = frame->header.channels;
59 char outfilename[1024];
60 subframe_stats_t stats;
63 /* do the human-readable part first */
64 fprintf(fout, "frame=%u\tblocksize=%u\tsample_rate=%u\tchannels=%u\tchannel_assignment=%s\n", frame_number, frame->header.blocksize, frame->header.sample_rate, channels, FLAC__ChannelAssignmentString[frame->header.channel_assignment]);
65 for(channel = 0; channel < channels; channel++) {
66 const FLAC__Subframe *subframe = frame->subframes+channel;
67 fprintf(fout, "\tsubframe=%u\twasted_bits=%u\ttype=%s", channel, subframe->wasted_bits, FLAC__SubframeTypeString[subframe->type]);
68 switch(subframe->type) {
69 case FLAC__SUBFRAME_TYPE_CONSTANT:
70 fprintf(fout, "\tvalue=%d\n", subframe->data.constant.value);
72 case FLAC__SUBFRAME_TYPE_FIXED:
73 fprintf(fout, "\torder=%u\tpartition_order=%u\n", subframe->data.fixed.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order); /*@@@ assumes method is partitioned-rice */
74 for(i = 0; i < subframe->data.fixed.order; i++)
75 fprintf(fout, "\t\twarmup[%u]=%d\n", i, subframe->data.fixed.warmup[i]);
76 if(aopts.do_residual_text) {
77 for(i = 0; i < frame->header.blocksize-subframe->data.fixed.order; i++)
78 fprintf(fout, "\t\tresidual[%u]=%d\n", i, subframe->data.fixed.residual[i]);
81 case FLAC__SUBFRAME_TYPE_LPC:
82 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); /*@@@ assumes method is partitioned-rice */
83 for(i = 0; i < subframe->data.lpc.order; i++)
84 fprintf(fout, "\t\twarmup[%u]=%d\n", i, subframe->data.lpc.warmup[i]);
85 if(aopts.do_residual_text) {
86 for(i = 0; i < frame->header.blocksize-subframe->data.lpc.order; i++)
87 fprintf(fout, "\t\tresidual[%u]=%d\n", i, subframe->data.lpc.residual[i]);
90 case FLAC__SUBFRAME_TYPE_VERBATIM:
96 /* now do the residual distributions if requested */
97 if(aopts.do_residual_gnuplot) {
98 for(channel = 0; channel < channels; channel++) {
99 const FLAC__Subframe *subframe = frame->subframes+channel;
100 unsigned residual_samples;
104 switch(subframe->type) {
105 case FLAC__SUBFRAME_TYPE_FIXED:
106 residual_samples = frame->header.blocksize - subframe->data.fixed.order;
107 for(i = 0; i < residual_samples; i++)
108 update_stats(&stats, subframe->data.fixed.residual[i], 1);
110 case FLAC__SUBFRAME_TYPE_LPC:
111 residual_samples = frame->header.blocksize - subframe->data.lpc.order;
112 for(i = 0; i < residual_samples; i++)
113 update_stats(&stats, subframe->data.lpc.residual[i], 1);
120 for(i = 0; i < stats.nbuckets; i++) {
121 update_stats(&all_, stats.buckets[i].residual, stats.buckets[i].count);
124 /* write the subframe */
125 sprintf(outfilename, "f%06u.s%u.gp", frame_number, channel);
126 compute_stats(&stats);
128 (void)dump_stats(&stats, outfilename);
133 void flac__analyze_finish(analysis_options aopts)
135 if(aopts.do_residual_gnuplot) {
136 compute_stats(&all_);
137 (void)dump_stats(&all_, "all");
141 void init_stats(subframe_stats_t *stats)
143 stats->peak_index = -1;
150 void update_stats(subframe_stats_t *stats, FLAC__int32 residual, unsigned incr)
153 const double r = (double)residual, a = r*incr;
155 stats->nsamples += incr;
159 for(i = 0; i < stats->nbuckets; i++) {
160 if(stats->buckets[i].residual == residual) {
161 stats->buckets[i].count += incr;
165 /* not found, make a new bucket */
167 stats->buckets[i].residual = residual;
168 stats->buckets[i].count = incr;
171 if(stats->peak_index < 0 || stats->buckets[i].count > stats->buckets[stats->peak_index].count)
172 stats->peak_index = i;
175 void compute_stats(subframe_stats_t *stats)
177 stats->mean = stats->sum / (double)stats->nsamples;
178 stats->variance = (stats->sos - (stats->sum * stats->sum / stats->nsamples)) / stats->nsamples;
179 stats->stddev = sqrt(stats->variance);
182 FLAC__bool dump_stats(const subframe_stats_t *stats, const char *filename)
186 const double m = stats->mean;
187 const double s1 = stats->stddev, s2 = s1*2, s3 = s1*3, s4 = s1*4, s5 = s1*5, s6 = s1*6;
188 const double p = stats->buckets[stats->peak_index].count;
190 outfile = fopen(filename, "w");
193 fprintf(stderr, "ERROR opening %s\n", filename);
197 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");
199 for(i = 0; i < stats->nbuckets; i++) {
200 fprintf(outfile, "%d %u\n", stats->buckets[i].residual, stats->buckets[i].count);
202 fprintf(outfile, "e\n");
204 fprintf(outfile, "%f %f\ne\n", stats->mean, p);
205 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s1, p*0.8, m+s1, p*0.8);
206 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s2, p*0.7, m+s2, p*0.7);
207 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s3, p*0.6, m+s3, p*0.6);
208 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s4, p*0.5, m+s4, p*0.5);
209 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s5, p*0.4, m+s5, p*0.4);
210 fprintf(outfile, "%f %f\n%f %f\ne\n", m-s6, p*0.3, m+s6, p*0.3);
212 fprintf(outfile, "pause -1 'waiting...'\n");