Index: xmms-musepack/branches/r2d/src/libmpc.cpp
===================================================================
--- xmms-musepack/branches/r2d/src/libmpc.cpp	(revision 101)
+++ xmms-musepack/branches/r2d/src/libmpc.cpp	(revision 103)
@@ -2,22 +2,22 @@
  * Copyright (c) 2005, The Musepack Development Team
  * All rights reserved.
- * 
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
  * met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
- * 
+ *
  *     * Redistributions in binary form must reproduce the above
  *       copyright notice, this list of conditions and the following
  *       disclaimer in the documentation and/or other materials provided
  *       with the distribution.
- * 
+ *
  *     * Neither the name of the The Musepack Development Team nor the
  *       names of its contributors may be used to endorse or promote
  *       products derived from this software without specific prior
  *       written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@@ -327,11 +327,13 @@
 {
 	mpc_reader reader;
-	if (mpc_reader_init_stdio(&reader, p_Filename) == MPC_STATUS_OK) {
+	mpc_demux * demux;
+	if (mpc_reader_init_stdio(&reader, p_Filename) == MPC_STATUS_OK && (demux = mpc_demux_init(&reader))) {
         MpcInfo tags = getTags(p_Filename);
         *p_Title = mpcGenerateTitle(tags, p_Filename);
         freeTags(tags);
-        mpc_streaminfo info;
-		mpc_streaminfo_init(&info, &reader);
-        *p_Length = static_cast<int> (1000 * mpc_streaminfo_get_length(&info));
+		mpc_streaminfo info;
+		mpc_demux_get_info(demux, &info);
+		*p_Length = static_cast<int> (1000 * mpc_streaminfo_get_length(&info));
+		mpc_demux_exit(demux);
 		mpc_reader_exit_stdio(&reader);
     } else {
@@ -522,7 +524,8 @@
 
 		mpc_reader reader;
-		if (mpc_reader_init_stdio(&reader, p_Filename) == MPC_STATUS_OK) {
+		mpc_demux * demux;
+		if (mpc_reader_init_stdio(&reader, p_Filename) == MPC_STATUS_OK && (demux = mpc_demux_init(&reader))) {
             mpc_streaminfo info;
-			mpc_streaminfo_init(&info, &reader);
+			mpc_demux_get_info(demux, &info);
 
             int time = static_cast<int> (mpc_streaminfo_get_length(&info));
@@ -559,4 +562,5 @@
             free(entry);
             freeTags(tags);
+			mpc_demux_exit(demux);
 			mpc_reader_exit_stdio(&reader);
         }
@@ -750,32 +754,29 @@
         mpcDecoder.isError = g_strdup_printf("[xmms-musepack] decodeStream is unable to open %s", filename);
         return endThread(0, true);
-    }
-
-    mpc_streaminfo info;
-	if (mpc_streaminfo_init(&info, &reader) != MPC_STATUS_OK) {
-        mpcDecoder.isError = g_strdup_printf("[xmms-musepack] decodeStream is unable to read %s", filename);
+	}
+
+	mpc_demux * demux = 0;
+	demux = mpc_demux_init(&reader);
+
+	if ( demux == 0 ) {
+		mpcDecoder.isError = g_strdup_printf("[xmms-musepack] decodeStream is unable to initialize decoder on %s", filename);
 		mpc_reader_exit_stdio(&reader);
-        return endThread(0, true);
-    }
-
+		return endThread(0, true);
+	}
+
+	mpc_streaminfo info;
+	mpc_demux_get_info(demux, &info);
     MpcInfo tags     = getTags(filename);
     track.display    = mpcGenerateTitle(tags, filename);
-    track.length     = static_cast<int> (1000 * mpc_streaminfo_get_length(&info));
-    track.bitrate    = static_cast<int> (info.average_bitrate);
-    track.sampleFreq = info.sample_freq;
-    track.channels   = info.channels;
+	track.length     = static_cast<int> (1000 * mpc_streaminfo_get_length(&info));
+	track.bitrate    = static_cast<int> (info.average_bitrate);
+	track.sampleFreq = info.sample_freq;
+	track.channels   = info.channels;
     freeTags(tags);
 
     MpcPlugin.set_info(track.display, track.length, track.bitrate, track.sampleFreq, track.channels);
 
-    mpc_decoder * decoder;
-	if (mpc_decoder_init(&decoder, &reader, &info) != MPC_STATUS_OK) {
-        mpcDecoder.isError = g_strdup_printf("[xmms-musepack] decodeStream is unable to initialize decoder on %s", filename);
-		mpc_reader_exit_stdio(&reader);
-        return endThread(0, true);
-    }
-
-	mpc_decoder_set_seeking(decoder, &info, pluginConfig.fastSeek);
-    setReplaygain(info, *decoder);
+// 	mpc_decoder_set_seeking(decoder, &info, pluginConfig.fastSeek);
+//     setReplaygain(info, *decoder);
 
     MPC_SAMPLE_FORMAT sampleBuffer[MPC_DECODER_BUFFER_LENGTH];
@@ -795,10 +796,11 @@
     lockRelease();
 
-    int counter = 2 * track.sampleFreq / 3;
+	int counter = 2 * track.sampleFreq / 3;
+	unsigned status = 1;
     while (isAlive())
     {
         if (getOffset() != -1)
         {
-            mpc_decoder_seek_seconds(decoder, mpcDecoder.offset);
+//             mpc_decoder_seek_seconds(decoder, mpcDecoder.offset);
             setOffset(-1);
         }
@@ -807,48 +809,39 @@
         short iPlaying = MpcPlugin.output->buffer_playing()? 1 : 0;
         int iFree = MpcPlugin.output->buffer_free();
-        if (!mpcDecoder.isPause &&  iFree >= ((1152 * 4) << iPlaying))
+        if (!mpcDecoder.isPause &&  iFree >= ((1152 * 4) << iPlaying) && status != 0)
         {
-            unsigned status = processBuffer(sampleBuffer, xmmsBuffer, *decoder);
-            if (status == (unsigned) (-1))
-            {
-				mpcDecoder.isError = g_strdup_printf("[xmms-musepack] error from internal decoder on %s", filename);
-				mpc_reader_exit_stdio(&reader);
-                return endThread(0, true);
-            }
-	    else if (status == 0)
-            {
-                //mpcDecoder.isError = g_strdup_printf("[xmms-musepack] null output from internal decoder on %s", filename);
-				mpc_reader_exit_stdio(&reader);
-                return endThread(0, true);
-            }
-            lockRelease();
-
-            if(pluginConfig.dynamicBitrate)
-            {
-                counter -= status;
-                if(counter < 0)
-                {
-                    MpcPlugin.set_info(track.display, track.length, track.bitrate, track.sampleFreq, track.channels);
-                    counter = 2 * track.sampleFreq / 3;
-                }
-            }
+			status = processBuffer(sampleBuffer, xmmsBuffer, *demux);
+			lockRelease();
+
+			if(pluginConfig.dynamicBitrate) {
+				counter -= status;
+				if(counter < 0) {
+					MpcPlugin.set_info(track.display, track.length, track.bitrate, track.sampleFreq, track.channels);
+					counter = 2 * track.sampleFreq / 3;
+				}
+			}
         }
         else
         {
-            lockRelease();
+			lockRelease();
+			if (!mpcDecoder.isPause && status == 0 && MpcPlugin.output->output_time() == 0)
+				break;
             xmms_usleep(10000);
-        }
+		}
 	}
+	mpc_demux_exit(demux);
 	mpc_reader_exit_stdio(&reader);
     return endThread(0, false);
 }
 
-static int processBuffer(MPC_SAMPLE_FORMAT* sampleBuffer, char* xmmsBuffer, mpc_decoder& decoder)
+static int processBuffer(MPC_SAMPLE_FORMAT* sampleBuffer, char* xmmsBuffer, mpc_demux& demux)
 {
     mpc_uint32_t vbrAcc = 0;
     mpc_uint32_t vbrUpd = 0;
-
-    unsigned status  = mpc_decoder_decode(&decoder, sampleBuffer, &vbrAcc, &vbrUpd);
-    copyBuffer(sampleBuffer, xmmsBuffer, status);
+	mpc_frame_info info;
+
+	info.buffer = sampleBuffer;
+	mpc_demux_decode(&demux, &info);
+    copyBuffer(sampleBuffer, xmmsBuffer, info.samples);
 
     if (pluginConfig.dynamicBitrate)
@@ -859,10 +852,10 @@
     if (pluginConfig.isEq)
     {
-        iir(xmmsBuffer, 4 * status);
-    }
-
-    MpcPlugin.add_vis_pcm(MpcPlugin.output->written_time(), FMT_S16_LE, track.channels, status * 4, xmmsBuffer);
-    MpcPlugin.output->write_audio(xmmsBuffer, status * 4);
-    return status;
+		iir(xmmsBuffer, 4 * info.samples);
+    }
+
+	MpcPlugin.add_vis_pcm(MpcPlugin.output->written_time(), FMT_S16_LE, track.channels, info.samples * 4, xmmsBuffer);
+	MpcPlugin.output->write_audio(xmmsBuffer, info.samples * 4);
+	return info.samples;
 }
 
