ole32/tests: Add more IStream_Seek tests for hglobalstream.
[wine] / dlls / winemp3.acm / mpegl3.c
1 /*
2  * MPEG Layer 3 handling
3  *
4  * Copyright (C) 2002 Eric Pouech
5  * Copyright (C) 2009 CodeWeavers, Aric Stewart
6  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30 #ifdef HAVE_MPG123_H
31 # include <mpg123.h>
32 #else
33 # ifdef HAVE_COREAUDIO_COREAUDIO_H
34 #  include <CoreFoundation/CoreFoundation.h>
35 #  include <CoreAudio/CoreAudio.h>
36 # endif
37 # ifdef HAVE_AUDIOTOOLBOX_AUDIOCONVERTER_H
38 #  include <AudioToolbox/AudioConverter.h>
39 # endif
40 # ifdef HAVE_AUDIOTOOLBOX_AUDIOFILE_H
41 #  include <AudioToolbox/AudioFile.h>
42 # endif
43 # ifdef HAVE_AUDIOTOOLBOX_AUDIOFILESTREAM_H
44 #  include <AudioToolbox/AudioFileStream.h>
45 # endif
46 #endif
47
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "mmsystem.h"
54 #include "mmreg.h"
55 #include "msacm.h"
56 #include "msacmdrv.h"
57 #include "wine/debug.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
60
61 /* table to list all supported formats... those are the basic ones. this
62  * also helps given a unique index to each of the supported formats
63  */
64 typedef struct
65 {
66     int         nChannels;
67     int         nBits;
68     int         rate;
69 } Format;
70
71 static const Format PCM_Formats[] =
72 {
73     {1,  8,  8000}, {2,  8,  8000}, {1, 16,  8000}, {2, 16,  8000},
74     {1,  8, 11025}, {2,  8, 11025}, {1, 16, 11025}, {2, 16, 11025},
75     {1,  8, 12000}, {2,  8, 12000}, {1, 16, 12000}, {2, 16, 12000},
76     {1,  8, 16000}, {2,  8, 16000}, {1, 16, 16000}, {2, 16, 16000},
77     {1,  8, 22050}, {2,  8, 22050}, {1, 16, 22050}, {2, 16, 22050},
78     {1,  8, 24000}, {2,  8, 24000}, {1, 16, 24000}, {2, 16, 24000},
79     {1,  8, 32000}, {2,  8, 32000}, {1, 16, 32000}, {2, 16, 32000},
80     {1,  8, 44100}, {2,  8, 44100}, {1, 16, 44100}, {2, 16, 44100},
81     {1,  8, 48000}, {2,  8, 48000}, {1, 16, 48000}, {2, 16, 48000}
82 };
83
84 static const Format MPEG3_Formats[] =
85 {
86     {1,  0,  8000}, {2,  0,  8000},
87     {1,  0, 11025}, {2,  0, 11025},
88     {1,  0, 12000}, {2,  0, 12000},
89     {1,  0, 16000}, {2,  0, 16000},
90     {1,  0, 22050}, {2,  0, 22050},
91     {1,  0, 24000}, {2,  0, 24000},
92     {1,  0, 32000}, {2,  0, 32000},
93     {1,  0, 44100}, {2,  0, 44100},
94     {1,  0, 48000}, {2,  0, 48000}
95 };
96
97 #define NUM_PCM_FORMATS         (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
98 #define NUM_MPEG3_FORMATS       (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
99
100 /***********************************************************************
101  *           MPEG3_GetFormatIndex
102  */
103 static  DWORD   MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
104 {
105     int         i, hi;
106     const Format *fmts;
107
108     switch (wfx->wFormatTag)
109     {
110     case WAVE_FORMAT_PCM:
111         hi = NUM_PCM_FORMATS;
112         fmts = PCM_Formats;
113         break;
114     case WAVE_FORMAT_MPEGLAYER3:
115         hi = NUM_MPEG3_FORMATS;
116         fmts = MPEG3_Formats;
117         break;
118     default:
119         return 0xFFFFFFFF;
120     }
121
122     for (i = 0; i < hi; i++)
123     {
124         if (wfx->nChannels == fmts[i].nChannels &&
125             wfx->nSamplesPerSec == fmts[i].rate &&
126             (wfx->wBitsPerSample == fmts[i].nBits || !fmts[i].nBits))
127             return i;
128     }
129
130     return 0xFFFFFFFF;
131 }
132
133 #ifdef HAVE_MPG123_H
134
135 typedef struct tagAcmMpeg3Data
136 {
137     void (*convert)(PACMDRVSTREAMINSTANCE adsi,
138                     const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
139     mpg123_handle *mh;
140 } AcmMpeg3Data;
141
142 /***********************************************************************
143  *           MPEG3_drvOpen
144  */
145 static LRESULT MPEG3_drvOpen(LPCSTR str)
146 {
147     mpg123_init();
148     return 1;
149 }
150
151 /***********************************************************************
152  *           MPEG3_drvClose
153  */
154 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
155 {
156     mpg123_exit();
157     return 1;
158 }
159
160
161 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
162                       const unsigned char* src, LPDWORD nsrc,
163                       unsigned char* dst, LPDWORD ndst)
164 {
165     AcmMpeg3Data*       amd = (AcmMpeg3Data*)adsi->dwDriver;
166     int                 ret;
167     size_t              size;
168     DWORD               dpos = 0;
169
170
171     if (*nsrc > 0)
172     {
173         ret = mpg123_feed(amd->mh, src, *nsrc);
174         if (ret != MPG123_OK)
175         {
176             ERR("Error feeding data\n");
177             *ndst = *nsrc = 0;
178             return;
179         }
180     }
181
182     do {
183         size = 0;
184         ret = mpg123_read(amd->mh, dst + dpos, *ndst - dpos, &size);
185         if (ret == MPG123_ERR)
186         {
187             FIXME("Error occurred during decoding!\n");
188             *ndst = *nsrc = 0;
189             return;
190         }
191
192         if (ret == MPG123_NEW_FORMAT)
193         {
194             long rate;
195             int channels, enc;
196             mpg123_getformat(amd->mh, &rate, &channels, &enc);
197             TRACE("New format: %li Hz, %i channels, encoding value %i\n", rate, channels, enc);
198         }
199         dpos += size;
200         if (dpos > *ndst) break;
201     } while (ret != MPG123_ERR && ret != MPG123_NEED_MORE);
202     *ndst = dpos;
203 }
204
205 /***********************************************************************
206  *           MPEG3_Reset
207  *
208  */
209 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
210 {
211     mpg123_feedseek(aad->mh, 0, SEEK_SET, NULL);
212     mpg123_close(aad->mh);
213     mpg123_open_feed(aad->mh);
214 }
215
216 /***********************************************************************
217  *           MPEG3_StreamOpen
218  *
219  */
220 static  LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
221 {
222     AcmMpeg3Data*       aad;
223     int err;
224
225     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
226
227     if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
228         MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
229         return ACMERR_NOTPOSSIBLE;
230
231     aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
232     if (aad == 0) return MMSYSERR_NOMEM;
233
234     adsi->dwDriver = (DWORD_PTR)aad;
235
236     if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
237         adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
238     {
239         goto theEnd;
240     }
241     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
242              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
243     {
244         /* resampling or mono <=> stereo not available
245          * MPEG3 algo only define 16 bit per sample output
246          */
247         if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
248             adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
249             adsi->pwfxDst->wBitsPerSample != 16)
250             goto theEnd;
251         aad->convert = mp3_horse;
252         aad->mh = mpg123_new(NULL,&err);
253         mpg123_open_feed(aad->mh);
254     }
255     /* no encoding yet
256     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
257              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
258     */
259     else goto theEnd;
260     MPEG3_Reset(adsi, aad);
261
262     return MMSYSERR_NOERROR;
263
264  theEnd:
265     HeapFree(GetProcessHeap(), 0, aad);
266     adsi->dwDriver = 0L;
267     return MMSYSERR_NOTSUPPORTED;
268 }
269
270 /***********************************************************************
271  *           MPEG3_StreamClose
272  *
273  */
274 static  LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
275 {
276     mpg123_close(((AcmMpeg3Data*)adsi->dwDriver)->mh);
277     mpg123_delete(((AcmMpeg3Data*)adsi->dwDriver)->mh);
278     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
279     return MMSYSERR_NOERROR;
280 }
281
282 #elif defined(HAVE_AUDIOFILESTREAMOPEN)
283
284 typedef struct tagAcmMpeg3Data
285 {
286     LRESULT (*convert)(PACMDRVSTREAMINSTANCE adsi,
287             const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
288     AudioConverterRef acr;
289     AudioStreamBasicDescription in,out;
290     AudioFileStreamID afs;
291
292     AudioBufferList outBuffer;
293     AudioBuffer inBuffer;
294
295     UInt32 NumberPackets;
296     AudioStreamPacketDescription *PacketDescriptions;
297
298     OSStatus lastError;
299 } AcmMpeg3Data;
300
301 static inline const char* wine_dbgstr_fourcc(ULONG fourcc)
302 {
303     char buf[4] = { (char) (fourcc >> 24), (char) (fourcc >> 16),
304                     (char) (fourcc >> 8),  (char) fourcc };
305     return wine_dbgstr_an(buf, sizeof(buf));
306 }
307
308 /***********************************************************************
309  *           MPEG3_drvOpen
310  */
311 static LRESULT MPEG3_drvOpen(LPCSTR str)
312 {
313     return 1;
314 }
315
316 /***********************************************************************
317  *           MPEG3_drvClose
318  */
319 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
320 {
321     return 1;
322 }
323
324
325 static OSStatus Mp3AudioConverterComplexInputDataProc (
326    AudioConverterRef             inAudioConverter,
327    UInt32                        *ioNumberDataPackets,
328    AudioBufferList               *ioData,
329    AudioStreamPacketDescription  **outDataPacketDescription,
330    void                          *inUserData
331 )
332 {
333     AcmMpeg3Data *amd = (AcmMpeg3Data*)inUserData;
334
335     if (amd->inBuffer.mDataByteSize > 0)
336     {
337         *ioNumberDataPackets = amd->NumberPackets;
338         ioData->mNumberBuffers = 1;
339         ioData->mBuffers[0].mDataByteSize =  amd->inBuffer.mDataByteSize;
340         ioData->mBuffers[0].mData =  amd->inBuffer.mData;
341         ioData->mBuffers[0].mNumberChannels =  amd->inBuffer.mNumberChannels;
342         amd->inBuffer.mDataByteSize = 0;
343         if (outDataPacketDescription)
344             *outDataPacketDescription = amd->PacketDescriptions;
345     }
346     else
347         *ioNumberDataPackets = 0;
348     return noErr;
349 }
350
351 static LRESULT mp3_leopard_horse(PACMDRVSTREAMINSTANCE adsi,
352                       const unsigned char* src, LPDWORD nsrc,
353                       unsigned char* dst, LPDWORD ndst)
354 {
355     AcmMpeg3Data*       amd = (AcmMpeg3Data*)adsi->dwDriver;
356     OSStatus            ret;
357
358     TRACE("ndst %u %p  <-  %u %p\n",*ndst,dst,*nsrc, src);
359     amd->outBuffer.mNumberBuffers = 1;
360     amd->outBuffer.mBuffers[0].mDataByteSize =  *ndst;
361     amd->outBuffer.mBuffers[0].mData = dst;
362     amd->outBuffer.mBuffers[0].mNumberChannels =  amd->out.mChannelsPerFrame;
363
364     memset(dst,0xff,*ndst);
365     ret = AudioFileStreamParseBytes( amd->afs, *nsrc, src, 0 );
366
367     if (ret != noErr)
368     {
369         *nsrc = 0;
370         ERR("Feed Error %s\n", wine_dbgstr_fourcc(ret));
371         return MMSYSERR_ERROR;
372     }
373     else if (amd->lastError != noErr)
374     {
375         *nsrc = 0;
376         ERR("Error during feed %s\n", wine_dbgstr_fourcc(ret));
377         return MMSYSERR_ERROR;
378     }
379
380     *ndst = amd->outBuffer.mBuffers[0].mDataByteSize;
381     *nsrc = amd->inBuffer.mDataByteSize;
382
383     return MMSYSERR_NOERROR;
384 }
385
386 /***********************************************************************
387  *           MPEG3_Reset
388  *
389  */
390 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
391 {
392     SInt64 offset;
393     AudioConverterReset(aad->acr);
394     AudioFileStreamSeek(aad->afs, 0, &offset, 0);
395 }
396
397 static void Mp3PropertyListenerProc ( void *inClientData,
398            AudioFileStreamID inAudioFileStream, UInt32 inPropertyID, UInt32 *ioFlags)
399 {
400     /* No operation at this time */
401 }
402
403 static void Mp3PacketsProc ( void *inClientData, UInt32 inNumberBytes,
404    UInt32 inNumberPackets, const void *inInputData,
405    AudioStreamPacketDescription *inPacketDescriptions)
406 {
407     AcmMpeg3Data *amd = (AcmMpeg3Data*)inClientData;
408     UInt32 size;
409
410     amd->inBuffer.mDataByteSize = inNumberBytes;
411     amd->inBuffer.mData = (void*)inInputData;
412     amd->inBuffer.mNumberChannels =  amd->in.mChannelsPerFrame;
413
414     amd->NumberPackets = inNumberPackets;
415     amd->PacketDescriptions = inPacketDescriptions;
416
417     size = amd->outBuffer.mBuffers[0].mDataByteSize / amd->out.mBytesPerPacket;
418     amd->lastError = AudioConverterFillComplexBuffer(amd->acr, Mp3AudioConverterComplexInputDataProc, inClientData, &size, &amd->outBuffer, NULL);
419 }
420
421 /***********************************************************************
422  *           MPEG3_StreamOpen
423  *
424  */
425 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
426 {
427     AcmMpeg3Data* aad;
428
429     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
430
431     if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
432         MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
433         return ACMERR_NOTPOSSIBLE;
434
435     aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
436     if (aad == 0) return MMSYSERR_NOMEM;
437
438     adsi->dwDriver = (DWORD_PTR)aad;
439
440     if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
441         adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
442     {
443         goto theEnd;
444     }
445     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
446              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
447     {
448         OSStatus err;
449
450         aad->in.mSampleRate = adsi->pwfxSrc->nSamplesPerSec;
451         aad->out.mSampleRate = adsi->pwfxDst->nSamplesPerSec;
452         aad->in.mBitsPerChannel = adsi->pwfxSrc->wBitsPerSample;
453         aad->out.mBitsPerChannel = adsi->pwfxDst->wBitsPerSample;
454         aad->in.mFormatID = kAudioFormatMPEGLayer3;
455         aad->out.mFormatID = kAudioFormatLinearPCM;
456         aad->in.mChannelsPerFrame = adsi->pwfxSrc->nChannels;
457         aad->out.mChannelsPerFrame = adsi->pwfxDst->nChannels;
458         aad->in.mFormatFlags = 0;
459         aad->out.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
460         aad->in.mBytesPerFrame = 0;
461
462         aad->out.mBytesPerFrame = (aad->out.mBitsPerChannel * aad->out.mChannelsPerFrame) / 8;
463         aad->in.mBytesPerPacket =  0;
464         aad->out.mBytesPerPacket = aad->out.mBytesPerFrame;
465         aad->in.mFramesPerPacket = 0;
466         aad->out.mFramesPerPacket = 1;
467         aad->in.mReserved = aad->out.mReserved = 0;
468
469         aad->acr = NULL;
470
471         err = AudioConverterNew(&aad->in, &aad->out ,&aad->acr);
472         if (err != noErr)
473         {
474             ERR("Create failed: %s\n", wine_dbgstr_fourcc(err));
475             goto theEnd;
476         }
477         else
478         {
479             aad->convert = mp3_leopard_horse;
480             err = AudioFileStreamOpen(aad, Mp3PropertyListenerProc, Mp3PacketsProc, kAudioFormatMPEGLayer3, &aad->afs);
481             if (err != noErr)
482             {
483                 ERR("Stream Open failed: %s\n", wine_dbgstr_fourcc(err));
484                 goto theEnd;
485             }
486         }
487     }
488     else goto theEnd;
489     MPEG3_Reset(adsi, aad);
490
491     return MMSYSERR_NOERROR;
492
493  theEnd:
494     HeapFree(GetProcessHeap(), 0, aad);
495     adsi->dwDriver = 0L;
496     return MMSYSERR_NOTSUPPORTED;
497 }
498
499 /***********************************************************************
500  *           MPEG3_StreamClose
501  *
502  */
503 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
504 {
505     AudioConverterDispose(((AcmMpeg3Data*)adsi->dwDriver)->acr);
506     AudioFileStreamClose(((AcmMpeg3Data*)adsi->dwDriver)->afs);
507     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
508     return MMSYSERR_NOERROR;
509 }
510
511 #endif
512
513 /***********************************************************************
514  *           MPEG3_DriverDetails
515  *
516  */
517 static  LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
518 {
519     add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
520     add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
521     add->wMid = 0xFF;
522     add->wPid = 0x00;
523     add->vdwACM = 0x01000000;
524     add->vdwDriver = 0x01000000;
525     add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
526     add->cFormatTags = 2; /* PCM, MPEG3 */
527     add->cFilterTags = 0;
528     add->hicon = NULL;
529     MultiByteToWideChar( CP_ACP, 0, "WINE-MPEG3", -1,
530                          add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
531     MultiByteToWideChar( CP_ACP, 0, "Wine MPEG3 decoder", -1,
532                          add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
533     MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
534                          add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
535     MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
536                          add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
537     add->szFeatures[0] = 0;
538
539     return MMSYSERR_NOERROR;
540 }
541
542 /***********************************************************************
543  *           MPEG3_FormatTagDetails
544  *
545  */
546 static  LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
547 {
548     static const WCHAR szPcm[]={'P','C','M',0};
549     static const WCHAR szMpeg3[]={'M','P','e','g','3',0};
550
551     switch (dwQuery)
552     {
553     case ACM_FORMATTAGDETAILSF_INDEX:
554         if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
555         break;
556     case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
557         if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
558         {
559             aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_MPEGLAYER3 is bigger than PCM */
560             break;
561         }
562         /* fall thru */
563     case ACM_FORMATTAGDETAILSF_FORMATTAG:
564         switch (aftd->dwFormatTag)
565         {
566         case WAVE_FORMAT_PCM:           aftd->dwFormatTagIndex = 0; break;
567         case WAVE_FORMAT_MPEGLAYER3:    aftd->dwFormatTagIndex = 1; break;
568         default:                        return ACMERR_NOTPOSSIBLE;
569         }
570         break;
571     default:
572         WARN("Unsupported query %08x\n", dwQuery);
573         return MMSYSERR_NOTSUPPORTED;
574     }
575
576     aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
577     switch (aftd->dwFormatTagIndex)
578     {
579     case 0:
580         aftd->dwFormatTag = WAVE_FORMAT_PCM;
581         aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
582         aftd->cStandardFormats = NUM_PCM_FORMATS;
583         lstrcpyW(aftd->szFormatTag, szPcm);
584         break;
585     case 1:
586         aftd->dwFormatTag = WAVE_FORMAT_MPEGLAYER3;
587         aftd->cbFormatSize = sizeof(MPEGLAYER3WAVEFORMAT);
588         aftd->cStandardFormats = NUM_MPEG3_FORMATS;
589         lstrcpyW(aftd->szFormatTag, szMpeg3);
590         break;
591     }
592     return MMSYSERR_NOERROR;
593 }
594
595 static void fill_in_wfx(unsigned cbwfx, WAVEFORMATEX* wfx, unsigned bit_rate)
596 {
597     MPEGLAYER3WAVEFORMAT*   mp3wfx = (MPEGLAYER3WAVEFORMAT*)wfx;
598
599     wfx->nAvgBytesPerSec = bit_rate / 8;
600     if (cbwfx >= sizeof(WAVEFORMATEX))
601         wfx->cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);
602     if (cbwfx >= sizeof(MPEGLAYER3WAVEFORMAT))
603     {
604         mp3wfx->wID = MPEGLAYER3_ID_MPEG;
605         mp3wfx->fdwFlags = MPEGLAYER3_FLAG_PADDING_OFF;
606         mp3wfx->nBlockSize = (bit_rate * 144) / wfx->nSamplesPerSec;
607         mp3wfx->nFramesPerBlock = 1;
608         mp3wfx->nCodecDelay = 0x0571;
609     }
610 }
611
612 /***********************************************************************
613  *           MPEG3_FormatDetails
614  *
615  */
616 static  LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
617 {
618     switch (dwQuery)
619     {
620     case ACM_FORMATDETAILSF_FORMAT:
621         if (MPEG3_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
622         break;
623     case ACM_FORMATDETAILSF_INDEX:
624         afd->pwfx->wFormatTag = afd->dwFormatTag;
625         switch (afd->dwFormatTag)
626         {
627         case WAVE_FORMAT_PCM:
628             if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
629             afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
630             afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
631             afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
632             /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
633              * afd->pwfx->cbSize = 0;
634              */
635             afd->pwfx->nBlockAlign =
636                 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
637             afd->pwfx->nAvgBytesPerSec =
638                 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
639             break;
640         case WAVE_FORMAT_MPEGLAYER3:
641             if (afd->dwFormatIndex >= NUM_MPEG3_FORMATS) return ACMERR_NOTPOSSIBLE;
642             afd->pwfx->nChannels = MPEG3_Formats[afd->dwFormatIndex].nChannels;
643             afd->pwfx->nSamplesPerSec = MPEG3_Formats[afd->dwFormatIndex].rate;
644             afd->pwfx->wBitsPerSample = MPEG3_Formats[afd->dwFormatIndex].nBits;
645             afd->pwfx->nBlockAlign = 1;
646             fill_in_wfx(afd->cbwfx, afd->pwfx, 192000);
647             break;
648         default:
649             WARN("Unsupported tag %08x\n", afd->dwFormatTag);
650             return MMSYSERR_INVALPARAM;
651         }
652         break;
653     default:
654         WARN("Unsupported query %08x\n", dwQuery);
655         return MMSYSERR_NOTSUPPORTED;
656     }
657     afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
658     afd->szFormat[0] = 0; /* let MSACM format this for us... */
659
660     return MMSYSERR_NOERROR;
661 }
662
663 /***********************************************************************
664  *           MPEG3_FormatSuggest
665  *
666  */
667 static  LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
668 {
669     /* some tests ... */
670     if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
671         adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
672         MPEG3_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
673     /* FIXME: should do those tests against the real size (according to format tag */
674
675     /* If no suggestion for destination, then copy source value */
676     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
677         adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
678     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
679         adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
680
681     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
682     {
683         if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
684             adfs->pwfxDst->wBitsPerSample = 4;
685         else
686             adfs->pwfxDst->wBitsPerSample = 16;
687     }
688     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
689     {
690         if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
691             adfs->pwfxDst->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
692         else
693             adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
694     }
695
696     /* check if result is ok */
697     if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
698
699     /* recompute other values */
700     switch (adfs->pwfxDst->wFormatTag)
701     {
702     case WAVE_FORMAT_PCM:
703         adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
704         adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
705         break;
706     case WAVE_FORMAT_MPEGLAYER3:
707         adfs->pwfxDst->nBlockAlign = 1;
708         fill_in_wfx(adfs->cbwfxDst, adfs->pwfxDst, 192000);
709         break;
710     default:
711         FIXME("\n");
712         break;
713     }
714
715     return MMSYSERR_NOERROR;
716 }
717
718 /***********************************************************************
719  *           MPEG3_StreamSize
720  *
721  */
722 static  LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
723 {
724     DWORD nblocks;
725
726     switch (adss->fdwSize)
727     {
728     case ACM_STREAMSIZEF_DESTINATION:
729         /* cbDstLength => cbSrcLength */
730         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
731             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
732         {
733             nblocks = (adss->cbDstLength - 3000) / (DWORD)(adsi->pwfxDst->nAvgBytesPerSec * 1152 / adsi->pwfxDst->nSamplesPerSec + 0.5);
734             if (nblocks == 0)
735                 return ACMERR_NOTPOSSIBLE;
736             adss->cbSrcLength = nblocks * 1152 * adsi->pwfxSrc->nBlockAlign;
737         }
738         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
739                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
740         {
741             nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * 1152);
742             if (nblocks == 0)
743                 return ACMERR_NOTPOSSIBLE;
744             adss->cbSrcLength = nblocks * (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec);
745         }
746         else
747         {
748             return MMSYSERR_NOTSUPPORTED;
749         }
750         break;
751     case ACM_STREAMSIZEF_SOURCE:
752         /* cbSrcLength => cbDstLength */
753         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
754             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
755         {
756             nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * 1152);
757             if (nblocks == 0)
758                 return ACMERR_NOTPOSSIBLE;
759             if (adss->cbSrcLength % (DWORD)(adsi->pwfxSrc->nBlockAlign * 1152))
760                 /* Round block count up. */
761                 nblocks++;
762             adss->cbDstLength = 3000 + nblocks * (DWORD)(adsi->pwfxDst->nAvgBytesPerSec * 1152 / adsi->pwfxDst->nSamplesPerSec + 0.5);
763         }
764         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
765                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
766         {
767             nblocks = adss->cbSrcLength / (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec);
768             if (nblocks == 0)
769                 return ACMERR_NOTPOSSIBLE;
770             if (adss->cbSrcLength % (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec))
771                 /* Round block count up. */
772                 nblocks++;
773             adss->cbDstLength = nblocks * 1152 * adsi->pwfxDst->nBlockAlign;
774         }
775         else
776         {
777             return MMSYSERR_NOTSUPPORTED;
778         }
779         break;
780     default:
781         WARN("Unsupported query %08x\n", adss->fdwSize);
782         return MMSYSERR_NOTSUPPORTED;
783     }
784     return MMSYSERR_NOERROR;
785 }
786
787 /***********************************************************************
788  *           MPEG3_StreamConvert
789  *
790  */
791 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
792 {
793     AcmMpeg3Data*       aad = (AcmMpeg3Data*)adsi->dwDriver;
794     DWORD               nsrc = adsh->cbSrcLength;
795     DWORD               ndst = adsh->cbDstLength;
796
797     if (adsh->fdwConvert &
798         ~(ACM_STREAMCONVERTF_BLOCKALIGN|
799           ACM_STREAMCONVERTF_END|
800           ACM_STREAMCONVERTF_START))
801     {
802         FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
803     }
804     /* ACM_STREAMCONVERTF_BLOCKALIGN
805      *  currently all conversions are block aligned, so do nothing for this flag
806      * ACM_STREAMCONVERTF_END
807      *  no pending data, so do nothing for this flag
808      */
809     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
810     {
811         MPEG3_Reset(adsi, aad);
812     }
813
814     aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
815     adsh->cbSrcLengthUsed = nsrc;
816     adsh->cbDstLengthUsed = ndst;
817
818     return MMSYSERR_NOERROR;
819 }
820
821 /**************************************************************************
822  *                      MPEG3_DriverProc                        [exported]
823  */
824 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
825                                          LPARAM dwParam1, LPARAM dwParam2)
826 {
827     TRACE("(%08lx %p %04x %08lx %08lx);\n",
828           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
829
830     switch (wMsg)
831     {
832     case DRV_LOAD:              return 1;
833     case DRV_FREE:              return 1;
834     case DRV_OPEN:              return MPEG3_drvOpen((LPSTR)dwParam1);
835     case DRV_CLOSE:             return MPEG3_drvClose(dwDevID);
836     case DRV_ENABLE:            return 1;
837     case DRV_DISABLE:           return 1;
838     case DRV_QUERYCONFIGURE:    return 1;
839     case DRV_CONFIGURE:         MessageBoxA(0, "MPEG3 filter !", "Wine Driver", MB_OK); return 1;
840     case DRV_INSTALL:           return DRVCNF_RESTART;
841     case DRV_REMOVE:            return DRVCNF_RESTART;
842
843     case ACMDM_DRIVER_NOTIFY:
844         /* no caching from other ACM drivers is done so far */
845         return MMSYSERR_NOERROR;
846
847     case ACMDM_DRIVER_DETAILS:
848         return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
849
850     case ACMDM_FORMATTAG_DETAILS:
851         return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
852
853     case ACMDM_FORMAT_DETAILS:
854         return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
855
856     case ACMDM_FORMAT_SUGGEST:
857         return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
858
859     case ACMDM_STREAM_OPEN:
860         return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
861
862     case ACMDM_STREAM_CLOSE:
863         return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
864
865     case ACMDM_STREAM_SIZE:
866         return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
867
868     case ACMDM_STREAM_CONVERT:
869         return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
870
871     case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
872     case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
873         /* this converter is not a hardware driver */
874     case ACMDM_FILTERTAG_DETAILS:
875     case ACMDM_FILTER_DETAILS:
876         /* this converter is not a filter */
877     case ACMDM_STREAM_RESET:
878         /* only needed for asynchronous driver... we aren't, so just say it */
879         return MMSYSERR_NOTSUPPORTED;
880     case ACMDM_STREAM_PREPARE:
881     case ACMDM_STREAM_UNPREPARE:
882         /* nothing special to do here... so don't do anything */
883         return MMSYSERR_NOERROR;
884
885     default:
886         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
887     }
888 }