-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCodec.cpp
943 lines (727 loc) · 22.3 KB
/
Codec.cpp
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
/*
** CDex - Open Source Digital Audio CD Extractor
**
** Copyright (C) 2006 - 2007 Georgy Berdyshev
** Copyright (C) 1999 Albert L. Faber
**
** http://cdexos.sourceforge.net/
** http://sourceforge.net/projects/cdexos
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#include "Config.h"
#include "Codec.h"
#include <math.h>
#include <msacm.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static SHORT pMyBuffer[2][1152];
static int gs_nDebug=0;
// CONSTRUCTOR
CAcmFormat::CAcmFormat()
{
ENTRY_TRACE( _T( "CAcmFormat::CAcmFormat()" ) );
cbStruct=0;
dwFormatIndex=0;
dwFormatTag=0;
fdwSupport=0;
pwfx=NULL;
cbwfx=0;
memset(szFormat,0x00,sizeof(szFormat));
EXIT_TRACE( _T( "CAcmFormat::CAcmFormat()" ) );
}
// DESTRUCTOR
CAcmFormat::~CAcmFormat()
{
ENTRY_TRACE( _T( "CAcmFormat::~CAcmFormat()" ) );
delete [] pwfx;
EXIT_TRACE( _T( "CAcmFormat::~CAcmFormat()" ) );
}
// ASSIGNMENT OPERATOR
CAcmFormat& CAcmFormat::operator=(const CAcmFormat& rhs)
{
ENTRY_TRACE( _T( "CAcmFormat::operator=" ) );
// Avoid self assignment
if (this!= &rhs)
{
// Deltete old pwfx
delete [] pwfx;pwfx=NULL;
cbStruct= rhs.cbStruct;
dwFormatIndex= rhs.dwFormatIndex;
dwFormatTag= rhs.dwFormatTag;
fdwSupport= rhs.fdwSupport;
cbwfx= rhs.cbwfx;
if (rhs.pwfx!=NULL)
{
// Allocate space for pwfx
pwfx=(LPWAVEFORMATEX) new BYTE[cbwfx];
// Assign it
memcpy(pwfx,rhs.pwfx,cbwfx);
}
_tcscpy( szFormat, rhs.szFormat );
}
EXIT_TRACE( _T( "CAcmFormat::operator=" ) );
return *this;
}
// COPY CONSTRUCTOR
CAcmFormat::CAcmFormat(const CAcmFormat& rhs)
{
ENTRY_TRACE( _T( "CAcmFormat::CAcmFormat(const CAcmFormat& rhs)" ) );
cbStruct= rhs.cbStruct;
dwFormatIndex= rhs.dwFormatIndex;
dwFormatTag= rhs.dwFormatTag;
fdwSupport= rhs.fdwSupport;
cbwfx= rhs.cbwfx;
if (rhs.pwfx!=NULL)
{
// Allocate space for pwfx
pwfx=(LPWAVEFORMATEX) new BYTE[cbwfx];
// Assign it
memcpy(pwfx,rhs.pwfx,cbwfx);
}
_tcscpy( szFormat, rhs.szFormat );
EXIT_TRACE( _T( "CAcmFormat::CAcmFormat(const CAcmFormat& rhs)" ) );
}
CAcmFormat::CAcmFormat(const ACMFORMATDETAILS& rhs)
{
ENTRY_TRACE( _T( "CAcmFormat::CAcmFormat(const ACMFORMATDETAILS& rhs)" ) );
cbStruct= rhs.cbStruct;
dwFormatIndex= rhs.dwFormatIndex;
dwFormatTag= rhs.dwFormatTag;
fdwSupport= rhs.fdwSupport;
cbwfx= rhs.cbwfx;
if (rhs.pwfx!=NULL)
{
// Allocate space for pwfx
pwfx=(LPWAVEFORMATEX) new BYTE[cbwfx];
// Assign it
memcpy(pwfx,rhs.pwfx,cbwfx);
}
_tcscpy( szFormat, rhs.szFormat );
EXIT_TRACE( _T( "CAcmFormat::CAcmFormat(const ACMFORMATDETAILS& rhs)" ) );
}
// CONSTRUCTOR
CAcmDriverInfo::CAcmDriverInfo()
{
ENTRY_TRACE( _T( "CAcmDriverInfo::CAcmDriverInfo()" ) );
hadid=NULL;
memset(&dd,0x00,sizeof(ACMDRIVERDETAILS));
EXIT_TRACE( _T( "CAcmDriverInfo::CAcmDriverInfo()" ) );
}
// DESTRUCTOR
CAcmDriverInfo::~CAcmDriverInfo()
{
LTRACE2( _T( "CAcmDriverInfo::~CAcmDriverInfo() %s" ), dd.szLongName );
}
// ASSIGNMENT OPERATOR
CAcmDriverInfo& CAcmDriverInfo::operator=(const CAcmDriverInfo& rhs)
{
ENTRY_TRACE( _T( "CAcmDriverInfo::operator=" ) );
// Avoid self assignment
if (this!= &rhs)
{
hadid=rhs.hadid;
dd=rhs.dd;
m_vFormatDetails=rhs.m_vFormatDetails;
}
EXIT_TRACE( _T( "CAcmDriverInfo::operator=" ) );
return *this;
}
// COPY CONSTRUCTOR
CAcmDriverInfo::CAcmDriverInfo(const
CAcmDriverInfo& rhs)
{
ENTRY_TRACE( _T( "CAcmDriverInfo::CAcmDriverInfo(const CAcmDriverInfo& rhs)" ) );
hadid=rhs.hadid;
dd=rhs.dd;
m_vFormatDetails=rhs.m_vFormatDetails;
EXIT_TRACE( _T( "CAcmDriverInfo::CAcmDriverInfo(const CAcmDriverInfo& rhs)" ) );
}
// CONSTRUCTOR
CAcmCodec::CAcmCodec()
{
ENTRY_TRACE( _T( "CAcmCodec::CAcmCodec()" ) );
m_bIntermPCM=FALSE;
m_pbtSrcBuffer = NULL;
m_pbtPcmBuffer = NULL;
m_pbtDstBuffer = NULL;
m_pPcmAcmDriver = NULL;
m_pDstAcmDriver = NULL;
m_pPcmAcmFormat = NULL;
m_pDstAcmFormat = NULL;
m_bMP3CodecInstalled = FALSE;
m_nOffset = 0;
m_nBufferSize = 0;
EXIT_TRACE( _T( "CAcmCodec::CAcmCodec()" ) );
}
void CAcmCodec::ReInit()
{
ENTRY_TRACE( _T( "CAcmCodec::ReInit()" ) );
delete [] m_pbtSrcBuffer;
delete [] m_pbtPcmBuffer;
delete [] m_pbtDstBuffer;
m_pbtSrcBuffer=NULL;
m_pbtPcmBuffer=NULL;
m_pbtDstBuffer=NULL;
m_pPcmAcmDriver=NULL;
m_pDstAcmDriver=NULL;
m_pPcmAcmFormat=NULL;
m_pDstAcmFormat=NULL;
m_hPcmStream=NULL;
m_hDstStream=NULL;
m_hPcmDriver=NULL;
m_hDstDriver=NULL;
memset(&m_DstStreamHdr, 0, sizeof(m_DstStreamHdr));
memset(&m_PcmStreamHdr, 0, sizeof(m_PcmStreamHdr));
EXIT_TRACE( _T( "CAcmCodec::ReInit()" ) );
}
// DESRUCTOR
CAcmCodec::~CAcmCodec()
{
ENTRY_TRACE( _T( "CAcmCodec::~CAcmCodec()" ) );
delete [] m_pbtSrcBuffer;
delete [] m_pbtPcmBuffer;
delete [] m_pbtDstBuffer;
EXIT_TRACE( _T( "CAcmCodec::~CAcmCodec()" ) );
}
int CAcmCodec::GetNumDetails(int nCodecIndex) const
{
return m_vAcmDriverInfo[nCodecIndex].m_vFormatDetails.size();
}
CUString CAcmCodec::GetFormatString(int nCodecIndex,int nFormatIndex) const
{
return m_vAcmDriverInfo[ nCodecIndex ].m_vFormatDetails[ nFormatIndex ].szFormat;
}
CUString CAcmCodec::GetFormatString(int nCodecIndex,int nFormatIndex,DWORD dwTag) const
{
if (m_vAcmDriverInfo[nCodecIndex].m_vFormatDetails[nFormatIndex].dwFormatTag==dwTag)
{
return m_vAcmDriverInfo[nCodecIndex].m_vFormatDetails[nFormatIndex].szFormat;
}
else
return _W( "" );
}
// Only get the Codec that support type dwTag
// Return empty string when TAG ID is not supported
CUString CAcmCodec::GetACMCodecName(int nCodecIndex,DWORD dwTag)
{
ENTRY_TRACE( _T( "CAcmCodec::GetACMCodecName()" ) );
// Sanity check
ASSERT((DWORD)nCodecIndex<m_vAcmDriverInfo.size());
BOOL bTagSupported=FALSE;
// Loop through formats
for (DWORD i=0;i<m_vAcmDriverInfo[nCodecIndex].m_vFormatDetails.size();i++)
{
// Check format tag out of ACMFORMATDETAILS
if (m_vAcmDriverInfo[nCodecIndex].m_vFormatDetails[i].dwFormatTag==dwTag)
bTagSupported=TRUE;
}
if (bTagSupported)
{
EXIT_TRACE( _T( "CAcmCodec::GetACMCodecName() OK" ) );
// Return long name of the nCodecIndex
return CUString(m_vAcmDriverInfo[nCodecIndex].dd.szLongName);
}
else
{
EXIT_TRACE( _T( "CAcmCodec::GetACMCodecName() EMPTY") );
return _W( "" );
}
}
void CAcmCodec::ShowError(MMRESULT& mmr)
{
ASSERT(FALSE);
LTRACE( _T( "++++++++++++++++++++++ Codec Error +++++++++++++" ) );
}
void CAcmCodec::DisplayCodecInfo(int nCodecIndex) const
{
ENTRY_TRACE( _T( "CAcmCodec::DisplayCodecInfo()" ) );
ACMDRIVERDETAILS dd=m_vAcmDriverInfo[nCodecIndex].dd;
CUString strTmp;
CUString strOut;
strOut.Format( _W( "Codec details for driver index %d" ), nCodecIndex );
strOut+= _W( "\r\n-------------------------------------------------\r\n" );
strOut+= _W( "\r\nDriver Details information\r\n" );
strTmp.Format( _W( " Short name: %s\r\n" ), (LPCWSTR)CUString( dd.szShortName ));
strOut+=strTmp;
strTmp.Format( _W( " Long name: %s\r\n" ), (LPCWSTR)CUString( dd.szLongName ));
strOut+=strTmp;
strTmp.Format( _W( " Copyright: %s\r\n" ), (LPCWSTR)CUString( dd.szCopyright ));
strOut+=strTmp;
strTmp.Format( _W( " Licensing: %s\r\n" ), (LPCWSTR)CUString( dd.szLicensing ));
strOut+=strTmp;
strTmp.Format( _W( " Features: %s\r\n" ), (LPCWSTR)CUString( dd.szFeatures));
strOut+=strTmp;
strTmp.Format( _W( " Supports %u formats\r\n" ), dd.cFormatTags );
strOut+=strTmp;
strTmp.Format( _W( " Supports %u filter formats\r\n" ), dd.cFilterTags );
strOut+=strTmp;
CDexMessageBox( strOut );
EXIT_TRACE( _T( "CAcmCodec::DisplayCodecInfo()" ) );
}
// Get the ACM version.
void CAcmCodec::GetVersion()
{
ENTRY_TRACE( _T( "CAcmCodec::GetVersion()" ) );
CUString strDebug;
DWORD dwACMVer = acmGetVersion();
LTRACE( _T( "ACM version %u.%.02u build %u" ), HIWORD( dwACMVer ) >> 8, HIWORD( dwACMVer ) & 0x00FF, LOWORD( dwACMVer ) );
if (LOWORD(dwACMVer) == 0)
LTRACE( _T( "Retail Version" ) );
EXIT_TRACE( _T( "CAcmCodec::GetVersion()" ));
}
BOOL CALLBACK CAcmCodec::FormatEnumProc(HACMDRIVERID hadid, LPACMFORMATDETAILS pafd, DWORD dwInstance, DWORD fdwSupport)
{
ENTRY_TRACE( _T( "CAcmCodec::FormatEnumProc()" ) );
// Get a pointer to the CAcmCodec caller object
CAcmCodec* pAcmCodec=(CAcmCodec*)dwInstance;
if (pafd)
{
CAcmFormat newFormat(*pafd);
LTRACE( _T( "New codec enumeration: %4.4lXH, %s" ), pafd->dwFormatTag, pafd->szFormat);
if (pAcmCodec->m_vAcmDriverInfo.size() >0 )
{
// if (pafd->pwfx->wFormatTag==0x55)
if (pafd->pwfx->wFormatTag== 0x270 || pafd->pwfx->wFormatTag== 0x55)
{
pAcmCodec->m_bMP3CodecInstalled=TRUE;
// Push pafd to format array
pAcmCodec->m_vAcmDriverInfo.back().m_vFormatDetails.push_back(newFormat);
LTRACE( _T( "Add encoder format to list" ) );
}
}
}
EXIT_TRACE( _T( "CAcmCodec::FormatEnumProc()") );
return TRUE; // Continue enumerating.
}
/*
void EnumerateFormats(void) {
int i;
MMRESULT retval;
for (i = 0;i < NumModes;i++) if (ModeList[i].wfx) free(ModeList[i].wfx);
if(hidMP3) {
//Open Driver
retval = acmDriverOpen( &hMP3,hidMP3,0);
if (retval) BadExit(retval);
memset(&fd, 0, sizeof(fd));
memset(&wf,0,sizeof(wf));
fd.cbStruct = sizeof(fd);
//fd.dwFormatTag = WAVE_FORMAT_UNKNOWN;
fd.pwfx = &wf;
retval = acmMetrics((HACMOBJ)hMP3, ACM_METRIC_MAX_SIZE_FORMAT, &fd.cbwfx);
fd.pwfx->nChannels=2;
fd.pwfx->nSamplesPerSec = 44100;
fd.pwfx->wBitsPerSample = 16;
fd.pwfx->wFormatTag = WAVE_FORMAT_PCM;
fd.pwfx->nBlockAlign = 2*16/8;
fd.pwfx->nAvgBytesPerSec = 44100 * (2*16/8);
NumModes = 0;
retval = acmFormatEnum(hMP3,&fd,EnumFormats,0L,ACM_FORMATENUMF_CONVERT);
if(hMP3) acmDriverClose(hMP3,0);
if (retval) {
printf("No information available\n");
BadExit(retval);
}
}
}
*/
// acmDriverEnumCallback function
BOOL CALLBACK CAcmCodec::DriverEnumProc(HACMDRIVERID hadid, DWORD dwInstance, DWORD fdwSupport)
{
MMRESULT mmr;
CAcmDriverInfo newDriverInfo;
HACMDRIVER had = NULL;
CUString strDebug;
ENTRY_TRACE( _T( "CAcmCodec::DriverEnumProc()") );
// Get a pointer to the CAcmCodec caller object
CAcmCodec* pAcmCodec=(CAcmCodec*)dwInstance;
// Set hadid of current Codec enumeration
newDriverInfo.hadid=hadid;
// Clear detail structure.
memset(&newDriverInfo.dd,0x00,sizeof(ACMDRIVERDETAILS));
// Set structure size of ACMDRIVERDETAILS structure
newDriverInfo.dd.cbStruct = sizeof(ACMDRIVERDETAILS);
// Get the details of this driver
mmr = acmDriverDetails(hadid, &newDriverInfo.dd, 0);
if (mmr)
{
pAcmCodec->ShowError(mmr);
return TRUE;
}
// Skip buggy codecs
if ( _tcsstr(newDriverInfo.dd.szLongName, _T( "Delrina" ) ) !=NULL
|| _tcsstr(newDriverInfo.dd.szLongName, _T( "VoxWare" ) ) !=NULL
|| _tcsstr(newDriverInfo.dd.szLongName, _T( "Ulead MPEG1 Layer2" ) ) !=NULL
)
{
return TRUE;
}
// Open the driver.
mmr = acmDriverOpen(&had,hadid, 0);
if (mmr)
{
pAcmCodec->ShowError(mmr);
return TRUE;
}
strDebug.Format( _W( "%s id: %8.8lxH supports: "), (LPCWSTR)CUString( newDriverInfo.dd.szLongName ), hadid );
if (fdwSupport & ACMDRIVERDETAILS_SUPPORTF_ASYNC)
{
strDebug+= _W( " async conversions ");
}
if (fdwSupport & ACMDRIVERDETAILS_SUPPORTF_CODEC)
{
strDebug+= _W( " different format conversions" );
}
if (fdwSupport & ACMDRIVERDETAILS_SUPPORTF_CONVERTER)
{
strDebug += _W( " same format conversions" );
}
if (fdwSupport & ACMDRIVERDETAILS_SUPPORTF_FILTER)
{
strDebug+= _W( " filtering" );
}
CUStringConvert strCnv;
LTRACE( strCnv.ToT( strDebug ) );
DWORD dwSize = 0;
// Add newDriverInfo to array
pAcmCodec->m_vAcmDriverInfo.push_back(newDriverInfo);
// Get metrics of the maximum WAVEFORMATEX structure size
mmr = acmMetrics((HACMOBJ)had, ACM_METRIC_MAX_SIZE_FORMAT, &dwSize);
// Correct size in case sizeof(WAVEFORMAT) is returned instead of
// sizeof(WAVEFORMATEX), can happen when only MS-PCM codecs are installed
if (dwSize < sizeof(WAVEFORMATEX))
dwSize = sizeof(WAVEFORMATEX);
// Allocate memory for temp wave format struct
WAVEFORMATEX* pwf = (WAVEFORMATEX*) new BYTE[dwSize];
// clear allocated memory of pwf structure
memset(pwf,0x00,dwSize);
// Set WAVEFORMATEX fields
pwf->wFormatTag = WAVE_FORMAT_PCM; // Search for PCM input format
pwf->nChannels = 2; // Stereo
pwf->nSamplesPerSec = 44100; // 44100 Hz
pwf->wBitsPerSample = 16; // 16 bits/sample
pwf->nBlockAlign = pwf->nChannels * pwf->wBitsPerSample / 8; // set alignment
pwf->nAvgBytesPerSec = pwf->nSamplesPerSec * pwf->nBlockAlign;
pwf->cbSize = (WORD)( dwSize-sizeof(WAVEFORMATEX) );
// Create fd struct
ACMFORMATDETAILS fd;
// clear contents of fd struct
memset(&fd, 0, sizeof(fd));
// Set structure size
fd.cbStruct = sizeof(fd);
// Set WAVEFORMAT related parameters
fd.pwfx = pwf;
fd.cbwfx = dwSize;
// Add some debug info
LTRACE2( _T( "Start FormatEnumProc" ) );
// Enumerate the available formats for this particular driver
mmr = acmFormatEnum(had, &fd, FormatEnumProc,dwInstance,ACM_FORMATENUMF_CONVERT);
if (mmr)
{
pAcmCodec->ShowError(mmr);
return TRUE;
}
// And close driver
mmr=acmDriverClose(had, 0);
if (mmr)
{
pAcmCodec->ShowError(mmr);
return TRUE;
}
// Delete pwf struct
delete [] pwf;
EXIT_TRACE( _T( "CAcmCodec::DriverEnumProc()" ) );
// Continue enumeration
return TRUE;
}
int CAcmCodec::GetNumEncoders(DWORD dwTag)
{
return 0;
}
void CAcmCodec::EnumDrivers()
{
CUStringConvert strCnv;
// Do we have to debug this stuff?
gs_nDebug= GetPrivateProfileInt( _T( "Debug" ), _T( "DebugCodec" ) , gs_nDebug, strCnv.ToT( g_config.GetIniFileName() ) );
ENTRY_TRACE( _T( "CAcmCodec::EnumDrivers()" ) );
// Clear all AcmDriverInfo entries
m_vAcmDriverInfo.clear();
// Get the ACM driver version
GetVersion();
#ifdef VERBOSE
// Enumerate the set of enabled drivers.
LATRACE2("Enabled drivers:\n");
#endif
MMRESULT mmr;
// Enumerate all Codecs
mmr = acmDriverEnum(DriverEnumProc,(DWORD)this,ACM_DRIVERENUMF_NOLOCAL);
// Was there an error
if (mmr)
ShowError(mmr);
// GetNumEncoders(0x55);
EXIT_TRACE( _T( "CAcmCodec::EnumDrivers()" ) );
}
// 1152 frames, two channels, and two bytes per sample
const int nMaxChunkSize = 1152 * 2 * 2 * 2;
CDEX_ERR CAcmCodec::InitStream( CUString& strCodec,
CUString& strFormat,
INT nCodecID,
int nSampleFreq,
int nChannels,
BOOL bHighQuality )
{
BOOL bFound;
CUString strTmp;
ENTRY_TRACE( _T( "CAcmCodec::InitStream()" ) );
m_bIntermPCM=FALSE;
WAVEFORMATEX wfPCM;
memset(&wfPCM, 0, sizeof(wfPCM));
bFound=FALSE;
// For safety reasons, when DeInitStream has not been called
ReInit();
int nChunkSize = nMaxChunkSize;
if ( nCodecID == 0x270 )
{
// nChunkSize = 3072;
}
// Find proper codec
int nCodecIndex=0;
for (nCodecIndex=0;nCodecIndex<GetNumCodecs();nCodecIndex++)
{
CUString strName(GetACMCodecName(nCodecIndex,nCodecID));
if (strName.Find(strCodec)>=0)
{
bFound=TRUE;
break;
}
}
if (bFound==FALSE)
return CDEX_ERROR;
bFound=FALSE;
int nFormat=0;
for (nFormat=0;nFormat<GetNumDetails(nCodecIndex);nFormat++)
{
CUString strFmt=GetFormatString(nCodecIndex,nFormat,nCodecID);
// CUString strFmt=GetFormatString( nCodecIndex, nFormat );
if (strFmt.Find(strFormat)>=0)
{
bFound=TRUE;
break;
}
}
if (bFound==FALSE)
{
LTRACE( _T( "CAcmCodec::InitStream() Coul not find target format") );
return CDEX_ERROR;
}
// Set pointer to MP3 driver
m_pDstAcmDriver=&m_vAcmDriverInfo[nCodecIndex];
// Set pointer to MP3 format
m_pDstAcmFormat=&m_pDstAcmDriver->m_vFormatDetails[nFormat];
// Setup WAVEFORMATEX source structure
WAVEFORMATEX wfSrc;
memset(&wfSrc, 0, sizeof(wfSrc));
wfSrc.cbSize = 0;
wfSrc.wFormatTag = WAVE_FORMAT_PCM; // PCM
wfSrc.nChannels = nChannels; // Stereo or mono
wfSrc.nSamplesPerSec = nSampleFreq; // Set Sample frequency
wfSrc.wBitsPerSample = 16; // 16 bits/sample
wfSrc.nBlockAlign = wfSrc.nChannels * wfSrc.wBitsPerSample / 8;
wfSrc.nAvgBytesPerSec = wfSrc.nSamplesPerSec * wfSrc.nBlockAlign;
// m_dwSrcSamples = nMaxChunkSize / sizeof( SHORT );
// m_dwDstSamples = nMaxChunkSize / sizeof( SHORT );
m_pbtSrcBuffer= new BYTE[ 2 * nMaxChunkSize ];
m_pbtDstBuffer= new BYTE[ nMaxChunkSize ];
m_nOffset = 0;
m_nBufferSize = 0;
// Open the driver.
HACMDRIVER had = NULL;
MMRESULT mmr = acmDriverOpen(&m_hDstDriver, m_pDstAcmDriver->hadid, 0);
// Get a pointer to the destination WAVEFORMATEX structure
LPWAVEFORMATEX pwfDst=m_pDstAcmFormat->pwfx;
// Try to open the stream
mmr = acmStreamOpen(&m_hDstStream,
m_hDstDriver, // The conversion ACM driver
&wfSrc, // Source format
pwfDst, // Destination format
NULL, // No filter
NULL, // No callback
NULL, // Instance data (not used)
(bHighQuality == TRUE) ? ACM_STREAMOPENF_NONREALTIME : 0 );
if (mmr)
{
LTRACE( _T( "CAcmCodec::InitStream() Failed to open conversion stream") );
CDexMessageBox( _W( "Failed to open a stream to do PCM to MP3 conversion" ) );
return CDEX_ERROR;
}
// Set struct size
m_DstStreamHdr.cbStruct = sizeof( m_DstStreamHdr );
//Set source pointer
m_DstStreamHdr.pbSrc = m_pbtSrcBuffer;
m_DstStreamHdr.cbSrcLength = nChunkSize;
m_DstStreamHdr.dwSrcUser = nChunkSize;
// Set destination pointer
m_DstStreamHdr.pbDst = m_pbtDstBuffer;
m_DstStreamHdr.cbDstLength = nChunkSize;
m_DstStreamHdr.dwDstUser = nChunkSize;
// Prepare the stream header the header.
mmr = acmStreamPrepareHeader( m_hDstStream, &m_DstStreamHdr, 0 );
// if (mmr)
// {
// LTRACE("CAcmCodec::InitStream() Failed to open conversion stream");
// CDexMessageBox( _W( "Failed to create WinCodec stream" ));
// return CDEX_ERROR;
// }
EXIT_TRACE( _T( "CAcmCodec::InitStream() OK" ) );
return CDEX_OK;
}
//////////////////// DEINIT /////////////////////////////
CDEX_ERR CAcmCodec::DeInitStream( PBYTE pOutput, PDWORD pdwOutput )
{
CDEX_ERR err;
ENTRY_TRACE( _T( "CAcmCodec::DeInitStream()" ) );
// Get last bytes out of the encoder
err = EncodeChunk( 0,
NULL,
pOutput,
pdwOutput,
ACM_STREAMCONVERTF_BLOCKALIGN | ACM_STREAMCONVERTF_END );
// Uprepare the header
if (m_hDstStream)
acmStreamUnprepareHeader(m_hDstStream, &m_DstStreamHdr, 0);
// Close the stream.
if (m_hPcmStream)
acmStreamClose(m_hPcmStream, 0);
// Close the driver
if (m_hPcmDriver)
acmDriverClose(m_hPcmDriver, 0);
// Close the stream.
if (m_hDstStream)
acmStreamClose(m_hDstStream, 0);
// Close the driver
if (m_hDstDriver)
acmDriverClose(m_hDstDriver, 0);
// Delete buffer memory
delete [] m_pbtSrcBuffer;m_pbtSrcBuffer=NULL;
delete [] m_pbtDstBuffer;m_pbtDstBuffer=NULL;
///////////////////////////////////////////////////////////////////
EXIT_TRACE( _T( "CAcmCodec::DeInitStream()" ) );
return err;
}
/*
CDEX_ERR CAcmCodec::WinEncoderFunc(void* pMP3Params)
{
CUString strTmp;
// Initialize encoder
if (InitStream(g_config.GetWinCodec(),g_config.GetWinCodecFmt())==CDEX_ERROR)
return CDEX_ERROR;
// Get the MP3 parameters
MP3PARAMS* pParams=(MP3PARAMS*)pMP3Params;
MMRESULT mmr;
// Open MP3 file
FILE* fp = fopen(pParams->lpszFileName,"wb+");
if (fp==NULL)
return CDEX_ERROR;
while(pParams->pBufferFunc(pMyBuffer)>0 )
{
SHORT* pSrcBuffer=(SHORT*)m_pbtSrcBuffer;
int j=0;
// Descramble bytes in correct order
for (int i=0;i<1152;i++)
{
pSrcBuffer[j++]=pMyBuffer[0][i];
pSrcBuffer[j++]=pMyBuffer[1][i];
}
m_DstStreamHdr.cbSrcLength=4*1152;
mmr = acmStreamConvert(m_hDstStream, &m_DstStreamHdr,ACM_STREAMCONVERTF_BLOCKALIGN);
if (mmr)
{
printf("Failed to do PCM to PCM conversion\n");
return CDEX_ERROR;
}
#ifdef VERBOSE
LATRACE("Converted OK\n");
#endif
if (m_DstStreamHdr.cbDstLengthUsed>0)
fwrite(m_DstStreamHdr.pbDst,1,m_DstStreamHdr.cbDstLengthUsed,fp);
}
mmr = acmStreamConvert(m_hDstStream,
&m_DstStreamHdr,
ACM_STREAMCONVERTF_BLOCKALIGN |
ACM_STREAMCONVERTF_END);
if (m_DstStreamHdr.cbDstLengthUsed>0)
fwrite(m_DstStreamHdr.pbDst,1,m_DstStreamHdr.cbDstLengthUsed,fp);
DeInitStream();
////////////////////////// END OF MOVE TO ENCODE CHUNK //////////////////////////
// CLose the output file
fclose(fp);
return CDEX_OK;
}
*/
CDEX_ERR CAcmCodec::EncodeChunk( DWORD nSamples, PSHORT pSamples, PBYTE pOutput, PDWORD pdwOutput,int nFlags )
{
MMRESULT mmr = 0;
// Copy the number of input samples
memcpy( &m_pbtSrcBuffer[ m_nOffset ], pSamples, nSamples * sizeof( SHORT ) );
m_nOffset += nSamples * sizeof( SHORT );
*pdwOutput = 0;
while ( m_nOffset >= m_nBufferSize && m_nOffset > 0 )
{
m_DstStreamHdr.cbSrcLength = m_nOffset;
mmr = acmStreamConvert( m_hDstStream, &m_DstStreamHdr, nFlags );
if ( m_DstStreamHdr.cbSrcLengthUsed > 0 )
{
memmove( m_pbtSrcBuffer,
&m_pbtSrcBuffer[ m_nOffset ],
m_nOffset - m_DstStreamHdr.cbSrcLengthUsed );
m_nOffset -= m_DstStreamHdr.cbSrcLengthUsed;
}
// Copy the output bytes
if ( m_DstStreamHdr.cbDstLengthUsed > 0 )
{
memcpy( &pOutput[ *pdwOutput ], m_DstStreamHdr.pbDst, m_DstStreamHdr.cbDstLengthUsed );
*pdwOutput += m_DstStreamHdr.cbDstLengthUsed;
}
if ( 0 == m_nBufferSize )
{
m_nBufferSize = m_DstStreamHdr.cbSrcLengthUsed;
}
}
if ( ( nFlags & ACM_STREAMCONVERTF_END ) && m_nOffset > 0 )
{
// get the last part out of the encoder
m_DstStreamHdr.cbSrcLength = m_nOffset;
mmr = acmStreamConvert( m_hDstStream, &m_DstStreamHdr, nFlags );
// Copy the output bytes
if ( m_DstStreamHdr.cbDstLengthUsed > 0 )
{
memcpy( &pOutput[ *pdwOutput ], m_DstStreamHdr.pbDst, m_DstStreamHdr.cbDstLengthUsed );
*pdwOutput += m_DstStreamHdr.cbDstLengthUsed;
}
}
if ( mmr )
{
ASSERT( FALSE );
return CDEX_ERROR;
}
#ifdef VERBOSE
LTRACE("Converted OK\n");
#endif
// Set the number of output bytes
return CDEX_OK;
}