First draft of the libosl patch series.
[paraslash.git] / mp3_afh.c
index ebb6bf5..2d18d5e 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.
  */
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -71,21 +71,6 @@ static const int mp3info_bitrate[2][3][14] = {
 static const int frame_size_index[] = {24000, 72000, 72000};
 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
 
 static const int frame_size_index[] = {24000, 72000, 72000};
 static const char *mode_text[] = {"stereo", "joint stereo", "dual channel", "mono", "invalid"};
 
-static char *make_taginfo(char *title, char *artist, char *album, char *year,
-               char *comment)
-{
-       return make_message("%s: %s, by %s\n" /* taginfo1 */
-               "%s: A: %s, Y: %s, C: %s\n", /* taginfo 2*/
-               status_item_list[SI_TAGINFO1],
-               (title && *title)? title : "(title tag not set)",
-               (artist && *artist)? artist : "(artist tag not set)",
-               status_item_list[SI_TAGINFO2],
-               (album && *album)?  album : "(album tag not set)",
-               (year && *year)? year : "????",
-               (comment && *comment)? comment : "(comment tag not set)"
-       );
-}
-
 #ifdef HAVE_LIBID3TAG
 
 #include <id3tag.h>
 #ifdef HAVE_LIBID3TAG
 
 #include <id3tag.h>
@@ -97,7 +82,7 @@ static char *get_latin1(id3_ucs4_t const *string)
        return (char *)id3_ucs4_latin1duplicate(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;
 {
        unsigned int k, nstrings = id3_field_getnstrings(field);
        char *result = NULL;
@@ -295,7 +280,7 @@ static int frame_length(struct mp3header *header)
 
 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
 {
 
 static int compare_headers(struct mp3header *h1,struct mp3header *h2)
 {
-       if ((*(uint*)h1) == (*(uint*)h2))
+       if ((*(unsigned int*)h1) == (*(unsigned int*)h2))
                return 1;
        if ((h1->version == h2->version) &&
                        (h1->layer == h2->layer) &&
                return 1;
        if ((h1->version == h2->version) &&
                        (h1->layer == h2->layer) &&
@@ -409,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)
 {
 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;
        struct timeval total_time = {0, 0};
        unsigned chunk_table_size = 1000; /* gets increased on demand */
        off_t fpos = 0;
@@ -418,15 +403,23 @@ static int mp3_read_info(unsigned char *map, size_t numbytes, int fd,
        char *taginfo;
 
        afhi->chunks_total = 0;
        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) {
        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);
                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;
                        break;
+               }
                ret = header_frequency(&header);
                if (ret < 0)
                        continue;
                ret = header_frequency(&header);
                if (ret < 0)
                        continue;
@@ -444,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;
                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,
                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;
                }
                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++;
                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;
                        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;
                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);
        afhi->channels = header_channels(&header);
        afhi->seconds_total = (tv2ms(&total_time) + 500) / 1000;
        tv_divide(afhi->chunks_total, &total_time, &afhi->chunk_tv);