]> git.tue.mpg.de Git - paraslash.git/commitdiff
mp3: Avoid rounding errors during bitrate calculation.
authorAndre Noll <maan@systemlinux.org>
Sun, 15 Mar 2009 17:59:48 +0000 (18:59 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 15 Mar 2009 17:59:48 +0000 (18:59 +0100)
Just add up the bitrates of all frames and divide by the number
of frames afterwards. By using uint64_t for the sum, we should
be safe wrt. overflows.

mp3_afh.c

index 932a772101e234165ca96aff85a9f684b07597cc..8eec436cffd3fed1c3f8c2f59a7468ebc17c7788 100644 (file)
--- a/mp3_afh.c
+++ b/mp3_afh.c
@@ -394,7 +394,7 @@ static int find_valid_start(unsigned char *map, size_t numbytes, off_t *fpos,
 static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
                struct afh_info *afhi)
 {
-       long freq_avg = 0, br_avg = 0;
+       uint64_t freq_sum = 0, br_sum = 0;
        int ret, len = 0, old_br = -1, vbr = 0;
        struct timeval total_time = {0, 0};
        unsigned chunk_table_size = 1000; /* gets increased on demand */
@@ -406,7 +406,7 @@ static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
        afhi->chunk_table = para_malloc(chunk_table_size * sizeof(uint32_t));
        taginfo = mp3_get_id3(map, numbytes, fd);
        while (1) {
-               unsigned long freq, br, fl;
+               int freq, br, fl;
                struct timeval tmp, cct; /* current chunk time */
                fpos += len;
                len = find_valid_start(map, numbytes, &fpos, &header);
@@ -429,35 +429,25 @@ static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
                tv_divide(br * 125, &tmp, &cct);
                tv_add(&cct, &total_time, &tmp);
                total_time = tmp;
-               //PARA_DEBUG_LOG("%s: br: %d, freq: %d, fl: %d, cct: %lu\n", __func__, br, freq, fl, cct.tv_usec);
                if (afhi->chunks_total >= chunk_table_size) {
                        chunk_table_size *= 2;
                        afhi->chunk_table = para_realloc(afhi->chunk_table,
                                chunk_table_size * sizeof(uint32_t));
                }
                afhi->chunk_table[afhi->chunks_total] = fpos;
-//             if (afhi->chunks_total < 10 || !(afhi->chunks_total % 1000))
-//                     PARA_INFO_LOG("chunk #%lu: %zd\n", afhi->chunks_total,
-//                             afhi->chunk_table[afhi->chunks_total]);
                afhi->chunks_total++;
-               if (afhi->chunks_total == 1) {
-                       freq_avg = freq;
-                       br_avg = br;
-                       old_br = br;
-                       continue;
-               }
-               freq_avg += ((long)freq - freq_avg) / ((long)afhi->chunks_total + 1);
-               br_avg += ((long)br - br_avg) / ((long)afhi->chunks_total + 1);
-               if (old_br != br)
+               freq_sum += freq;
+               br_sum += br;
+               if (afhi->chunks_total != 1 && old_br != br)
                        vbr = 1;
                old_br = br;
        }
        ret = -E_MP3_INFO;
-       if (!afhi->chunks_total || !freq_avg || !br_avg)
+       if (!afhi->chunks_total || !freq_sum || !br_sum)
                goto err_out;
        afhi->chunk_table[afhi->chunks_total] = numbytes - 1;
-       afhi->bitrate = br_avg;
-       afhi->frequency = freq_avg;
+       afhi->bitrate = br_sum / afhi->chunks_total;
+       afhi->frequency = freq_sum / afhi->chunks_total;
        afhi->channels = header_channels(&header);
        afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
        tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);