Remove an overambitious sanity check in the fecdec filter.
[paraslash.git] / mp3_afh.c
index b809e8b3611d278d41b6ad7f965aa547389e52d3..2d18d5e5de7df641f6cbe7b7ad937cb3f2d69e3e 100644 (file)
--- a/mp3_afh.c
+++ b/mp3_afh.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2003-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -82,7 +82,7 @@ static char *get_latin1(id3_ucs4_t const *string)
        return (char *)id3_ucs4_latin1duplicate(string);
 }
 
-char *get_stringlist(union id3_field *field)
+static char *get_stringlist(union id3_field *field)
 {
        unsigned int k, nstrings = id3_field_getnstrings(field);
        char *result = NULL;
@@ -394,8 +394,8 @@ 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;
-       int ret, len = 0, old_br = -1, vbr = 0;
+       uint64_t freq_sum = 0, br_sum = 0;
+       int fl = 0, ret, len = 0, old_br = -1, vbr = 0;
        struct timeval total_time = {0, 0};
        unsigned chunk_table_size = 1000; /* gets increased on demand */
        off_t fpos = 0;
@@ -403,15 +403,23 @@ static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
        char *taginfo;
 
        afhi->chunks_total = 0;
-       afhi->chunk_table = para_malloc(chunk_table_size * sizeof(size_t));
+       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;
                struct timeval tmp, cct; /* current chunk time */
                fpos += len;
                len = find_valid_start(map, numbytes, &fpos, &header);
-               if (len <= 0)
+               if (len <= 0) {
+                       size_t end;
+                       ret = -E_MP3_INFO;
+                       if (!afhi->chunks_total)
+                               goto err_out;
+                       end = afhi->chunk_table[afhi->chunks_total - 1] + fl;
+                       afhi->chunk_table[afhi->chunks_total]
+                               = PARA_MIN(end, numbytes);
                        break;
+               }
                ret = header_frequency(&header);
                if (ret < 0)
                        continue;
@@ -429,35 +437,24 @@ 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(size_t));
+                               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 (!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);