Index: /dsfilters/temp/libmpcenc/src/bitstream.c
===================================================================
--- /dsfilters/temp/libmpcenc/src/bitstream.c	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/bitstream.c	(revision 398)
@@ -0,0 +1,170 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include <mpc/mpc_types.h>
+#include <mpc/datatypes.h>
+#include <mpc/mpcmath.h>
+
+#include "../include/libmpcenc.h"
+#include "psy_model.h"
+#include "bitstream.h"
+#include "encoder.h"
+
+#pragma warning(disable: 4244)	// warning C4244: '+=' : conversion from 'double' to 'float', possible loss of data
+
+#define MAX_ENUM 32
+
+static const mpc_uint32_t Cnk[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
+	{0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465},
+	{0, 0, 0, 1, 4, 10, 20, 35, 56, 84, 120, 165, 220, 286, 364, 455, 560, 680, 816, 969, 1140, 1330, 1540, 1771, 2024, 2300, 2600, 2925, 3276, 3654, 4060, 4495},
+	{0, 0, 0, 0, 1, 5, 15, 35, 70, 126, 210, 330, 495, 715, 1001, 1365, 1820, 2380, 3060, 3876, 4845, 5985, 7315, 8855, 10626, 12650, 14950, 17550, 20475, 23751, 27405, 31465},
+	{0, 0, 0, 0, 0, 1, 6, 21, 56, 126, 252, 462, 792, 1287, 2002, 3003, 4368, 6188, 8568, 11628, 15504, 20349, 26334, 33649, 42504, 53130, 65780, 80730, 98280, 118755, 142506, 169911},
+	{0, 0, 0, 0, 0, 0, 1, 7, 28, 84, 210, 462, 924, 1716, 3003, 5005, 8008, 12376, 18564, 27132, 38760, 54264, 74613, 100947, 134596, 177100, 230230, 296010, 376740, 475020, 593775, 736281},
+	{0, 0, 0, 0, 0, 0, 0, 1, 8, 36, 120, 330, 792, 1716, 3432, 6435, 11440, 19448, 31824, 50388, 77520, 116280, 170544, 245157, 346104, 480700, 657800, 888030, 1184040, 1560780, 2035800, 2629575},
+	{0, 0, 0, 0, 0, 0, 0, 0, 1, 9, 45, 165, 495, 1287, 3003, 6435, 12870, 24310, 43758, 75582, 125970, 203490, 319770, 490314, 735471, 1081575, 1562275, 2220075, 3108105, 4292145, 5852925, 7888725},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620, 92378, 167960, 293930, 497420, 817190, 1307504, 2042975, 3124550, 4686825, 6906900, 10015005, 14307150, 20160075},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 11, 66, 286, 1001, 3003, 8008, 19448, 43758, 92378, 184756, 352716, 646646, 1144066, 1961256, 3268760, 5311735, 8436285, 13123110, 20030010, 30045015, 44352165},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 78, 364, 1365, 4368, 12376, 31824, 75582, 167960, 352716, 705432, 1352078, 2496144, 4457400, 7726160, 13037895, 21474180, 34597290, 54627300, 84672315},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 13, 91, 455, 1820, 6188, 18564, 50388, 125970, 293930, 646646, 1352078, 2704156, 5200300, 9657700, 17383860, 30421755, 51895935, 86493225, 141120525},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 14, 105, 560, 2380, 8568, 27132, 77520, 203490, 497420, 1144066, 2496144, 5200300, 10400600, 20058300, 37442160, 67863915, 119759850, 206253075},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 15, 120, 680, 3060, 11628, 38760, 116280, 319770, 817190, 1961256, 4457400, 9657700, 20058300, 40116600, 77558760, 145422675, 265182525},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 136, 816, 3876, 15504, 54264, 170544, 490314, 1307504, 3268760, 7726160, 17383860, 37442160, 77558760, 155117520, 300540195},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 17, 153, 969, 4845, 20349, 74613, 245157, 735471, 2042975, 5311735, 13037895, 30421755, 67863915, 145422675, 300540195}
+};
+
+static const mpc_uint8_t Cnk_len[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
+	{0, 0, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9},
+	{0, 0, 0, 2, 4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 13, 13},
+	{0, 0, 0, 0, 3, 4, 6, 7, 7, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},
+	{0, 0, 0, 0, 0, 3, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18},
+	{0, 0, 0, 0, 0, 0, 3, 5, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20},
+	{0, 0, 0, 0, 0, 0, 0, 3, 6, 7, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 21, 22, 22},
+	{0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 19, 20, 21, 21, 22, 22, 23, 23, 23, 24},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 8, 10, 11, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 10, 12, 13, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 24, 25, 25, 26, 26},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 23, 24, 25, 26, 26, 27, 27},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 9, 11, 13, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 28},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 15, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 27, 28, 29},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 7, 10, 12, 14, 16, 17, 19, 20, 21, 23, 24, 25, 26, 27, 28, 28, 29},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 10, 12, 14, 16, 18, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 8, 10, 13, 15, 17, 18, 20, 21, 23, 24, 25, 27, 28, 29, 30}
+
+};
+
+static const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM] =
+{
+	{0, 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
+	{0, 0, 1, 2, 6, 1, 11, 4, 28, 19, 9, 62, 50, 37, 23, 8, 120, 103, 85, 66, 46, 25, 3, 236, 212, 187, 161, 134, 106, 77, 47, 16},
+	{0, 0, 0, 0, 6, 12, 29, 8, 44, 8, 91, 36, 226, 148, 57, 464, 344, 208, 55, 908, 718, 508, 277, 24, 1796, 1496, 1171, 820, 442, 36, 3697, 3232},
+	{0, 0, 0, 0, 3, 1, 29, 58, 2, 46, 182, 17, 309, 23, 683, 228, 1716, 1036, 220, 3347, 2207, 877, 7529, 5758, 3734, 1434, 15218, 12293, 9017, 5363, 1303, 29576},
+	{0, 0, 0, 0, 0, 2, 11, 8, 2, 4, 50, 232, 761, 46, 1093, 3824, 2004, 7816, 4756, 880, 12419, 6434, 31887, 23032, 12406, 65292, 50342, 32792, 12317, 119638, 92233, 60768},
+	{0, 0, 0, 0, 0, 0, 1, 4, 44, 46, 50, 100, 332, 1093, 3187, 184, 4008, 14204, 5636, 26776, 11272, 56459, 30125, 127548, 85044, 31914, 228278, 147548, 49268, 454801, 312295, 142384},
+	{0, 0, 0, 0, 0, 0, 0, 0, 28, 8, 182, 232, 332, 664, 1757, 4944, 13320, 944, 15148, 53552, 14792, 91600, 16987, 178184, 43588, 390776, 160546, 913112, 536372, 61352, 1564729, 828448},
+	{0, 0, 0, 0, 0, 0, 0, 0, 7, 19, 91, 17, 761, 1093, 1757, 3514, 8458, 21778, 55490, 5102, 58654, 204518, 33974, 313105, 1015577, 534877, 1974229, 1086199, 4096463, 2535683, 499883, 6258916},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 9, 36, 309, 46, 3187, 4944, 8458, 16916, 38694, 94184, 230358, 26868, 231386, 789648, 54177, 1069754, 3701783, 1481708, 6762211, 2470066, 13394357, 5505632},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 62, 226, 23, 1093, 184, 13320, 21778, 38694, 77388, 171572, 401930, 953086, 135896, 925544, 3076873, 8340931, 3654106, 13524422, 3509417, 22756699, 2596624},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 50, 148, 683, 3824, 4008, 944, 55490, 94184, 171572, 343144, 745074, 1698160, 3931208, 662448, 3739321, 12080252, 32511574, 12481564, 49545413, 5193248},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 37, 57, 228, 2004, 14204, 15148, 5102, 230358, 401930, 745074, 1490148, 3188308, 7119516, 16170572, 3132677, 15212929, 47724503, 127314931, 42642616},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 23, 464, 1716, 7816, 5636, 53552, 58654, 26868, 953086, 1698160, 3188308, 6376616, 13496132, 29666704, 66353813, 14457878, 62182381, 189497312},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 8, 344, 1036, 4756, 26776, 14792, 204518, 231386, 135896, 3931208, 7119516, 13496132, 26992264, 56658968, 123012781, 3252931, 65435312},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 208, 220, 880, 11272, 91600, 33974, 789648, 925544, 662448, 16170572, 29666704, 56658968, 113317936, 236330717, 508019104},
+	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 103, 55, 3347, 12419, 56459, 16987, 313105, 54177, 3076873, 3739321, 3132677, 66353813, 123012781, 236330717}
+};
+
+static const mpc_uint8_t log2[32] =
+{ 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6};
+
+static const mpc_uint8_t log2_lost[32] =
+{ 0, 1, 0, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 31};
+
+
+
+void mpc_bs_encode_size(mpc_bitstream *bs, mpc_uint64_t size, mpc_bool_t add_code_size)
+{
+	int				j, bytes = 1;
+	mpc_uint8_t		one_byte;
+
+	if (add_code_size) {
+		while ((1ull << (7 * bytes)) - bytes <= size) bytes++;
+		size += bytes;
+	} else {
+		while ((1ull << (7 * bytes)) <= size) bytes++;
+	}
+
+	// write individual bytes
+	for (j = 0; j<bytes-1; j++) {
+		one_byte = 0x80 | ((size >> (7*(bytes - 1 - j))) & 0x7f);
+		mpc_bs_putbits(bs, one_byte, 8);
+	}
+
+	one_byte = size & 0x7f;
+	mpc_bs_putbits(bs, one_byte, 8);
+}
+
+void mpc_bs_encode_golomb(mpc_bitstream *bs, mpc_uint32_t nb, mpc_uint_t k)
+{
+	unsigned int l = (nb >> k) + 1;
+	nb &= (1 << k) - 1;
+
+	while( l > 31 ){
+		mpc_bs_putbits(bs, 0, 31);
+		l -= 31;
+	}
+	mpc_bs_putbits(bs, 1, l);
+	mpc_bs_putbits(bs, nb, k);
+}
+
+void mpc_bs_encode_enum(mpc_bitstream *bs, const mpc_uint32_t bits, const mpc_uint_t N)
+{
+	mpc_uint32_t code = 0;
+	const mpc_uint32_t * C = Cnk[0];
+	unsigned int n = 0, k = 0;
+
+	for( ; n < N; n++){
+		if ((bits >> n) & 1) {
+			code += C[n];
+			C += MAX_ENUM;
+			k++;
+		}
+	}
+
+	if (k == 0) return;
+
+	if (code < Cnk_lost[k-1][n-1]) {
+		mpc_bs_putbits(bs, code, Cnk_len[k-1][n-1] - 1);
+	} else {
+		mpc_bs_putbits(bs, code + Cnk_lost[k-1][n-1], Cnk_len[k-1][n-1]);
+	}
+}
+
+void mpc_bs_encode_log(mpc_bitstream *bs, mpc_uint32_t value, mpc_uint32_t max)
+{
+	if (value < log2_lost[max - 1]) {
+		mpc_bs_putbits(bs, value, log2[max - 1] - 1);
+	} else {
+		mpc_bs_putbits(bs, value + log2_lost[max - 1], log2[max - 1]);
+	}
+}
Index: /dsfilters/temp/libmpcenc/src/bitstream.h
===================================================================
--- /dsfilters/temp/libmpcenc/src/bitstream.h	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/bitstream.h	(revision 398)
@@ -0,0 +1,80 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#pragma once
+
+
+
+typedef struct
+{
+	mpc_uint32_t	bitbuf;				// bitbuffer
+	mpc_uint8_t		*buf;				// byte buffer
+	int				bits;				// number of bits in bitbuffer
+} mpc_bitstream;
+
+
+static mpc_inline void mpc_bs_init(mpc_bitstream *bs, mpc_uint8_t *b)
+{
+	bs->bitbuf = 0;
+	bs->buf    = b;
+	bs->bits   = 0;
+}
+
+static mpc_inline void mpc_bs_putbits(mpc_bitstream *bs, mpc_uint32_t val, int num)
+{
+	bs->bits += num;
+	if (num < 32) val &= (1<<num)-1;
+
+	if (bs->bits > 32) {
+		bs->bits -= 32;
+		bs->bitbuf |= val >> (bs->bits);
+		*bs->buf++ = ( bs->bitbuf >> 24 ) & 0xff;
+		*bs->buf++ = ( bs->bitbuf >> 16 ) & 0xff;
+		*bs->buf++ = ( bs->bitbuf >>  8 ) & 0xff;
+		*bs->buf++ = ( bs->bitbuf >>  0 ) & 0xff;
+		bs->bitbuf = val << (32 - bs->bits);
+	} else
+	bs->bitbuf |= val << (32 - bs->bits);
+}
+
+static mpc_inline void mpc_bs_writebits(mpc_bitstream *bs)
+{
+	while (bs->bits >= 8) {
+		*bs->buf++ = (bs->bitbuf >> 24) & 0xff;
+		bs->bitbuf <<= 8;
+		bs->bits -= 8;
+	}
+}
+
+static mpc_inline void mpc_bs_bytealign(mpc_bitstream *bs, int digit)
+{
+	int	to_write = (8 - bs->bits & 0x07) & 0x07;
+	if (to_write > 0) {
+		mpc_bs_putbits(bs, (1 << to_write) - digit, to_write);
+	}
+}
+
+
+void mpc_bs_encode_size(mpc_bitstream *bs, mpc_uint64_t size, mpc_bool_t add_code_size);
+void mpc_bs_encode_enum(mpc_bitstream *bs, const mpc_uint32_t bits, const mpc_uint_t N);
+void mpc_bs_encode_log(mpc_bitstream *bs, mpc_uint32_t value, mpc_uint32_t max);
+void mpc_bs_encode_golomb(mpc_bitstream *bs, mpc_uint32_t nb, mpc_uint_t k);
+
+
+
Index: /dsfilters/temp/libmpcenc/src/crc32.c
===================================================================
--- /dsfilters/temp/libmpcenc/src/crc32.c	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/crc32.c	(revision 398)
@@ -0,0 +1,56 @@
+/*
+*  C Implementation: crc32
+*
+*  code from http://www.w3.org/TR/PNG/#D-CRCAppendix
+*
+*/
+
+/* Table of CRCs of all 8-bit messages. */
+static unsigned long crc_table[256];
+
+/* Flag: has the table been computed? Initially false. */
+static int crc_table_computed = 0;
+
+/* Make the table for a fast CRC. */
+static void make_crc_table(void)
+{
+	unsigned long c;
+	int n, k;
+
+	for (n = 0; n < 256; n++) {
+		c = (unsigned long) n;
+		for (k = 0; k < 8; k++) {
+			if (c & 1)
+				c = 0xedb88320L ^ (c >> 1);
+			else
+				c = c >> 1;
+		}
+		crc_table[n] = c;
+	}
+	crc_table_computed = 1;
+}
+
+
+/* Update a running CRC with the bytes buf[0..len-1]--the CRC
+	should be initialized to all 1's, and the transmitted value
+	is the 1's complement of the final running CRC (see the
+	crc() routine below). */
+
+static unsigned long update_crc(unsigned long crc, unsigned char *buf, int len)
+{
+	unsigned long c = crc;
+	int n;
+
+	if (!crc_table_computed)
+		make_crc_table();
+	for (n = 0; n < len; n++) {
+		c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
+	}
+	return c;
+}
+
+/* Return the CRC of the bytes buf[0..len-1]. */
+unsigned long crc32(unsigned char *buf, int len)
+{
+	return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
+}
Index: /dsfilters/temp/libmpcenc/src/enc_interface.c
===================================================================
--- /dsfilters/temp/libmpcenc/src/enc_interface.c	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/enc_interface.c	(revision 398)
@@ -0,0 +1,243 @@
+/*
+ * Musepack audio compression
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include <mpc/mpc_types.h>
+#include <mpc/datatypes.h>
+#include <mpc/mpcmath.h>
+
+#include "../include/libmpcenc.h"
+#include "psy_model.h"
+#include "bitstream.h"
+#include "encoder.h"
+#include "syntax_sv8.h"
+
+#pragma warning(disable: 4244)	// warning C4244: '+=' : conversion from 'double' to 'float', possible loss of data
+
+// constructor & destructor
+int mpc_encoder_open(mpc_encoder_t *instance)
+{
+	mpc_enc_t	*encoder;
+
+	if (!instance) return -1;
+
+	// new instance of the encoder
+	encoder = (mpc_enc_t*) malloc(sizeof(mpc_enc_t));
+	if (!encoder) {
+		return -1;
+	}
+
+	// everything is blank...
+	memset(encoder, 0, sizeof(mpc_enc_t));
+
+	*instance = (mpc_encoder_t)encoder;
+	return 0;
+}
+
+int mpc_encoder_close(mpc_encoder_t instance)
+{
+	mpc_enc_t	*encoder = (mpc_enc_t*)instance;
+
+	if (!instance) return 0;
+
+	// release buffers... etc.
+	if (encoder->buffer) {
+		free(encoder->buffer);
+		encoder->buffer = NULL;
+	}
+
+	free(encoder);
+	return 0;
+}
+
+// simple way to configure the encoder
+int mpc_encoder_config(mpc_encoder_t instance, int samplerate, int channels, int quality)
+{
+	mpc_enc_t				*encoder = (mpc_enc_t*)instance;
+	mpc_encoder_config_t	*cfg	 = NULL;
+
+	if (!encoder) return -1;
+
+	cfg = &encoder->config;
+	
+	// verify parameters
+	if (samplerate != 44100 &&
+		samplerate != 48000 &&
+		samplerate != 37800 &&
+		samplerate != 32000) return -2;
+
+	if (channels <= 0 ||
+		channels > 2) return -3;
+
+	if (quality < 0 ||
+		quality > 10) return -4;
+
+	// prepare default config with given quality
+	cfg->samplerate				= samplerate;
+	cfg->channels				= channels;
+	cfg->quality				= quality;
+	cfg->stream_version			= 8;
+	
+	cfg->enable_seek_table		= 0;
+	cfg->seek_power				= 2;
+	cfg->frames_per_block_power	= 6;
+
+	// preliminary initialization
+	mpc_psy_model_set_quality(&encoder->psy_model, quality);
+
+	// copy the values from PSY model
+	cfg->ns_order		 = encoder->psy_model.NS_Order;
+	cfg->var_ltq		 = encoder->psy_model.varLtq;
+	cfg->pns			 = encoder->psy_model.PNS;		
+	cfg->min_val_choice	 = encoder->psy_model.MinValChoice;
+	cfg->trans_detect	 = encoder->psy_model.TransDetect;
+	cfg->short_thr		 = encoder->psy_model.ShortThr;
+	cfg->nmt			 = encoder->psy_model.NMT;
+	cfg->tmn			 = encoder->psy_model.TMN;
+	cfg->cvd_used		 = encoder->psy_model.CVD_used;
+	cfg->ms_channel_mode = encoder->psy_model.MS_Channelmode;
+	cfg->min_smr		 = encoder->psy_model.minSMR;
+	cfg->tmp_mask_used	 = encoder->psy_model.tmpMask_used;
+
+	return 0;
+}
+
+
+int mpc_encoder_get_config(mpc_encoder_t instance, mpc_encoder_config_t **config)
+{
+	mpc_enc_t	*encoder = (mpc_enc_t*)instance;
+	
+	if (!encoder || !config) return -1;
+
+	// pointer to our config structure
+	*config = &encoder->config;
+	return 0;
+}
+
+
+int mpc_encoder_init(mpc_encoder_t instance)
+{
+	mpc_enc_t				*encoder = (mpc_enc_t*)instance;
+	mpc_encoder_config_t	*cfg	 = NULL;
+	PsyModel				*m		 = NULL;
+	
+	if (!encoder) return -1;
+
+	cfg = &encoder->config;
+	m   = &encoder->psy_model;
+
+	// now we need to calculate the buffer size
+	cfg->buffer_size	= (1 << cfg->frames_per_block_power) * sizeof(mpc_uint8_t) * MAX_FRAME_SIZE;
+
+	// alloc our own buffer
+	if (encoder->buffer) {
+		free(encoder->buffer);
+		encoder->buffer = NULL;
+	}
+	encoder->buffer = (mpc_uint8_t*)malloc(cfg->buffer_size);
+	if (!encoder->buffer) return -2;
+
+	// let the encoder know
+	encoder->frames_per_block_pwr	= cfg->frames_per_block_power;
+
+	// reflect the changes into the PSY model
+	m->NS_Order		  = cfg->ns_order;
+	m->varLtq		  = cfg->var_ltq;
+	m->PNS			  = cfg->pns;		
+	m->MinValChoice   = cfg->min_val_choice;
+	m->TransDetect	  = cfg->trans_detect;
+	m->ShortThr		  = cfg->short_thr;
+	m->NMT			  = cfg->nmt;
+	m->TMN			  = cfg->tmn;
+	m->CVD_used		  = cfg->cvd_used;
+	m->MS_Channelmode = cfg->ms_channel_mode ;
+	m->minSMR		  = cfg->min_smr;
+	m->tmpMask_used   = cfg->tmp_mask_used;
+
+	// also init the psy model
+	mpc_psy_model_init(&encoder->psy_model);
+
+	return 0;
+}
+
+
+// encoding PCM samples
+int mpc_encoder_encode_pcm(mpc_encoder_t instance, float *samples, int count, mpc_uint8_t *buf, int *bytes_written)
+{
+	return 0;
+}
+
+int mpc_encoder_encode_tail(mpc_encoder_t instance, mpc_uint8_t *buf, int *bytes_written)
+{
+	return 0;
+}
+
+
+int mpc_encoder_get_hdr_packet(mpc_encoder_t instance, int type, mpc_uint8_t *buf, int *bytes_written)
+{
+	mpc_enc_t		*encoder = (mpc_enc_t*)instance;
+	mpc_uint8_t		temp[1024];
+	mpc_bitstream	bs;
+
+	// output data must be valid
+	if (!encoder) return -1;
+	if (!buf || !bytes_written) return -1;
+
+	// prepare output bitstream
+	mpc_bs_init(&bs, temp);
+
+	switch (type) {
+	case PACKET_MAGIC:
+		{
+			mpc_bs_init(&bs, buf);
+			writeMagic(&bs);
+			*bytes_written = (bs.buf - temp);
+		}
+		break;
+
+	case PACKET_TYPE('S','H'):
+		{
+			writeStreamHeader(encoder, &bs);			
+			*bytes_written = writePacket(buf, type, temp, (bs.buf - temp), MPC_TRUE);
+		}
+		break;
+
+	case PACKET_TYPE('R','G'):
+		{
+			writeGainInfo(encoder, &bs);			
+			*bytes_written = writePacket(buf, type, temp, (bs.buf - temp), MPC_FALSE);
+		}
+		break;
+
+	case PACKET_TYPE('E','I'):
+		{
+			writeEncoderInfo(encoder, &bs);			
+			*bytes_written = writePacket(buf, type, temp, (bs.buf - temp), MPC_FALSE);
+		}
+		break;
+
+	// no other packet types
+	default:	return -1;
+	}
+
+	return 0;
+}
+
+
Index: /dsfilters/temp/libmpcenc/src/encoder.h
===================================================================
--- /dsfilters/temp/libmpcenc/src/encoder.h	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/encoder.h	(revision 398)
@@ -0,0 +1,87 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#pragma once
+
+
+// FIXME : define this somewhere else
+#ifndef NULL
+#define NULL 0
+#endif
+
+#define MPC_FRAME_LENGTH (36 * 32)
+#define MAX_FRAME_SIZE 4352
+
+
+
+typedef struct {
+	mpc_uint16_t		Code;        // >= 14 bit
+	mpc_uint16_t		Length;      // >=  4 bit
+} Huffman_t;
+
+typedef struct {
+
+	// bitstream
+	mpc_bitstream		bs;
+	mpc_uint8_t			*buffer;				// Buffer for audio packets
+
+	mpc_uint64_t		outputBits;				// Counter for the number of written bits in the bitstream
+
+	// total stats
+	mpc_uint64_t		samples_encoded;		// total number of encoded (and delivered) samples
+	mpc_uint64_t		samples_to_skip;		// number of samples to skip at start
+
+	mpc_uint_t			framesInBlock;			// Number of frames in current block
+	mpc_uint_t			frames_per_block_pwr;	// Number of frame in a block = 1 << frames_per_block_pwr
+
+	// seeking
+	mpc_uint32_t		*seek_table;
+	mpc_uint32_t		seek_pos;				/// current position in the seek table
+	mpc_uint32_t		seek_ref;				/// reference position for the seek information
+	mpc_uint32_t		seek_ptr;				/// position of the seek pointer block
+	mpc_uint32_t		seek_pwr;				/// keep a seek table entry every 2^seek_pwr block
+	mpc_uint32_t		block_cnt;				/// number of encoded blocks
+
+
+	mpc_uint32_t		MS_Channelmode;
+	mpc_uint32_t		Overflows;				//  = 0;      // number of internal (filterbank) clippings
+	mpc_uint32_t		MaxBand;				/// number of non zero bands in last frame
+
+ 	mpc_int32_t			SCF_Index_L [32] [3];
+	mpc_int32_t			SCF_Index_R [32] [3];	// holds scalefactor-indices
+	mpc_int32_t			SCF_Last_L  [32];
+	mpc_int32_t			SCF_Last_R  [32];		// Last coded SCF value
+	mpc_quantizer		Q [32];					// holds quantized samples
+	mpc_int32_t			Res_L [32];
+	mpc_int32_t			Res_R [32];				// holds the chosen quantizer for each subband
+	mpc_bool_t			DSCF_Flag_L [32];
+	mpc_bool_t			DSCF_Flag_R [32];		// differential SCF used?
+	mpc_bool_t			MS_Flag[32];			// MS used?
+
+	// configuration
+	mpc_encoder_config_t	config;
+
+	// psy model in use
+	PsyModel				psy_model;
+
+} mpc_enc_t;
+
+
+
+
Index: /dsfilters/temp/libmpcenc/src/syntax_sv8.c
===================================================================
--- /dsfilters/temp/libmpcenc/src/syntax_sv8.c	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/syntax_sv8.c	(revision 398)
@@ -0,0 +1,142 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include <mpc/mpc_types.h>
+#include <mpc/datatypes.h>
+#include <mpc/mpcmath.h>
+
+#include "../include/libmpcenc.h"
+#include "psy_model.h"
+#include "bitstream.h"
+#include "encoder.h"
+#include "syntax_sv8.h"
+
+unsigned long crc32(unsigned char *buf, int len);
+
+
+void writeMagic(mpc_bitstream *bs)
+{
+	mpc_bs_putbits(bs, 'M', 8);
+	mpc_bs_putbits(bs, 'P', 8);
+	mpc_bs_putbits(bs, 'C', 8);
+	mpc_bs_putbits(bs, 'K', 8);
+
+	mpc_bs_bytealign(bs, 0);
+	mpc_bs_writebits(bs);
+}
+
+int writePacket(mpc_uint8_t *outbuf, int code, mpc_uint8_t *payload, int payload_len, mpc_bool_t add_crc)
+{
+	mpc_bitstream	bs;
+	mpc_uint32_t	crc;
+
+	if (!outbuf) return 0;
+
+	mpc_bs_init(&bs, outbuf);
+	
+	//	All packets are composed as
+	//	KEY / SIZE / (optional CRC) / PAYLOAD
+
+	mpc_bs_putbits(&bs, code, 16);
+
+	if (add_crc) {
+		mpc_bs_encode_size(&bs, payload_len+4, MPC_TRUE);
+
+		crc = crc32((unsigned char *)payload, payload_len);
+		mpc_bs_putbits(&bs, crc, 32);
+
+	} else {
+		mpc_bs_encode_size(&bs, payload_len, MPC_TRUE);
+	}
+
+	mpc_bs_writebits(&bs);
+
+	// now copy the payload
+	if (payload_len > 0) {
+		memcpy(bs.buf, payload, payload_len);
+		bs.buf += payload_len;
+	}
+
+	// return the total length of packet in bytes
+	return (int)(bs.buf - outbuf);
+}
+
+
+
+void writeStreamHeader(mpc_enc_t *e, mpc_bitstream *bs)
+{
+	mpc_bs_putbits(bs, 8, 8);										// Stream Version
+
+	mpc_bs_encode_size(bs, e->samples_encoded, MPC_FALSE);			// total sample count
+	mpc_bs_encode_size(bs, e->samples_to_skip, MPC_FALSE);			// to skip at the beginning
+	
+	switch (e->config.samplerate) {
+	case 44100:		mpc_bs_putbits(bs, 0, 3);	break;
+	case 48000:		mpc_bs_putbits(bs, 1, 3);	break;
+	case 37800:		mpc_bs_putbits(bs, 2, 3);	break;
+	case 32000:		mpc_bs_putbits(bs, 3, 3);	break;
+	}
+
+	mpc_bs_putbits(bs, e->psy_model.Max_Band - 1, 5);				// max bandwidth
+	mpc_bs_putbits(bs, e->config.channels - 1, 4);					// channels
+	mpc_bs_putbits(bs, e->config.ms_channel_mode > 0, 1);			// MS-Coding Flag
+	mpc_bs_putbits(bs, e->config.frames_per_block_power >> 1, 3);	// frames per block (log4 unit)
+
+	mpc_bs_bytealign(bs, 0);
+	mpc_bs_writebits(bs);
+}
+
+void writeGainInfo(mpc_enc_t *e, mpc_bitstream *bs)
+{
+	mpc_bs_putbits(bs, 1,  8 );										// version
+	mpc_bs_putbits(bs, e->config.title_gain,  16 );					// Title gain
+	mpc_bs_putbits(bs, e->config.title_peak,  16 );					// Title peak
+	mpc_bs_putbits(bs, e->config.album_gain,  16 );					// Album gain
+	mpc_bs_putbits(bs, e->config.album_peak,  16 );					// Album peak
+
+	mpc_bs_bytealign(bs, 0);
+	mpc_bs_writebits(bs);
+}
+
+void writeEncoderInfo(mpc_enc_t *e, mpc_bitstream *bs)
+{
+	mpc_bs_putbits(bs, (mpc_uint32_t)(e->psy_model.FullQual * 8 + .5), 7);
+	mpc_bs_putbits(bs, e->psy_model.PNS > 0, 1);	
+	mpc_bs_putbits(bs, MPCENC_MAJOR, 8);	
+	mpc_bs_putbits(bs, MPCENC_MINOR, 8);	
+	mpc_bs_putbits(bs, MPCENC_BUILD, 8);	
+
+	mpc_bs_bytealign(bs, 0);
+	mpc_bs_writebits(bs);
+}
+
+void writeSeekTable(mpc_enc_t *e, mpc_bitstream *bs)
+{
+	// TODO: 
+}
+
+
+
+
+
+
+
Index: /dsfilters/temp/libmpcenc/src/syntax_sv8.h
===================================================================
--- /dsfilters/temp/libmpcenc/src/syntax_sv8.h	(revision 398)
+++ /dsfilters/temp/libmpcenc/src/syntax_sv8.h	(revision 398)
@@ -0,0 +1,32 @@
+/*
+ * Musepack audio compression
+ * Copyright (C) 1999-2004 Buschmann/Klemm/Piecha/Wolf
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#pragma once
+
+// assembles a SV8 packet from payload
+int writePacket(mpc_uint8_t *outbuf, int code, mpc_uint8_t *payload, int payload_len, mpc_bool_t add_crc);
+
+void writeMagic(mpc_bitstream *bs);
+
+void writeStreamHeader(mpc_enc_t *e, mpc_bitstream *bs);
+void writeGainInfo(mpc_enc_t *e, mpc_bitstream *bs);
+void writeEncoderInfo(mpc_enc_t *e, mpc_bitstream *bs);
+void writeSeekTable(mpc_enc_t *e, mpc_bitstream *bs);
+
+//void writeBitstream_SV8(mpc_enc_t *, int);
Index: /dsfilters/temp/libmpcenc/vcproj/libmpcenc.vcproj
===================================================================
--- /dsfilters/temp/libmpcenc/vcproj/libmpcenc.vcproj	(revision 397)
+++ /dsfilters/temp/libmpcenc/vcproj/libmpcenc.vcproj	(revision 398)
@@ -152,37 +152,5 @@
 			>
 			<File
-				RelativePath="..\src\ans.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\cvd.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\encoder.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\fft4g.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\fft_routines.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\profile.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\psy_model.c"
-				>
-			</File>
-			<File
-				RelativePath="..\src\psy_model.h"
-				>
-			</File>
-			<File
-				RelativePath="..\src\psy_tabs.c"
+				RelativePath="..\src\crc32.c"
 				>
 			</File>
@@ -203,4 +171,72 @@
 			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
 			>
+		</Filter>
+		<Filter
+			Name="PsyModel"
+			>
+			<File
+				RelativePath="..\src\ans.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\cvd.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\fft4g.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\fft_routines.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\profile.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\psy_model.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\psy_model.h"
+				>
+			</File>
+			<File
+				RelativePath="..\src\psy_tabs.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\syntax_sv8.c"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Encoder"
+			>
+			<File
+				RelativePath="..\src\bitstream.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\bitstream.h"
+				>
+			</File>
+			<File
+				RelativePath="..\src\enc_interface.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\encoder.c"
+				>
+			</File>
+			<File
+				RelativePath="..\src\encoder.h"
+				>
+			</File>
+			<File
+				RelativePath="..\src\syntax_sv8.h"
+				>
+			</File>
 		</Filter>
 	</Files>
