Index: libmpcdec/branches/zorg/src/streaminfo.c
===================================================================
--- libmpcdec/branches/zorg/src/streaminfo.c	(revision 63)
+++ libmpcdec/branches/zorg/src/streaminfo.c	(revision 68)
@@ -32,70 +32,61 @@
   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
-
 /// \file streaminfo.c
 /// Implementation of streaminfo reading functions.
 
-#include <mpcdec/mpcdec.h>
-#include <mpcdec/internal.h>
+#include <mpcdec/streaminfo.h>
+#include <stdio.h>
+#include "internal.h"
+
+static const char na[] = "n.a.";
+static const char *versionNames[] = {
+    na, "'Unstable/Experimental'", na, na, na, "'quality 0'", "'quality 1'", 
+    "'Telephone'", "'Thumb'", "'Radio'", "'Standard'", "'Extreme'", "'Insane'",
+    "'BrainDead'", "'quality 9'", "'quality 10'"
+};
+static const mpc_int32_t samplefreqs[4] = { 44100, 48000, 37800, 32000 };
 
 static const char *
-Stringify(mpc_uint32_t profile) // profile is 0...15, where 7...13 is used
+mpc_get_version_string(mpc_uint32_t profile) // profile is 0...15, where 7...13 is used
 {
-    static const char na[] = "n.a.";
-    static const char *Names[] = {
-        na, "'Unstable/Experimental'", na, na,
-        na, "'quality 0'", "'quality 1'", "'Telephone'",
-        "'Thumb'", "'Radio'", "'Standard'", "'Xtreme'",
-        "'Insane'", "'BrainDead'", "'quality 9'", "'quality 10'"
-    };
-
-    return profile >= sizeof(Names) / sizeof(*Names) ? na : Names[profile];
+    return profile >= sizeof versionNames / sizeof *versionNames ? na : versionNames[profile];
 }
 
 void
-mpc_streaminfo_init(mpc_streaminfo * si)
+mpc_streaminfo_init(mpc_streaminfo* si)
 {
-    memset(si, 0, sizeof(mpc_streaminfo));
+    memset(si, 0, sizeof *si);
 }
 
 /// Reads streaminfo from SV7 header. 
-static mpc_int32_t
-streaminfo_read_header_sv7(mpc_streaminfo * si, mpc_uint32_t HeaderData[8])
+static mpc_status
+streaminfo_read_header_sv7(mpc_streaminfo* si, mpc_uint32_t HeaderData[8])
 {
-    const mpc_int32_t samplefreqs[4] = { 44100, 48000, 37800, 32000 };
-
-    //mpc_uint32_t    HeaderData [8];
     mpc_uint16_t Estimatedpeak_title = 0;
 
     if (si->stream_version > 0x71) {
         //        Update (si->stream_version);
-        return 0;
+        return MPC_STATUS_OK;
     }
 
-    /*
-       if ( !fp->seek ( si->header_position ) )         // seek to header start
-       return ERROR_CODE_FILE;
-       if ( fp->read ( HeaderData, sizeof HeaderData) != sizeof HeaderData )
-       return ERROR_CODE_FILE;
-     */
-
-    si->bitrate = 0;
-    si->frames = HeaderData[1];
-    si->is = 0;
-    si->ms = (HeaderData[2] >> 30) & 0x0001;
-    si->max_band = (HeaderData[2] >> 24) & 0x003F;
-    si->block_size = 1;
-    si->profile = (HeaderData[2] << 8) >> 28;
-    si->profile_name = Stringify(si->profile);
-    si->sample_freq = samplefreqs[(HeaderData[2] >> 16) & 0x0003];
-    Estimatedpeak_title = (mpc_uint16_t) (HeaderData[2] & 0xFFFF);   // read the ReplayGain data
-    si->gain_title = (mpc_uint16_t) ((HeaderData[3] >> 16) & 0xFFFF);
-    si->peak_title = (mpc_uint16_t) (HeaderData[3] & 0xFFFF);
-    si->gain_album = (mpc_uint16_t) ((HeaderData[4] >> 16) & 0xFFFF);
-    si->peak_album = (mpc_uint16_t) (HeaderData[4] & 0xFFFF);
-    si->is_true_gapless = (HeaderData[5] >> 31) & 0x0001; // true gapless: used?
-    si->last_frame_samples = (HeaderData[5] >> 20) & 0x07FF;  // true gapless: valid samples for last frame
-    si->fast_seek = (HeaderData[5] >> 19) & 0x0001;  // fast seeking
-    si->encoder_version = (HeaderData[6] >> 24) & 0x00FF;
+    si->bitrate            = 0;
+    si->frames             = HeaderData[1];
+    si->is                 = 0;
+    si->ms                 = (HeaderData[2] >> 30) & 0x0001;
+    si->max_band           = (HeaderData[2] >> 24) & 0x003F;
+    si->block_size         = 1;
+    si->profile            = (HeaderData[2] << 8) >> 28;
+    si->profile_name       = mpc_get_version_string(si->profile);
+    si->sample_freq        = samplefreqs[(HeaderData[2] >> 16) & 0x0003];
+    Estimatedpeak_title    = (mpc_uint16_t) (HeaderData[2] & 0xFFFF);   // read the ReplayGain data
+    si->gain_title         = (mpc_uint16_t) ((HeaderData[3] >> 16) & 0xFFFF);
+    si->peak_title         = (mpc_uint16_t) (HeaderData[3] & 0xFFFF);
+    si->gain_album         = (mpc_uint16_t) ((HeaderData[4] >> 16) & 0xFFFF);
+    si->peak_album         = (mpc_uint16_t) (HeaderData[4] & 0xFFFF);
+    si->is_true_gapless    = (HeaderData[5] >> 31) & 0x0001; // true gapless: used?
+    si->last_frame_samples = (HeaderData[5] >> 20) & 0x07FF; // true gapless: valid samples for last frame
+    si->fast_seek          = (HeaderData[5] >> 19) & 0x0001; // fast seeking
+    si->encoder_version    = (HeaderData[6] >> 24) & 0x00FF;
+    si->channels           = 2;
 
     if (si->encoder_version == 0) {
@@ -108,8 +99,5 @@
                     si->encoder_version / 10 % 10);
             break;
-        case 2:
-        case 4:
-        case 6:
-        case 8:
+        case 2: case 4: case 6: case 8:
             sprintf(si->encoder, "Beta %u.%02u", si->encoder_version / 100,
                     si->encoder_version % 100);
@@ -122,138 +110,57 @@
     }
 
-    //    if ( si->peak_title == 0 )                                      // there is no correct peak_title contained within header
-    //        si->peak_title = (mpc_uint16_t)(Estimatedpeak_title * 1.18);
-    //    if ( si->peak_album == 0 )
-    //        si->peak_album = si->peak_title;                          // no correct peak_album, use peak_title
-
-    //si->sample_freq    = 44100;                                     // AB: used by all files up to SV7
-    si->channels = 2;
-
-    return ERROR_CODE_OK;
+    return MPC_STATUS_OK;
 }
 
-// read information from SV4-SV6 header
-#ifdef MPC_SUPPORT_SV456
-static mpc_int32_t
-streaminfo_read_header_sv6(mpc_streaminfo * si, mpc_uint32_t HeaderData[8])
+// reads file header and tags
+mpc_status
+mpc_streaminfo_read(mpc_streaminfo * si, mpc_reader *p_reader)
 {
-    //mpc_uint32_t    HeaderData [8];
-
-    /*
-       if ( !fp->seek (  si->header_position ) )         // seek to header start
-       return ERROR_CODE_FILE;
-       if ( fp->read ( HeaderData, sizeof HeaderData ) != sizeof HeaderData )
-       return ERROR_CODE_FILE;
-     */
-
-    si->bitrate = (HeaderData[0] >> 23) & 0x01FF;   // read the file-header (SV6 and below)
-    si->is = (HeaderData[0] >> 22) & 0x0001;
-    si->ms = (HeaderData[0] >> 21) & 0x0001;
-    si->stream_version = (HeaderData[0] >> 11) & 0x03FF;
-    si->max_band = (HeaderData[0] >> 6) & 0x001F;
-    si->block_size = (HeaderData[0]) & 0x003F;
-    si->profile = 0;
-    si->profile_name = Stringify((mpc_uint32_t) (-1));
-    if (si->stream_version >= 5)
-        si->frames = HeaderData[1]; // 32 bit
-    else
-        si->frames = (HeaderData[1] >> 16); // 16 bit
-
-    si->gain_title = 0;          // not supported
-    si->peak_title = 0;
-    si->gain_album = 0;
-    si->peak_album = 0;
-
-    si->last_frame_samples = 0;
-    si->is_true_gapless = 0;
-
-    si->encoder_version = 0;
-    si->encoder[0] = '\0';
-
-    if (si->stream_version == 7)
-        return ERROR_CODE_SV7BETA;  // are there any unsupported parameters used?
-    if (si->bitrate != 0)
-        return ERROR_CODE_CBR;
-    if (si->is != 0)
-        return ERROR_CODE_IS;
-    if (si->block_size != 1)
-        return ERROR_CODE_BLOCKSIZE;
-
-    if (si->stream_version < 6) // Bugfix: last frame was invalid for up to SV5
-        si->frames -= 1;
-
-    si->sample_freq = 44100;     // AB: used by all files up to SV7
-    si->channels = 2;
-
-    if (si->stream_version < 4 || si->stream_version > 7)
-        return ERROR_CODE_INVALIDSV;
-
-    return ERROR_CODE_OK;
-}
-#endif
-// reads file header and tags
-mpc_int32_t
-mpc_streaminfo_read(mpc_streaminfo * si, mpc_reader * r)
-{
-    mpc_uint32_t HeaderData[8];
-    mpc_int32_t Error = 0;
+    mpc_uint32_t headerData[8]; int err;
 
     // get header position
-    if ((si->header_position = JumpID3v2(r)) < 0) {
-        return ERROR_CODE_FILE;
+    err = si->header_position = mpc_skip_id3v2(p_reader);
+    if(err < 0) return MPC_STATUS_FILE;
+
+    // seek to first byte of mpc data
+    err = p_reader->seek(p_reader, si->header_position);
+    if(!err) return MPC_STATUS_FILE;
+    err = p_reader->read(p_reader, headerData, 8 * 4);
+    if(err != 8 * 4) return MPC_STATUS_FILE;
+    err = p_reader->seek(p_reader, si->header_position + 6 * 4);
+    if(!err) return MPC_STATUS_FILE;
+
+    err = p_reader->get_size(p_reader);
+    if(err < 0) return MPC_STATUS_FILE;
+    si->tag_offset = si->total_file_length = err;
+
+    err = memcmp(headerData, "MP+", 3);
+    if(!err) return MPC_STATUS_INVALIDSV;
+
+#ifndef MPC_LITTLE_ENDIAN
+    {
+        mpc_uint32_t ptr;
+        for (ptr = 0; ptr < 8; ptr++)
+            headerData[ptr] = mpc_swap32(headerData[ptr]);
     }
-    // seek to first byte of mpc data
-    if (!r->seek(r->data, si->header_position)) {
-        return ERROR_CODE_FILE;
-    }
-    if (r->read(r->data, HeaderData, 8 * 4) != 8 * 4) {
-        return ERROR_CODE_FILE;
-    }
-    if (!r->seek(r->data, si->header_position + 6 * 4)) {
-        return ERROR_CODE_FILE;
-    }
+#endif
+     si->stream_version = headerData[0] >> 24;
 
-    si->total_file_length = r->get_size(r->data);
-    si->tag_offset = si->total_file_length;
-    if (memcmp(HeaderData, "MP+", 3) == 0) {
-#ifndef MPC_LITTLE_ENDIAN
-        mpc_uint32_t ptr;
-        for (ptr = 0; ptr < 8; ptr++) {
-            HeaderData[ptr] = mpc_swap32(HeaderData[ptr]);
-        }
-#endif
-        si->stream_version = HeaderData[0] >> 24;
-
-        // stream version 8
-        if ((si->stream_version & 15) >= 8) {
-            return ERROR_CODE_INVALIDSV;
-        }
-        // stream version 7
-        else if ((si->stream_version & 15) == 7) {
-            Error = streaminfo_read_header_sv7(si, HeaderData);
-            if (Error != ERROR_CODE_OK) return Error;
-        }
-    } else {
-#ifdef MPC_SUPPORT_SV456
-        // stream version 4-6
-        Error = streaminfo_read_header_sv6(si, HeaderData);
-#else
-        return ERROR_CODE_INVALIDSV;
-#endif
-    }
+     if ((si->stream_version & 15) >= 8)
+        return MPC_STATUS_INVALIDSV;
+     else if ((si->stream_version & 15) == 7)
+     {
+        err = streaminfo_read_header_sv7(si, headerData);
+        if(err < 0) return err;
+     }
 
     // estimation, exact value needs too much time
-    si->pcm_samples = 1152 * si->frames - 576;
+    si->pcm_samples     = 1152 * si->frames - 576;
+    si->average_bitrate = 0;
+    if (si->pcm_samples > 0)
+        si->average_bitrate = (si->tag_offset  - si->header_position) * 8.0 
+                            *  si->sample_freq / si->pcm_samples;
 
-    if (si->pcm_samples > 0) {
-        si->average_bitrate =
-            (si->tag_offset -
-             si->header_position) * 8.0 * si->sample_freq / si->pcm_samples;
-    }
-    else {
-        si->average_bitrate = 0;
-    }
-
-    return ERROR_CODE_OK;
+    return MPC_STATUS_OK;
 }
 
@@ -261,6 +168,5 @@
 mpc_streaminfo_get_length(mpc_streaminfo * si)
 {
-    return (double)mpc_streaminfo_get_length_samples(si) /
-        (double)si->sample_freq;
+    return (double) mpc_streaminfo_get_length_samples(si) / si->sample_freq;
 }
 
@@ -269,10 +175,8 @@
 {
     mpc_int64_t samples = (mpc_int64_t) si->frames * MPC_FRAME_LENGTH;
-    if (si->is_true_gapless) {
+    if (si->is_true_gapless)
         samples -= (MPC_FRAME_LENGTH - si->last_frame_samples);
-    }
-    else {
+    else
         samples -= MPC_DECODER_SYNTH_DELAY;
-    }
     return samples;
 }
