File: ACM.cpp

package info (click to toggle)
lame 3.100-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 7,368 kB
  • sloc: ansic: 33,554; cpp: 8,221; sh: 5,127; makefile: 472; pascal: 215; perl: 33; xml: 13
file content (1405 lines) | stat: -rw-r--r-- 48,588 bytes parent folder | download | duplicates (10)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
/**
 *
 * Lame ACM wrapper, encode/decode MP3 based RIFF/AVI files in MS Windows
 *
 *  Copyright (c) 2002 Steve Lhomme <steve.lhomme at free.fr>
 *
 * 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
 *
 */
 
/*!
	\author Steve Lhomme
	\version \$Id: ACM.cpp,v 1.20 2007/05/17 22:25:40 robert Exp $
*/

#if !defined(STRICT)
#define STRICT
#endif // STRICT

#include <algorithm>

#include <windows.h>
#include <windowsx.h>
#include <intshcut.h>

#include <mmreg.h>
#include <msacm.h>
#include <msacmdrv.h>

#include <assert.h>

#include <lame.h>

#include "adebug.h"
#include "resource.h"
#include "ACMStream.h"

#ifdef ENABLE_DECODING
#include "DecodeStream.h"
#endif // ENABLE_DECODING

#include "ACM.h"

#ifndef IDC_HAND
#define IDC_HAND            MAKEINTRESOURCE(32649)
#endif // IDC_HAND

char ACM::VersionString[120];

const char ACM_VERSION[] = "0.9.2";

#ifdef WIN32
//
//  32-bit versions
//
#if (WINVER >= 0x0400)
 #define VERSION_ACM_DRIVER  MAKE_ACM_VERSION(4,  0, 0)
#else
#define VERSION_ACM_DRIVER  MAKE_ACM_VERSION(3, 51, 0)
#endif
#define VERSION_MSACM MAKE_ACM_VERSION(3, 50, 0)

#else
//
//  16-bit versions
//
#define VERSION_ACM_DRIVER MAKE_ACM_VERSION(1, 0, 0)
#define VERSION_MSACM MAKE_ACM_VERSION(2, 1, 0)

#endif

#define PERSONAL_FORMAT WAVE_FORMAT_MPEGLAYER3
#define SIZE_FORMAT_STRUCT sizeof(MPEGLAYER3WAVEFORMAT)
//#define SIZE_FORMAT_STRUCT 0

//static const char channel_mode[][13] = {"mono","stereo","joint stereo","dual channel"};
static const char channel_mode[][13] = {"mono","stereo"};
static const unsigned int mpeg1_freq[] = {48000,44100,32000};
static const unsigned int mpeg2_freq[] = {24000,22050,16000,12000,11025,8000};
static const unsigned int mpeg1_bitrate[] = {320, 256, 224, 192, 160, 128, 112, 96, 80, 64, 56, 48, 40, 32};
static const unsigned int mpeg2_bitrate[] = {160, 144, 128, 112,  96,  80,  64, 56, 48, 40, 32, 24, 16,  8};

#define SIZE_CHANNEL_MODE (sizeof(channel_mode)  / (sizeof(char) * 13))
#define SIZE_FREQ_MPEG1 (sizeof(mpeg1_freq)    / sizeof(unsigned int))
#define SIZE_FREQ_MPEG2 (sizeof(mpeg2_freq)    / sizeof(unsigned int))
#define SIZE_BITRATE_MPEG1 (sizeof(mpeg1_bitrate) / sizeof(unsigned int))
#define SIZE_BITRATE_MPEG2 (sizeof(mpeg2_bitrate) / sizeof(unsigned int))

static const int FORMAT_TAG_MAX_NB = 2; // PCM and PERSONAL (mandatory to have at least PCM and your format)
static const int FILTER_TAG_MAX_NB = 0; // this is a codec, not a filter

// number of supported PCM formats
static const int FORMAT_MAX_NB_PCM =
	2 *                                           // number of PCM channel mode (stereo/mono)
		(SIZE_FREQ_MPEG1 + // number of MPEG 1 sampling freq
		SIZE_FREQ_MPEG2); // number of MPEG 2 sampling freq

//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
bool bitrate_item::operator<(const bitrate_item & other_bitrate) const
{
	return (other_bitrate.frequency < frequency ||
		    (other_bitrate.frequency == frequency &&
			 (other_bitrate.bitrate < bitrate ||
			  (other_bitrate.bitrate == bitrate &&
			   (other_bitrate.channels < channels)))));
}

//////////////////////////////////////////////////////////////////////
// Configuration Dialog
//////////////////////////////////////////////////////////////////////
/*
static CALLBACK ConfigProc(
  HWND hwndDlg,  // handle to dialog box
UINT uMsg,     // message
WPARAM wParam, // first message parameter
LPARAM lParam  // second message parameter
)
{
	BOOL bResult;

	switch (uMsg) {
		case WM_COMMAND:
			UINT command;
			command = GET_WM_COMMAND_ID(wParam, lParam);
            if (IDOK == command)
            {
                EndDialog(hwndDlg, (IDOK == command));
            } else if (IDCANCEL == command)
            {
                EndDialog(hwndDlg, (IDOK == command));
            }
            bResult = FALSE;
			break;
		default:
			bResult = FALSE; // will be treated by DefWindowProc
}
	return bResult;
}


inline DWORD ACM::Configure(HWND hParentWindow, LPDRVCONFIGINFO pConfig)
{
	my_debug.OutPut(DEBUG_LEVEL_FUNC_START, "ACM : Configure (Parent Window = 0x%08X)",hParentWindow);

	DialogBoxParam( my_hModule, MAKEINTRESOURCE(IDD_CONFIG), hParentWindow, ::ConfigProc , (LPARAM)this);

	return DRVCNF_OK; // Can also return
					// DRVCNF_CANCEL
					// and DRVCNF_RESTART
}
*/
//////////////////////////////////////////////////////////////////////
// About Dialog
//////////////////////////////////////////////////////////////////////

static BOOL CALLBACK AboutProc(
  HWND hwndDlg,  // handle to dialog box
UINT uMsg,     // message
WPARAM wParam, // first message parameter
LPARAM lParam  // second message parameter
)
{
	static HBRUSH hBrushStatic = NULL;
//	static LOGFONT lf;  // structure for font information  
//	static HFONT hfnt;
	static HCURSOR hcOverCursor = NULL;
	BOOL bResult;

	switch (uMsg) {
		case WM_INITDIALOG:
			char tmp[150];
			wsprintf(tmp,"LAME MP3 codec v%s", ACM::GetVersionString());
			::SetWindowText(GetDlgItem( hwndDlg, IDC_STATIC_ABOUT_TITLE), tmp);

/*
			::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf); 
			lf.lfUnderline = TRUE;

			hfnt = ::CreateFontIndirect(&lf);

			::SendMessage(::GetDlgItem(hwndDlg,IDC_STATIC_ABOUT_URL), WM_SETFONT, (WPARAM) hfnt, TRUE);
* /
			hBrushStatic = ::CreateSolidBrush(::GetSysColor (COLOR_BTNFACE));
*/			hcOverCursor = ::LoadCursor(NULL,(LPCTSTR)IDC_HAND); 
			if (hcOverCursor == NULL)
				hcOverCursor = ::LoadCursor(NULL,(LPCTSTR)IDC_CROSS); 

			bResult = TRUE;
			break;
/*
		case WM_CTLCOLORSTATIC:
			/// \todo only if there are URLs
			if ((HWND)lParam == ::GetDlgItem(hwndDlg,IDC_STATIC_ABOUT_URL))
			{
				::SetTextColor((HDC)wParam, ::GetSysColor (COLOR_HIGHLIGHT));
				::SetBkColor((HDC)wParam, ::GetSysColor (COLOR_BTNFACE));

				return (LRESULT) hBrushStatic;
			}
			else
				return (LRESULT) NULL;
*/
		case WM_MOUSEMOVE:
			{
				POINT pnt;
				::GetCursorPos(&pnt);

				RECT rect;
				::GetWindowRect( ::GetDlgItem(hwndDlg,IDC_STATIC_ABOUT_URL), &rect);

				if (  ::PtInRect(&rect,pnt)  )
				{
					::SetCursor(hcOverCursor);
				}


			}
			break;

		case WM_LBUTTONUP:
			{
				POINT pnt;
				::GetCursorPos(&pnt);

				RECT rect;
				::GetWindowRect( ::GetDlgItem(hwndDlg,IDC_STATIC_ABOUT_URL), &rect);

				TCHAR Url[200];
				bool bUrl = false;
				if (::PtInRect(&rect,pnt))
				{
					wsprintf(Url,get_lame_url());
					bUrl = true;
				}

				if (bUrl)
				{
					LPSTR tmpStr;
					HRESULT hresult = ::TranslateURL(Url, TRANSLATEURL_FL_GUESS_PROTOCOL|TRANSLATEURL_FL_GUESS_PROTOCOL, &tmpStr);
					if (hresult == S_OK)
						::ShellExecute(hwndDlg,"open",tmpStr,NULL,"",SW_SHOWMAXIMIZED );
					else if (hresult == S_FALSE)
						::ShellExecute(hwndDlg,"open",Url,NULL,"",SW_SHOWMAXIMIZED );
				}

			}
			break;

		case WM_COMMAND:
			UINT command;
			command = GET_WM_COMMAND_ID(wParam, lParam);
            if (IDOK == command)
            {
                EndDialog(hwndDlg, TRUE);
            }
            bResult = FALSE;
			break;

		case IDC_STATIC_ABOUT_URL:
			break;
		default:
			bResult = FALSE; // will be treated by DefWindowProc
}
	return bResult;
}

inline DWORD ACM::About(HWND hParentWindow)
{
	my_debug.OutPut(DEBUG_LEVEL_FUNC_START, "ACM : About (Parent Window = 0x%08X)",hParentWindow);

	DialogBoxParam( my_hModule, MAKEINTRESOURCE(IDD_ABOUT), hParentWindow, ::AboutProc , (LPARAM)this);

	return DRVCNF_OK; // Can also return
// DRVCNF_CANCEL
// and DRVCNF_RESTART
}


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ACM::ACM( HMODULE hModule )
 :my_hModule(hModule),
  my_hIcon(NULL),
  my_debug(ADbg(DEBUG_LEVEL_CREATION)),
  my_EncodingProperties(hModule)
{
	my_EncodingProperties.ParamsRestore();

	/// \todo get the debug level from the registry
	unsigned char DebugFileName[512];

	char tmp[128];
	wsprintf(tmp,"LAMEacm 0x%08X",this);
	my_debug.setPrefix(tmp); /// \todo get it from the registry
	my_debug.setIncludeTime(true);  /// \todo get it from the registry

	// Check in the registry if we have to Output Debug information
	DebugFileName[0] = '\0';

	HKEY OssKey;
	if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\MUKOLI", 0, KEY_READ , &OssKey ) == ERROR_SUCCESS) {
		DWORD DataType;
		DWORD DebugFileNameSize = 512;
		if (RegQueryValueEx( OssKey, "DebugFile", NULL, &DataType, DebugFileName, &DebugFileNameSize ) == ERROR_SUCCESS) {
			if (DataType == REG_SZ) {
				my_debug.setUseFile(true);
				my_debug.setDebugFile((char *)DebugFileName);
				my_debug.OutPut("Debug file is %s",(char *)DebugFileName);
			}
		}
	}
        wsprintf(VersionString,"%s - %s", ACM_VERSION, get_lame_version() );
	BuildBitrateTable();
	
	my_debug.OutPut(DEBUG_LEVEL_FUNC_START, "New ACM Creation (0x%08X)",this);
}

ACM::~ACM()
{
// not used, it's done automatically when closing the driver	if (my_hIcon != NULL)
//		CloseHandle(my_hIcon);

	bitrate_table.clear();

	my_debug.OutPut(DEBUG_LEVEL_FUNC_START, "ACM Deleted (0x%08X)",this);
}

//////////////////////////////////////////////////////////////////////
// Main message handler
//////////////////////////////////////////////////////////////////////

LONG ACM::DriverProcedure(const HDRVR hdrvr, const UINT msg, LONG lParam1, LONG lParam2)
{
    DWORD dwRes = 0L;

//my_debug.OutPut(DEBUG_LEVEL_MSG, "message 0x%08X for ThisACM 0x%08X", msg, this);

switch (msg) {
    case DRV_INSTALL:
		my_debug.OutPut(DEBUG_LEVEL_MSG, "DRV_INSTALL");
		// Sent when the driver is installed.
		dwRes = DRVCNF_OK;  // Can also return 
		break;              // DRVCNF_CANCEL
							// and DRV_RESTART

	case DRV_REMOVE:
		// Sent when the driver is removed.
		my_debug.OutPut(DEBUG_LEVEL_MSG, "DRV_REMOVE");
		dwRes = 1L;  // return value ignored
		break;

    case DRV_QUERYCONFIGURE:
		my_debug.OutPut(DEBUG_LEVEL_MSG, "DRV_QUERYCONFIGURE");
		// Sent to determine if the driver can be
		// configured.
		dwRes = 1L;  // Zero indicates configuration
		break;       // NOT supported

	case DRV_CONFIGURE:
		my_debug.OutPut(DEBUG_LEVEL_MSG, "DRV_CONFIGURE");
		// Sent to display the configuration
		// dialog box for the driver.
//		dwRes = Configure( (HWND) lParam1, (LPDRVCONFIGINFO) lParam2 );
		if (my_EncodingProperties.Config(my_hModule, (HWND) lParam1))
		{
			dwRes = DRVCNF_OK; // Can also return
					// DRVCNF_CANCEL
					// and DRVCNF_RESTART
		} else {
			dwRes = DRVCNF_CANCEL;
		}
		break;

	/**************************************
	// ACM additional messages
	***************************************/

	case ACMDM_DRIVER_ABOUT:
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_DRIVER_ABOUT");

		dwRes = About( (HWND) lParam1 );

        break;

	case ACMDM_DRIVER_DETAILS: // acmDriverDetails
		// Fill-in general informations about the driver/codec
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_DRIVER_DETAILS");

		dwRes = OnDriverDetails(hdrvr, (LPACMDRIVERDETAILS) lParam1);
        
		break;

	case ACMDM_FORMATTAG_DETAILS: // acmFormatTagDetails
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_FORMATTAG_DETAILS");

		dwRes = OnFormatTagDetails((LPACMFORMATTAGDETAILS) lParam1, lParam2);

        break;

	case ACMDM_FORMAT_DETAILS: // acmFormatDetails
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_FORMAT_DETAILS");

		dwRes = OnFormatDetails((LPACMFORMATDETAILS) lParam1, lParam2);
		
        break;           

    case ACMDM_FORMAT_SUGGEST: // acmFormatSuggest
		// Sent to determine if the driver can be
		// configured.
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_FORMAT_SUGGEST");
		dwRes = OnFormatSuggest((LPACMDRVFORMATSUGGEST) lParam1);
        break; 

	/**************************************
	// ACM stream messages
	***************************************/

	case ACMDM_STREAM_OPEN:
	// Sent to determine if the driver can be
	// configured.
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_OPEN");
		dwRes = OnStreamOpen((LPACMDRVSTREAMINSTANCE) lParam1);
        break; 

	case ACMDM_STREAM_SIZE:
	// returns a recommended size for a source 
	// or destination buffer on an ACM stream
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_SIZE");
		dwRes = OnStreamSize((LPACMDRVSTREAMINSTANCE)lParam1, (LPACMDRVSTREAMSIZE)lParam2);
        break; 

	case ACMDM_STREAM_PREPARE:
	// prepares an ACMSTREAMHEADER structure for
	// an ACM stream conversion
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_PREPARE");
		dwRes = OnStreamPrepareHeader((LPACMDRVSTREAMINSTANCE)lParam1, (LPACMSTREAMHEADER) lParam2);
        break; 

	case ACMDM_STREAM_UNPREPARE:
	// cleans up the preparation performed by
	// the ACMDM_STREAM_PREPARE message for an ACM stream
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_UNPREPARE");
		dwRes = OnStreamUnPrepareHeader((LPACMDRVSTREAMINSTANCE)lParam1, (LPACMSTREAMHEADER) lParam2);
        break; 

	case ACMDM_STREAM_CONVERT:
	// perform a conversion on the specified conversion stream
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_CONVERT");
		dwRes = OnStreamConvert((LPACMDRVSTREAMINSTANCE)lParam1, (LPACMDRVSTREAMHEADER) lParam2);
		
        break; 

	case ACMDM_STREAM_CLOSE:
	// closes an ACM conversion stream
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACMDM_STREAM_CLOSE");
		dwRes = OnStreamClose((LPACMDRVSTREAMINSTANCE)lParam1);
        break;

	/**************************************
	// Unknown message
	***************************************/

	default:
	// Process any other messages.
		my_debug.OutPut(DEBUG_LEVEL_MSG, "ACM::DriverProc unknown message (0x%08X), lParam1 = 0x%08X, lParam2 = 0x%08X", msg, lParam1, lParam2);
		return DefDriverProc ((DWORD)this, hdrvr, msg, lParam1, lParam2);
    }

    return dwRes;
}

//////////////////////////////////////////////////////////////////////
// Special message handlers
//////////////////////////////////////////////////////////////////////
/*!
	Retreive the config details of this ACM driver
	The index represent the specified format

	\param a_FormatDetails will be filled with all the corresponding data
*/
inline DWORD ACM::OnFormatDetails(LPACMFORMATDETAILS a_FormatDetails, const LPARAM a_Query)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACM_FORMATDETAILS a_Query = 0x%08X",a_Query);
	switch (a_Query & ACM_FORMATDETAILSF_QUERYMASK) {

		// Fill-in the informations corresponding to the FormatDetails->dwFormatTagIndex
		case ACM_FORMATDETAILSF_INDEX :
			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "enter ACM_FORMATDETAILSF_INDEX for index 0x%04X:%03d",a_FormatDetails->dwFormatTag,a_FormatDetails->dwFormatIndex);
			if (a_FormatDetails->dwFormatTag == PERSONAL_FORMAT) {
				if (a_FormatDetails->dwFormatIndex < GetNumberEncodingFormats()) {
					LPWAVEFORMATEX WaveExt;
					WaveExt = a_FormatDetails->pwfx;

					WaveExt->wFormatTag = PERSONAL_FORMAT;

					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "format in  : channels %d, sample rate %d", WaveExt->nChannels, WaveExt->nSamplesPerSec);
					GetMP3FormatForIndex(a_FormatDetails->dwFormatIndex, *WaveExt, a_FormatDetails->szFormat);
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "format out : channels %d, sample rate %d", WaveExt->nChannels, WaveExt->nSamplesPerSec);
					Result = MMSYSERR_NOERROR;
				}
				else
				{
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACM_FORMATDETAILSF_INDEX unknown index 0x%04X:%03d",a_FormatDetails->dwFormatTag,a_FormatDetails->dwFormatIndex);
				}
			}
			else if (a_FormatDetails->dwFormatTag == WAVE_FORMAT_PCM) {
				if (a_FormatDetails->dwFormatIndex < FORMAT_MAX_NB_PCM) {
					LPWAVEFORMATEX WaveExt;
					WaveExt = a_FormatDetails->pwfx;

					WaveExt->wFormatTag = WAVE_FORMAT_PCM;

					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "format in  : channels %d, sample rate %d", WaveExt->nChannels, WaveExt->nSamplesPerSec);
					GetPCMFormatForIndex(a_FormatDetails->dwFormatIndex, *WaveExt, a_FormatDetails->szFormat);
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "format out : channels %d, sample rate %d", WaveExt->nChannels, WaveExt->nSamplesPerSec);
					Result = MMSYSERR_NOERROR;
				}
				else
				{
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACM_FORMATDETAILSF_INDEX unknown index 0x%04X:%03d",a_FormatDetails->dwFormatTag,a_FormatDetails->dwFormatIndex);
				}
			}
			else
			{
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Unknown a_FormatDetails->dwFormatTag = 0x%08X",a_FormatDetails->dwFormatTag);
			}

		case ACM_FORMATDETAILSF_FORMAT :
			/// \todo we may output the corresponding strong (only for personal format)
			LPWAVEFORMATEX WaveExt;
			WaveExt = a_FormatDetails->pwfx;

			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "enter ACM_FORMATDETAILSF_FORMAT : 0x%04X:%03d, format in : channels %d, sample rate %d",a_FormatDetails->dwFormatTag,a_FormatDetails->dwFormatIndex, WaveExt->nChannels, WaveExt->nSamplesPerSec);

			Result = MMSYSERR_NOERROR;
			break;
		
		default:
			Result = ACMERR_NOTPOSSIBLE;
			break;
	}

	a_FormatDetails->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;

	return Result;
}

/*!
	Retreive the details of each known format by this ACM driver
	The index represent the specified format (0 = MP3 / 1 = PCM)

	\param a_FormatTagDetails will be filled with all the corresponding data
*/
inline DWORD ACM::OnFormatTagDetails(LPACMFORMATTAGDETAILS a_FormatTagDetails, const LPARAM a_Query)
{
	DWORD Result;
	DWORD the_format = WAVE_FORMAT_UNKNOWN; // the format to give details

	if (a_FormatTagDetails->cbStruct >= sizeof(*a_FormatTagDetails)) {

		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACMDM_FORMATTAG_DETAILS, a_Query = 0x%08X",a_Query);
		switch(a_Query & ACM_FORMATTAGDETAILSF_QUERYMASK) {

			case ACM_FORMATTAGDETAILSF_INDEX:
			// Fill-in the informations corresponding to the a_FormatDetails->dwFormatTagIndex
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "get ACM_FORMATTAGDETAILSF_INDEX for index %03d",a_FormatTagDetails->dwFormatTagIndex);

				if (a_FormatTagDetails->dwFormatTagIndex < FORMAT_TAG_MAX_NB) {
					switch (a_FormatTagDetails->dwFormatTagIndex)
					{
					case 0:
						the_format = PERSONAL_FORMAT;
						break;
					default :
						the_format = WAVE_FORMAT_PCM;
						break;
					}
				}
				else
				{
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACM_FORMATTAGDETAILSF_INDEX for unsupported index %03d",a_FormatTagDetails->dwFormatTagIndex);
					Result = ACMERR_NOTPOSSIBLE;
				}
				break;

			case ACM_FORMATTAGDETAILSF_FORMATTAG:
			// Fill-in the informations corresponding to the a_FormatDetails->dwFormatTagIndex and hdrvr given
				switch (a_FormatTagDetails->dwFormatTag)
				{
				case WAVE_FORMAT_PCM:
					the_format = WAVE_FORMAT_PCM;
					break;
				case PERSONAL_FORMAT:
					the_format = PERSONAL_FORMAT;
					break;
				default:
                    return (ACMERR_NOTPOSSIBLE);
				}
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "get ACM_FORMATTAGDETAILSF_FORMATTAG for index 0x%02X, cStandardFormats = %d",a_FormatTagDetails->dwFormatTagIndex,a_FormatTagDetails->cStandardFormats);
				break;
			case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "ACM_FORMATTAGDETAILSF_LARGESTSIZE not used");
				Result = 0L;
				break;
			default:
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnFormatTagDetails Unknown Format tag query");
				Result = MMSYSERR_NOTSUPPORTED;
				break;
		}

		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnFormatTagDetails the_format = 0x%08X",the_format);
		switch(the_format)
		{
			case WAVE_FORMAT_PCM:
				a_FormatTagDetails->dwFormatTag      = WAVE_FORMAT_PCM;
				a_FormatTagDetails->dwFormatTagIndex = 0;
				a_FormatTagDetails->cbFormatSize     = sizeof(PCMWAVEFORMAT);
				/// \note 0 may mean we don't know how to decode
				a_FormatTagDetails->fdwSupport       = ACMDRIVERDETAILS_SUPPORTF_CODEC;
				a_FormatTagDetails->cStandardFormats = FORMAT_MAX_NB_PCM;
				// should be filled by Windows				a_FormatTagDetails->szFormatTag[0] = '\0';
				Result = MMSYSERR_NOERROR;
				break;
			case PERSONAL_FORMAT:
				a_FormatTagDetails->dwFormatTag      = PERSONAL_FORMAT;
				a_FormatTagDetails->dwFormatTagIndex = 1;
				a_FormatTagDetails->cbFormatSize     = SIZE_FORMAT_STRUCT;
				a_FormatTagDetails->fdwSupport       = ACMDRIVERDETAILS_SUPPORTF_CODEC;
				a_FormatTagDetails->cStandardFormats = GetNumberEncodingFormats();
				lstrcpyW( a_FormatTagDetails->szFormatTag, L"Lame MP3" );
				Result = MMSYSERR_NOERROR;
				break;
			default:
				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnFormatTagDetails Unknown format 0x%08X",the_format);
				return (ACMERR_NOTPOSSIBLE);
		}
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnFormatTagDetails %d possibilities for format 0x%08X",a_FormatTagDetails->cStandardFormats,the_format);
	}
	else
	{
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "a_FormatTagDetails->cbStruct < sizeof(*a_FormatDetails)");
		Result = ACMERR_NOTPOSSIBLE;
	}

	return Result;
}

/*!
	Retreive the global details of this ACM driver

	\param a_DriverDetail will be filled with all the corresponding data
*/
inline DWORD ACM::OnDriverDetails(const HDRVR hdrvr, LPACMDRIVERDETAILS a_DriverDetail)
{
	if (my_hIcon == NULL)
		my_hIcon = LoadIcon(GetDriverModuleHandle(hdrvr), MAKEINTRESOURCE(IDI_ICON));
	a_DriverDetail->hicon       = my_hIcon;

	a_DriverDetail->fccType     = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
	a_DriverDetail->fccComp     = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;

	/// \note this is an explicit hack of the FhG values
	/// \note later it could be a new value when the decoding is done
	a_DriverDetail->wMid        = MM_FRAUNHOFER_IIS;
	a_DriverDetail->wPid        = MM_FHGIIS_MPEGLAYER3;

	a_DriverDetail->vdwACM      = VERSION_MSACM;
	a_DriverDetail->vdwDriver   = VERSION_ACM_DRIVER;
	a_DriverDetail->fdwSupport  = ACMDRIVERDETAILS_SUPPORTF_CODEC;
	a_DriverDetail->cFormatTags = FORMAT_TAG_MAX_NB; // 2 : MP3 and PCM
//	a_DriverDetail->cFormatTags = 1; // 2 : MP3 and PCM
	a_DriverDetail->cFilterTags = FILTER_TAG_MAX_NB;

	lstrcpyW( a_DriverDetail->szShortName, L"LAME MP3" );
	char tmpStr[128];
	wsprintf(tmpStr, "LAME MP3 Codec v%s", GetVersionString());
	int u = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, tmpStr, -1, a_DriverDetail->szLongName, 0);
	MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, tmpStr, -1, a_DriverDetail->szLongName, u);
	lstrcpyW( a_DriverDetail->szCopyright, L"2002 Steve Lhomme" );
	lstrcpyW( a_DriverDetail->szLicensing, L"LGPL (see gnu.org)" );
	/// \todo update this part when the code changes
	lstrcpyW( a_DriverDetail->szFeatures , L"only CBR implementation" );

    return MMSYSERR_NOERROR;  // Can also return DRVCNF_CANCEL
}

/*!
	Suggest an output format for the specified input format

	\param a_FormatSuggest will be filled with all the corresponding data
*/
inline DWORD ACM::OnFormatSuggest(LPACMDRVFORMATSUGGEST a_FormatSuggest)
{
	DWORD Result = MMSYSERR_NOTSUPPORTED;
    DWORD fdwSuggest = (ACM_FORMATSUGGESTF_TYPEMASK & a_FormatSuggest->fdwSuggest);

my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest %s%s%s%s (0x%08X)",
				 (fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS) ? "channels, ":"",
				 (fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC) ? "samples/sec, ":"",
				 (fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE) ? "bits/sample, ":"",
				 (fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG) ? "format, ":"",
				 fdwSuggest);

my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest for source format = 0x%04X, channels = %d, Samples/s = %d, AvgB/s = %d, BlockAlign = %d, b/sample = %d",
				 a_FormatSuggest->pwfxSrc->wFormatTag,
				 a_FormatSuggest->pwfxSrc->nChannels,
				 a_FormatSuggest->pwfxSrc->nSamplesPerSec,
				 a_FormatSuggest->pwfxSrc->nAvgBytesPerSec,
				 a_FormatSuggest->pwfxSrc->nBlockAlign,
				 a_FormatSuggest->pwfxSrc->wBitsPerSample);

my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggested destination format = 0x%04X, channels = %d, Samples/s = %d, AvgB/s = %d, BlockAlign = %d, b/sample = %d",
			 a_FormatSuggest->pwfxDst->wFormatTag,
			 a_FormatSuggest->pwfxDst->nChannels,
			 a_FormatSuggest->pwfxDst->nSamplesPerSec,
			 a_FormatSuggest->pwfxDst->nAvgBytesPerSec,
			 a_FormatSuggest->pwfxDst->nBlockAlign,
			 a_FormatSuggest->pwfxDst->wBitsPerSample);

	switch (a_FormatSuggest->pwfxSrc->wFormatTag)
	{
        case WAVE_FORMAT_PCM:
			/// \todo handle here the decoding ?
			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest for PCM source");
            //
			//  if the destination format tag is restricted, verify that
			//  it is within our capabilities...
			//
			//  this driver is able to decode to PCM
			//
			if (ACM_FORMATSUGGESTF_WFORMATTAG & fdwSuggest)
            {
                if (PERSONAL_FORMAT != a_FormatSuggest->pwfxDst->wFormatTag)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->wFormatTag = PERSONAL_FORMAT;
            }


my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest succeed A");
            //
			//  if the destination channel count is restricted, verify that
			//  it is within our capabilities...
			//
			//  this driver is not able to change the number of channels
			//
			if (ACM_FORMATSUGGESTF_NCHANNELS & fdwSuggest)
            {
                if (a_FormatSuggest->pwfxSrc->nChannels != a_FormatSuggest->pwfxDst->nChannels)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->nChannels = a_FormatSuggest->pwfxSrc->nChannels;
            }

			if (a_FormatSuggest->pwfxSrc->nChannels != 1 && a_FormatSuggest->pwfxSrc->nChannels != 2)
				return MMSYSERR_INVALPARAM;


my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest succeed B");
            //
			//  if the destination samples per second is restricted, verify
			//  that it is within our capabilities...
			//
			//  this driver is not able to change the sample rate
			//
			if (ACM_FORMATSUGGESTF_NSAMPLESPERSEC & fdwSuggest)
            {
                if (a_FormatSuggest->pwfxSrc->nSamplesPerSec != a_FormatSuggest->pwfxDst->nSamplesPerSec)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->nSamplesPerSec = a_FormatSuggest->pwfxSrc->nSamplesPerSec;
            }


my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest succeed C");
            //
			//  if the destination bits per sample is restricted, verify
			//  that it is within our capabilities...
			//
			//  We prefer decoding to 16-bit PCM.
			//
			if (ACM_FORMATSUGGESTF_WBITSPERSAMPLE & fdwSuggest)
            {
                if ( (16 != a_FormatSuggest->pwfxDst->wBitsPerSample) && (8 != a_FormatSuggest->pwfxDst->wBitsPerSample) )
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->wBitsPerSample = 16;
            }

			//			a_FormatSuggest->pwfxDst->nBlockAlign = FORMAT_BLOCK_ALIGN;
			a_FormatSuggest->pwfxDst->nBlockAlign = a_FormatSuggest->pwfxDst->nChannels * a_FormatSuggest->pwfxDst->wBitsPerSample / 8;
			
			a_FormatSuggest->pwfxDst->nAvgBytesPerSec = a_FormatSuggest->pwfxDst->nChannels * 64000 / 8;

			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest succeed");
			Result = MMSYSERR_NOERROR;


			break;
		case PERSONAL_FORMAT:
			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest for PERSONAL source");
            //
			//  if the destination format tag is restricted, verify that
			//  it is within our capabilities...
			//
			//  this driver is able to decode to PCM
			//
			if (ACM_FORMATSUGGESTF_WFORMATTAG & fdwSuggest)
            {
                if (WAVE_FORMAT_PCM != a_FormatSuggest->pwfxDst->wFormatTag)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
            }


            //
			//  if the destination channel count is restricted, verify that
			//  it is within our capabilities...
			//
			//  this driver is not able to change the number of channels
			//
			if (ACM_FORMATSUGGESTF_NCHANNELS & fdwSuggest)
            {
                if (a_FormatSuggest->pwfxSrc->nChannels != a_FormatSuggest->pwfxDst->nChannels)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->nChannels = a_FormatSuggest->pwfxSrc->nChannels;
            }


            //
			//  if the destination samples per second is restricted, verify
			//  that it is within our capabilities...
			//
			//  this driver is not able to change the sample rate
			//
			if (ACM_FORMATSUGGESTF_NSAMPLESPERSEC & fdwSuggest)
            {
                if (a_FormatSuggest->pwfxSrc->nSamplesPerSec != a_FormatSuggest->pwfxDst->nSamplesPerSec)
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->nSamplesPerSec = a_FormatSuggest->pwfxSrc->nSamplesPerSec;
            }


            //
			//  if the destination bits per sample is restricted, verify
			//  that it is within our capabilities...
			//
			//  We prefer decoding to 16-bit PCM.
			//
			if (ACM_FORMATSUGGESTF_WBITSPERSAMPLE & fdwSuggest)
            {
                if ( (16 != a_FormatSuggest->pwfxDst->wBitsPerSample) && (8 != a_FormatSuggest->pwfxDst->wBitsPerSample) )
                    return (ACMERR_NOTPOSSIBLE);
            }
            else
			{
                a_FormatSuggest->pwfxDst->wBitsPerSample = 16;
            }

			//			a_FormatSuggest->pwfxDst->nBlockAlign = FORMAT_BLOCK_ALIGN;
			a_FormatSuggest->pwfxDst->nBlockAlign = a_FormatSuggest->pwfxDst->nChannels * a_FormatSuggest->pwfxDst->wBitsPerSample / 8;
			
			/// \todo this value must be a correct one !
			a_FormatSuggest->pwfxDst->nAvgBytesPerSec = a_FormatSuggest->pwfxDst->nSamplesPerSec * a_FormatSuggest->pwfxDst->nChannels * a_FormatSuggest->pwfxDst->wBitsPerSample / 8;

			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggest succeed");
			Result = MMSYSERR_NOERROR;


			break;
	}

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Suggested destination format = 0x%04X, channels = %d, Samples/s = %d, AvgB/s = %d, BlockAlign = %d, b/sample = %d",
				 a_FormatSuggest->pwfxDst->wFormatTag,
				 a_FormatSuggest->pwfxDst->nChannels,
				 a_FormatSuggest->pwfxDst->nSamplesPerSec,
				 a_FormatSuggest->pwfxDst->nAvgBytesPerSec,
				 a_FormatSuggest->pwfxDst->nBlockAlign,
				 a_FormatSuggest->pwfxDst->wBitsPerSample);

	return Result;
}

/*!
	Create a stream instance for decoding/encoding

	\param a_StreamInstance contain information about the stream desired
*/
inline DWORD ACM::OnStreamOpen(LPACMDRVSTREAMINSTANCE a_StreamInstance)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	//
	//  the most important condition to check before doing anything else
	//  is that this ACM driver can actually perform the conversion we are
	//  being opened for. this check should fail as quickly as possible
	//  if the conversion is not possible by this driver.
	//
	//  it is VERY important to fail quickly so the ACM can attempt to
	//  find a driver that is suitable for the conversion. also note that
	//  the ACM may call this driver several times with slightly different
	//  format specifications before giving up.
	//
	//  this driver first verifies that the source and destination formats
	//  are acceptable...
	//
	switch (a_StreamInstance->pwfxSrc->wFormatTag)
	{
        case WAVE_FORMAT_PCM:
			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream for PCM source (%05d samples %d channels %d bits/sample)",a_StreamInstance->pwfxSrc->nSamplesPerSec,a_StreamInstance->pwfxSrc->nChannels,a_StreamInstance->pwfxSrc->wBitsPerSample);
			if (a_StreamInstance->pwfxDst->wFormatTag == PERSONAL_FORMAT)
			{
				unsigned int OutputFrequency;

				/// \todo Smart mode
				if (my_EncodingProperties.GetSmartOutputMode())
					OutputFrequency = ACMStream::GetOutputSampleRate(a_StreamInstance->pwfxSrc->nSamplesPerSec,a_StreamInstance->pwfxDst->nAvgBytesPerSec,a_StreamInstance->pwfxDst->nChannels);
				else
					OutputFrequency = a_StreamInstance->pwfxSrc->nSamplesPerSec;

				my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream for PERSONAL output (%05d samples %d channels %d bits/sample %d kbps)",a_StreamInstance->pwfxDst->nSamplesPerSec,a_StreamInstance->pwfxDst->nChannels,a_StreamInstance->pwfxDst->wBitsPerSample,8 * a_StreamInstance->pwfxDst->nAvgBytesPerSec);

				/// \todo add the possibility to have channel resampling (mono to stereo / stereo to mono)
				/// \todo support resampling ?
				/// \todo only do the test on OutputFrequency in "Smart Output" mode
				if (a_StreamInstance->pwfxDst->nSamplesPerSec != OutputFrequency ||
//					a_StreamInstance->pwfxSrc->nSamplesPerSec != a_StreamInstance->pwfxDst->nSamplesPerSec ||
					a_StreamInstance->pwfxSrc->nChannels != a_StreamInstance->pwfxDst->nChannels ||
					a_StreamInstance->pwfxSrc->wBitsPerSample != 16)
				{
					Result = ACMERR_NOTPOSSIBLE;
				} else {
					if ((a_StreamInstance->fdwOpen & ACM_STREAMOPENF_QUERY) == 0)
					{
						ACMStream * the_stream = ACMStream::Create();
						a_StreamInstance->dwInstance = (DWORD) the_stream;

						if (the_stream != NULL)
						{
							MPEGLAYER3WAVEFORMAT * casted = (MPEGLAYER3WAVEFORMAT *) a_StreamInstance->pwfxDst;
							vbr_mode a_mode = (casted->fdwFlags-2 == 0)?vbr_abr:vbr_off;
							if (the_stream->init(a_StreamInstance->pwfxDst->nSamplesPerSec,
												 OutputFrequency,
												 a_StreamInstance->pwfxDst->nChannels,
												 a_StreamInstance->pwfxDst->nAvgBytesPerSec,
												 a_mode))
								Result = MMSYSERR_NOERROR;
							else
								ACMStream::Erase( the_stream );
						}
					}
					else
					{
						Result = MMSYSERR_NOERROR;
					}
				}
			}
			break;
		case PERSONAL_FORMAT:
			my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream for PERSONAL source (%05d samples %d channels %d bits/sample %d kbps)",a_StreamInstance->pwfxSrc->nSamplesPerSec,a_StreamInstance->pwfxSrc->nChannels,a_StreamInstance->pwfxSrc->wBitsPerSample,8 * a_StreamInstance->pwfxSrc->nAvgBytesPerSec);
			if (a_StreamInstance->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
			{
#ifdef ENABLE_DECODING
				if ((a_StreamInstance->fdwOpen & ACM_STREAMOPENF_QUERY) == 0)
				{
					/// \todo create the decoding stream
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream for PCM output (%05d samples %d channels %d bits/sample %d B/s)",a_StreamInstance->pwfxDst->nSamplesPerSec,a_StreamInstance->pwfxDst->nChannels,a_StreamInstance->pwfxDst->wBitsPerSample,a_StreamInstance->pwfxDst->nAvgBytesPerSec);

					DecodeStream * the_stream = DecodeStream::Create();
					a_StreamInstance->dwInstance = (DWORD) the_stream;

					if (the_stream != NULL)
					{
						if (the_stream->init(a_StreamInstance->pwfxDst->nSamplesPerSec,
											 a_StreamInstance->pwfxDst->nChannels,
											 a_StreamInstance->pwfxDst->nAvgBytesPerSec,
											 a_StreamInstance->pwfxSrc->nAvgBytesPerSec))
							Result = MMSYSERR_NOERROR;
						else
							DecodeStream::Erase( the_stream );
					}
				}
				else
				{
					/// \todo decoding verification
					my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream is valid");
					Result = MMSYSERR_NOERROR;
				}
#endif // ENABLE_DECODING
			}
			break;
	}

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Open stream Result = %d",Result);
	return Result;
}

inline DWORD ACM::OnStreamSize(LPACMDRVSTREAMINSTANCE a_StreamInstance, LPACMDRVSTREAMSIZE the_StreamSize)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

    switch (ACM_STREAMSIZEF_QUERYMASK & the_StreamSize->fdwSize)
    {
	case ACM_STREAMSIZEF_DESTINATION:
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Get source buffer size for destination size = %d",the_StreamSize->cbDstLength);
		break;
	case ACM_STREAMSIZEF_SOURCE:
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "Get destination buffer size for source size = %d",the_StreamSize->cbSrcLength);
        if (WAVE_FORMAT_PCM == a_StreamInstance->pwfxSrc->wFormatTag &&
			PERSONAL_FORMAT == a_StreamInstance->pwfxDst->wFormatTag)
        {
			ACMStream * the_stream = (ACMStream *) a_StreamInstance->dwInstance;
			if (the_stream != NULL)
			{
				the_StreamSize->cbDstLength = the_stream->GetOutputSizeForInput(the_StreamSize->cbSrcLength);
				Result = MMSYSERR_NOERROR;
			}
		}
        else if (PERSONAL_FORMAT == a_StreamInstance->pwfxSrc->wFormatTag &&
			 WAVE_FORMAT_PCM== a_StreamInstance->pwfxDst->wFormatTag)
		{
#ifdef ENABLE_DECODING
			DecodeStream * the_stream = (DecodeStream *) a_StreamInstance->dwInstance;
			if (the_stream != NULL)
			{
				the_StreamSize->cbDstLength = the_stream->GetOutputSizeForInput(the_StreamSize->cbSrcLength);
				Result = MMSYSERR_NOERROR;
			}
#endif // ENABLE_DECODING
		}
		break;
	default:
		Result = MMSYSERR_INVALFLAG;
		break;
	}

	return Result;
}

inline DWORD ACM::OnStreamClose(LPACMDRVSTREAMINSTANCE a_StreamInstance)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnStreamClose the stream 0x%X",a_StreamInstance->dwInstance);
    if (WAVE_FORMAT_PCM == a_StreamInstance->pwfxSrc->wFormatTag &&
		PERSONAL_FORMAT == a_StreamInstance->pwfxDst->wFormatTag)
    {
	ACMStream::Erase( (ACMStream *) a_StreamInstance->dwInstance );
	}
    else if (PERSONAL_FORMAT == a_StreamInstance->pwfxSrc->wFormatTag &&
		 WAVE_FORMAT_PCM== a_StreamInstance->pwfxDst->wFormatTag)
    {
#ifdef ENABLE_DECODING
		DecodeStream::Erase( (DecodeStream *) a_StreamInstance->dwInstance );
#endif // ENABLE_DECODING
	}

	// nothing to do yet
	Result = MMSYSERR_NOERROR;

	return Result;
}

inline DWORD ACM::OnStreamPrepareHeader(LPACMDRVSTREAMINSTANCE a_StreamInstance, LPACMSTREAMHEADER a_StreamHeader)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "  prepare : Src : %d (0x%08X) / %d - Dst : %d (0x%08X) / %d"
												, a_StreamHeader->cbSrcLength
												, a_StreamHeader->pbSrc
												, a_StreamHeader->cbSrcLengthUsed
												, a_StreamHeader->cbDstLength
												, a_StreamHeader->pbDst
												, a_StreamHeader->cbDstLengthUsed
											  );

	if (WAVE_FORMAT_PCM == a_StreamInstance->pwfxSrc->wFormatTag &&
		PERSONAL_FORMAT == a_StreamInstance->pwfxDst->wFormatTag)
	{
		ACMStream * the_stream = (ACMStream *)a_StreamInstance->dwInstance;
		
		if (the_stream->open(my_EncodingProperties))
			Result = MMSYSERR_NOERROR;
	}
	else if (PERSONAL_FORMAT == a_StreamInstance->pwfxSrc->wFormatTag &&
		     WAVE_FORMAT_PCM == a_StreamInstance->pwfxDst->wFormatTag)
	{
#ifdef ENABLE_DECODING
		DecodeStream * the_stream = (DecodeStream *)a_StreamInstance->dwInstance;
		
		if (the_stream->open())
			Result = MMSYSERR_NOERROR;
#endif // ENABLE_DECODING
	}

	return Result;
}

inline DWORD ACM::OnStreamUnPrepareHeader(LPACMDRVSTREAMINSTANCE a_StreamInstance, LPACMSTREAMHEADER a_StreamHeader)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "unprepare : Src : %d / %d - Dst : %d / %d"
											, a_StreamHeader->cbSrcLength
											, a_StreamHeader->cbSrcLengthUsed
											, a_StreamHeader->cbDstLength
											, a_StreamHeader->cbDstLengthUsed
											);
    if (WAVE_FORMAT_PCM == a_StreamInstance->pwfxSrc->wFormatTag &&
		PERSONAL_FORMAT == a_StreamInstance->pwfxDst->wFormatTag)
    {
	ACMStream * the_stream = (ACMStream *)a_StreamInstance->dwInstance;
	DWORD OutputSize = a_StreamHeader->cbDstLength;
	
	if (the_stream->close(a_StreamHeader->pbDst, &OutputSize) && (OutputSize <= a_StreamHeader->cbDstLength))
	{
		a_StreamHeader->cbDstLengthUsed = OutputSize;
			Result = MMSYSERR_NOERROR;
		}
	}
    else if (PERSONAL_FORMAT == a_StreamInstance->pwfxSrc->wFormatTag &&
		 WAVE_FORMAT_PCM== a_StreamInstance->pwfxDst->wFormatTag)
    {
#ifdef ENABLE_DECODING
		DecodeStream * the_stream = (DecodeStream *)a_StreamInstance->dwInstance;
		DWORD OutputSize = a_StreamHeader->cbDstLength;
		
		if (the_stream->close(a_StreamHeader->pbDst, &OutputSize) && (OutputSize <= a_StreamHeader->cbDstLength))
		{
			a_StreamHeader->cbDstLengthUsed = OutputSize;
			Result = MMSYSERR_NOERROR;
	}
#endif // ENABLE_DECODING
	}

	return Result;
}

inline DWORD ACM::OnStreamConvert(LPACMDRVSTREAMINSTANCE a_StreamInstance, LPACMDRVSTREAMHEADER a_StreamHeader)
{
	DWORD Result = ACMERR_NOTPOSSIBLE;

	if (WAVE_FORMAT_PCM == a_StreamInstance->pwfxSrc->wFormatTag &&
		PERSONAL_FORMAT == a_StreamInstance->pwfxDst->wFormatTag)
	{
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnStreamConvert SRC = PCM (encode)");

		ACMStream * the_stream = (ACMStream *) a_StreamInstance->dwInstance;
		if (the_stream != NULL)
		{
			if (the_stream->ConvertBuffer( a_StreamHeader ))
				Result = MMSYSERR_NOERROR;
		}
	}
	else if (PERSONAL_FORMAT == a_StreamInstance->pwfxSrc->wFormatTag &&
		     WAVE_FORMAT_PCM == a_StreamInstance->pwfxDst->wFormatTag)
	{
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnStreamConvert SRC = MP3 (decode)");

#ifdef ENABLE_DECODING
		DecodeStream * the_stream = (DecodeStream *) a_StreamInstance->dwInstance;
		if (the_stream != NULL)
		{
			if (the_stream->ConvertBuffer( a_StreamHeader ))
				Result = MMSYSERR_NOERROR;
		}
#endif // ENABLE_DECODING
	}
	else
		my_debug.OutPut(DEBUG_LEVEL_FUNC_CODE, "OnStreamConvert unsupported conversion");

	return Result;
}


void ACM::GetMP3FormatForIndex(const DWORD the_Index, WAVEFORMATEX & the_Format, unsigned short the_String[ACMFORMATDETAILS_FORMAT_CHARS]) const
{
	int Block_size;
    char temp[ACMFORMATDETAILS_FORMAT_CHARS];


	if (the_Index < bitrate_table.size())
	{
	//	the_Format.wBitsPerSample = 16;
		the_Format.wBitsPerSample = 0;
	
		/// \todo handle more channel modes (mono, stereo, joint-stereo, dual-channel)
	//	the_Format.nChannels = SIZE_CHANNEL_MODE - int(the_Index % SIZE_CHANNEL_MODE);
	
		the_Format.nBlockAlign = 1;

		the_Format.nSamplesPerSec = bitrate_table[the_Index].frequency;
		the_Format.nAvgBytesPerSec = bitrate_table[the_Index].bitrate * 1000 / 8;
		if (bitrate_table[the_Index].frequency >= mpeg1_freq[SIZE_FREQ_MPEG1-1])
			Block_size = 1152;
		else
			Block_size = 576;
	
		the_Format.nChannels = bitrate_table[the_Index].channels;

		the_Format.cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);
		MPEGLAYER3WAVEFORMAT * tmpFormat = (MPEGLAYER3WAVEFORMAT *) &the_Format;
		tmpFormat->wID             = 1;
		// this is the only way I found to know if we do CBR or ABR
		tmpFormat->fdwFlags        = 2 + ((bitrate_table[the_Index].mode == vbr_abr)?0:2);
		tmpFormat->nBlockSize      = Block_size * the_Format.nAvgBytesPerSec / the_Format.nSamplesPerSec;
		tmpFormat->nFramesPerBlock = 1;
		tmpFormat->nCodecDelay     = 0; // 0x0571 on FHG
	
         /// \todo : generate the string with the appropriate stereo mode
         if (bitrate_table[the_Index].mode == vbr_abr)
             wsprintfA( temp, "%d Hz, %d kbps ABR, %s", the_Format.nSamplesPerSec, the_Format.nAvgBytesPerSec * 8 / 1000, (the_Format.nChannels == 1)?"Mono":"Stereo");
         else
             wsprintfA( temp, "%d Hz, %d kbps CBR, %s", the_Format.nSamplesPerSec, the_Format.nAvgBytesPerSec * 8 / 1000, (the_Format.nChannels == 1)?"Mono":"Stereo");

         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, the_String, ACMFORMATDETAILS_FORMAT_CHARS);
     }
 }

void ACM::GetPCMFormatForIndex(const DWORD the_Index, WAVEFORMATEX & the_Format, unsigned short the_String[ACMFORMATDETAILS_FORMAT_CHARS]) const
{
	the_Format.nChannels = SIZE_CHANNEL_MODE - int(the_Index % SIZE_CHANNEL_MODE);
	the_Format.wBitsPerSample = 16;
	the_Format.nBlockAlign = the_Format.nChannels * the_Format.wBitsPerSample / 8;


	DWORD a_Channel_Independent = the_Index / SIZE_CHANNEL_MODE;

	// first MPEG1 frequencies
	if (a_Channel_Independent < SIZE_FREQ_MPEG1)
	{
		the_Format.nSamplesPerSec = mpeg1_freq[a_Channel_Independent];
	}
	else
	{
		a_Channel_Independent -= SIZE_FREQ_MPEG1;
		the_Format.nSamplesPerSec = mpeg2_freq[a_Channel_Independent];
	}

	the_Format.nAvgBytesPerSec = the_Format.nSamplesPerSec * the_Format.nChannels * the_Format.wBitsPerSample / 8;
}

DWORD ACM::GetNumberEncodingFormats() const
{
	return bitrate_table.size();
}

bool ACM::IsSmartOutput(const int frequency, const int bitrate, const int channels) const
{
	double compression_ratio = double(frequency * 2 * channels) / double(bitrate * 100);

//my_debug.OutPut(DEBUG_LEVEL_FUNC_DEBUG, "compression_ratio %f, freq %d, bitrate %d, channels %d", compression_ratio, frequency, bitrate, channels);

	if(my_EncodingProperties.GetSmartOutputMode())
		return (compression_ratio <= my_EncodingProperties.GetSmartRatio());
	else return true;
}

void ACM::BuildBitrateTable()
{
	my_debug.OutPut("entering BuildBitrateTable");

	// fill the table
	unsigned int channel,bitrate,freq;
	
	bitrate_table.clear();

	// CBR bitrates
	for (channel = 0;channel < SIZE_CHANNEL_MODE;channel++)
	{
		// MPEG I
		for (freq = 0;freq < SIZE_FREQ_MPEG1;freq++)
		{
			for (bitrate = 0;bitrate < SIZE_BITRATE_MPEG1;bitrate++)
			{

				if (!my_EncodingProperties.GetSmartOutputMode() || IsSmartOutput(mpeg1_freq[freq], mpeg1_bitrate[bitrate], channel+1))
				{
					bitrate_item bitrate_table_tmp;
					
					bitrate_table_tmp.frequency = mpeg1_freq[freq];
					bitrate_table_tmp.bitrate = mpeg1_bitrate[bitrate];
					bitrate_table_tmp.channels = channel+1;
					bitrate_table_tmp.mode = vbr_off;
					bitrate_table.push_back(bitrate_table_tmp);
				}
			}
		}
		// MPEG II / II.5
		for (freq = 0;freq < SIZE_FREQ_MPEG2;freq++)
		{
			for (bitrate = 0;bitrate < SIZE_BITRATE_MPEG2;bitrate++)
			{
				if (!my_EncodingProperties.GetSmartOutputMode() || IsSmartOutput(mpeg2_freq[freq], mpeg2_bitrate[bitrate], channel+1))
				{
					bitrate_item bitrate_table_tmp;

                                        bitrate_table_tmp.frequency = mpeg2_freq[freq];
					bitrate_table_tmp.bitrate = mpeg2_bitrate[bitrate];
					bitrate_table_tmp.channels = channel+1;
					bitrate_table_tmp.mode = vbr_abr;
					bitrate_table.push_back(bitrate_table_tmp);
				}
			}
		}
	}

	if (my_EncodingProperties.GetAbrOutputMode())
	// ABR bitrates
	{
		for (channel = 0;channel < SIZE_CHANNEL_MODE;channel++)
		{
			// MPEG I
			for (freq = 0;freq < SIZE_FREQ_MPEG1;freq++)
			{
				for (bitrate = my_EncodingProperties.GetAbrBitrateMax();
					   bitrate >= my_EncodingProperties.GetAbrBitrateMin(); 
				     bitrate -= my_EncodingProperties.GetAbrBitrateStep())
				{
					if (bitrate >= mpeg1_bitrate[SIZE_BITRATE_MPEG1-1] && (!my_EncodingProperties.GetSmartOutputMode() || IsSmartOutput(mpeg1_freq[freq], bitrate, channel+1)))
					{
						bitrate_item bitrate_table_tmp;
						
						bitrate_table_tmp.frequency = mpeg1_freq[freq];
						bitrate_table_tmp.bitrate = bitrate;
						bitrate_table_tmp.channels = channel+1;
						bitrate_table_tmp.mode = vbr_abr;
						bitrate_table.push_back(bitrate_table_tmp);
					}
				}
			}
			// MPEG II / II.5
			for (freq = 0;freq < SIZE_FREQ_MPEG2;freq++)
			{
				for (bitrate = my_EncodingProperties.GetAbrBitrateMax();
					   bitrate >= my_EncodingProperties.GetAbrBitrateMin(); 
				     bitrate -= my_EncodingProperties.GetAbrBitrateStep())
				{
					if (bitrate >= mpeg2_bitrate[SIZE_BITRATE_MPEG2-1] && (!my_EncodingProperties.GetSmartOutputMode() || IsSmartOutput(mpeg2_freq[freq], bitrate, channel+1)))
					{
						bitrate_item bitrate_table_tmp;
						
						bitrate_table_tmp.frequency = mpeg2_freq[freq];
						bitrate_table_tmp.bitrate = bitrate;
						bitrate_table_tmp.channels = channel+1;
						bitrate_table_tmp.mode = vbr_abr;
						bitrate_table.push_back(bitrate_table_tmp);
					}
				}
			}
		}
	}

	// sorting by frequency/bitrate/channel
	std::sort(bitrate_table.begin(), bitrate_table.end());

/*	{
		// display test
		int i=0;
		for (i=0; i<bitrate_table.size();i++)
		{
			my_debug.OutPut("bitrate_table[%d].frequency = %d",i,bitrate_table[i].frequency);
			my_debug.OutPut("bitrate_table[%d].bitrate = %d",i,bitrate_table[i].bitrate);
			my_debug.OutPut("bitrate_table[%d].channel = %d",i,bitrate_table[i].channels);
			my_debug.OutPut("bitrate_table[%d].ABR = %s\n",i,(bitrate_table[i].mode == vbr_abr)?"ABR":"CBR");
		}
	}*/

	my_debug.OutPut("leaving BuildBitrateTable");
}