makefiles: Output all the tool install rules explicitly for each module.
[wine/multimedia.git] / dlls / winemp3.acm / mpegl3.c
blob607450119b443a6cec3a9c13309e36dfd7b5cef1
1 /*
2 * MPEG Layer 3 handling
4 * Copyright (C) 2002 Eric Pouech
5 * Copyright (C) 2009 CodeWeavers, Aric Stewart
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
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
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"
59 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
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
64 typedef struct
66 int nChannels;
67 int nBits;
68 int rate;
69 } Format;
71 static const Format PCM_Formats[] =
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}
84 static const Format MPEG3_Formats[] =
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}
97 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
98 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
100 /***********************************************************************
101 * MPEG3_GetFormatIndex
103 static DWORD MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
105 int i, hi;
106 const Format *fmts;
108 switch (wfx->wFormatTag)
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;
122 for (i = 0; i < hi; i++)
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;
130 return 0xFFFFFFFF;
133 #ifdef HAVE_MPG123_H
135 typedef struct tagAcmMpeg3Data
137 void (*convert)(PACMDRVSTREAMINSTANCE adsi,
138 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
139 mpg123_handle *mh;
140 } AcmMpeg3Data;
142 /***********************************************************************
143 * MPEG3_drvOpen
145 static LRESULT MPEG3_drvOpen(LPCSTR str)
147 mpg123_init();
148 return 1;
151 /***********************************************************************
152 * MPEG3_drvClose
154 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
156 mpg123_exit();
157 return 1;
161 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
162 const unsigned char* src, LPDWORD nsrc,
163 unsigned char* dst, LPDWORD ndst)
165 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
166 int ret;
167 size_t size;
168 DWORD dpos = 0;
171 if (*nsrc > 0)
173 ret = mpg123_feed(amd->mh, src, *nsrc);
174 if (ret != MPG123_OK)
176 ERR("Error feeding data\n");
177 *ndst = *nsrc = 0;
178 return;
182 do {
183 size = 0;
184 ret = mpg123_read(amd->mh, dst + dpos, *ndst - dpos, &size);
185 if (ret == MPG123_ERR)
187 FIXME("Error occurred during decoding!\n");
188 *ndst = *nsrc = 0;
189 return;
192 if (ret == MPG123_NEW_FORMAT)
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);
199 dpos += size;
200 if (dpos > *ndst) break;
201 } while (ret != MPG123_ERR && ret != MPG123_NEED_MORE);
202 *ndst = dpos;
205 /***********************************************************************
206 * MPEG3_Reset
209 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
211 mpg123_feedseek(aad->mh, 0, SEEK_SET, NULL);
212 mpg123_close(aad->mh);
213 mpg123_open_feed(aad->mh);
216 /***********************************************************************
217 * MPEG3_StreamOpen
220 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
222 AcmMpeg3Data* aad;
223 int err;
225 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
227 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
228 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
229 return ACMERR_NOTPOSSIBLE;
231 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
232 if (aad == 0) return MMSYSERR_NOMEM;
234 adsi->dwDriver = (DWORD_PTR)aad;
236 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
237 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
239 goto theEnd;
241 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
242 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
244 /* resampling or mono <=> stereo not available
245 * MPEG3 algo only define 16 bit per sample output
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);
255 /* no encoding yet
256 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
257 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
259 else goto theEnd;
260 MPEG3_Reset(adsi, aad);
262 return MMSYSERR_NOERROR;
264 theEnd:
265 HeapFree(GetProcessHeap(), 0, aad);
266 adsi->dwDriver = 0L;
267 return MMSYSERR_NOTSUPPORTED;
270 /***********************************************************************
271 * MPEG3_StreamClose
274 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
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;
282 #elif defined(HAVE_AUDIOFILESTREAMOPEN)
284 typedef struct tagAcmMpeg3Data
286 LRESULT (*convert)(PACMDRVSTREAMINSTANCE adsi,
287 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
288 AudioConverterRef acr;
289 AudioStreamBasicDescription in,out;
290 AudioFileStreamID afs;
292 AudioBufferList outBuffer;
293 AudioBuffer inBuffer;
295 UInt32 NumberPackets;
296 AudioStreamPacketDescription *PacketDescriptions;
298 OSStatus lastError;
299 } AcmMpeg3Data;
301 static inline const char* wine_dbgstr_fourcc(unsigned long fourcc)
303 char buf[4] = { (char) (fourcc >> 24), (char) (fourcc >> 16),
304 (char) (fourcc >> 8), (char) fourcc };
305 return wine_dbgstr_an(buf, sizeof(buf));
308 /***********************************************************************
309 * MPEG3_drvOpen
311 static LRESULT MPEG3_drvOpen(LPCSTR str)
313 return 1;
316 /***********************************************************************
317 * MPEG3_drvClose
319 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
321 return 1;
325 static OSStatus Mp3AudioConverterComplexInputDataProc (
326 AudioConverterRef inAudioConverter,
327 UInt32 *ioNumberDataPackets,
328 AudioBufferList *ioData,
329 AudioStreamPacketDescription **outDataPacketDescription,
330 void *inUserData
333 AcmMpeg3Data *amd = (AcmMpeg3Data*)inUserData;
335 if (amd->inBuffer.mDataByteSize > 0)
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;
346 else
347 *ioNumberDataPackets = 0;
348 return noErr;
351 static LRESULT mp3_leopard_horse(PACMDRVSTREAMINSTANCE adsi,
352 const unsigned char* src, LPDWORD nsrc,
353 unsigned char* dst, LPDWORD ndst)
355 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
356 OSStatus ret;
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;
364 memset(dst,0xff,*ndst);
365 ret = AudioFileStreamParseBytes( amd->afs, *nsrc, src, 0 );
367 if (ret != noErr)
369 *nsrc = 0;
370 ERR("Feed Error %s\n", wine_dbgstr_fourcc(ret));
371 return MMSYSERR_ERROR;
373 else if (amd->lastError != noErr)
375 *nsrc = 0;
376 ERR("Error during feed %s\n", wine_dbgstr_fourcc(ret));
377 return MMSYSERR_ERROR;
380 *ndst = amd->outBuffer.mBuffers[0].mDataByteSize;
381 *nsrc = amd->inBuffer.mDataByteSize;
383 return MMSYSERR_NOERROR;
386 /***********************************************************************
387 * MPEG3_Reset
390 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
392 SInt64 offset;
393 AudioConverterReset(aad->acr);
394 AudioFileStreamSeek(aad->afs, 0, &offset, 0);
397 static void Mp3PropertyListenerProc ( void *inClientData,
398 AudioFileStreamID inAudioFileStream, UInt32 inPropertyID, UInt32 *ioFlags)
400 /* No operation at this time */
403 static void Mp3PacketsProc ( void *inClientData, UInt32 inNumberBytes,
404 UInt32 inNumberPackets, const void *inInputData,
405 AudioStreamPacketDescription *inPacketDescriptions)
407 AcmMpeg3Data *amd = (AcmMpeg3Data*)inClientData;
408 UInt32 size;
410 amd->inBuffer.mDataByteSize = inNumberBytes;
411 amd->inBuffer.mData = (void*)inInputData;
412 amd->inBuffer.mNumberChannels = amd->in.mChannelsPerFrame;
414 amd->NumberPackets = inNumberPackets;
415 amd->PacketDescriptions = inPacketDescriptions;
417 size = amd->outBuffer.mBuffers[0].mDataByteSize / amd->out.mBytesPerPacket;
418 amd->lastError = AudioConverterFillComplexBuffer(amd->acr, Mp3AudioConverterComplexInputDataProc, inClientData, &size, &amd->outBuffer, NULL);
421 /***********************************************************************
422 * MPEG3_StreamOpen
425 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
427 AcmMpeg3Data* aad;
429 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
431 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
432 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
433 return ACMERR_NOTPOSSIBLE;
435 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
436 if (aad == 0) return MMSYSERR_NOMEM;
438 adsi->dwDriver = (DWORD_PTR)aad;
440 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
441 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
443 goto theEnd;
445 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
446 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
448 OSStatus err;
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;
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;
469 aad->acr = NULL;
471 err = AudioConverterNew(&aad->in, &aad->out ,&aad->acr);
472 if (err != noErr)
474 ERR("Create failed: %s\n", wine_dbgstr_fourcc(err));
475 goto theEnd;
477 else
479 aad->convert = mp3_leopard_horse;
480 err = AudioFileStreamOpen(aad, Mp3PropertyListenerProc, Mp3PacketsProc, kAudioFormatMPEGLayer3, &aad->afs);
481 if (err != noErr)
483 ERR("Stream Open failed: %s\n", wine_dbgstr_fourcc(err));
484 goto theEnd;
488 else goto theEnd;
489 MPEG3_Reset(adsi, aad);
491 return MMSYSERR_NOERROR;
493 theEnd:
494 HeapFree(GetProcessHeap(), 0, aad);
495 adsi->dwDriver = 0L;
496 return MMSYSERR_NOTSUPPORTED;
499 /***********************************************************************
500 * MPEG3_StreamClose
503 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
505 AudioConverterDispose(((AcmMpeg3Data*)adsi->dwDriver)->acr);
506 AudioFileStreamClose(((AcmMpeg3Data*)adsi->dwDriver)->afs);
507 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
508 return MMSYSERR_NOERROR;
511 #endif
513 /***********************************************************************
514 * MPEG3_DriverDetails
517 static LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
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;
539 return MMSYSERR_NOERROR;
542 /***********************************************************************
543 * MPEG3_FormatTagDetails
546 static LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
548 static const WCHAR szPcm[]={'P','C','M',0};
549 static const WCHAR szMpeg3[]={'M','P','e','g','3',0};
551 switch (dwQuery)
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)
559 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_MPEGLAYER3 is bigger than PCM */
560 break;
562 /* fall thru */
563 case ACM_FORMATTAGDETAILSF_FORMATTAG:
564 switch (aftd->dwFormatTag)
566 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
567 case WAVE_FORMAT_MPEGLAYER3: aftd->dwFormatTagIndex = 1; break;
568 default: return ACMERR_NOTPOSSIBLE;
570 break;
571 default:
572 WARN("Unsupported query %08x\n", dwQuery);
573 return MMSYSERR_NOTSUPPORTED;
576 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
577 switch (aftd->dwFormatTagIndex)
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;
592 return MMSYSERR_NOERROR;
595 static void fill_in_wfx(unsigned cbwfx, WAVEFORMATEX* wfx, unsigned bit_rate)
597 MPEGLAYER3WAVEFORMAT* mp3wfx = (MPEGLAYER3WAVEFORMAT*)wfx;
599 wfx->nAvgBytesPerSec = bit_rate / 8;
600 if (cbwfx >= sizeof(WAVEFORMATEX))
601 wfx->cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);
602 if (cbwfx >= sizeof(MPEGLAYER3WAVEFORMAT))
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;
612 /***********************************************************************
613 * MPEG3_FormatDetails
616 static LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
618 switch (dwQuery)
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)
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;
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;
652 break;
653 default:
654 WARN("Unsupported query %08x\n", dwQuery);
655 return MMSYSERR_NOTSUPPORTED;
657 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
658 afd->szFormat[0] = 0; /* let MSACM format this for us... */
660 return MMSYSERR_NOERROR;
663 /***********************************************************************
664 * MPEG3_FormatSuggest
667 static LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
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 */
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;
681 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
683 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
684 adfs->pwfxDst->wBitsPerSample = 4;
685 else
686 adfs->pwfxDst->wBitsPerSample = 16;
688 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
690 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
691 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
692 else
693 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
696 /* check if result is ok */
697 if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
699 /* recompute other values */
700 switch (adfs->pwfxDst->wFormatTag)
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;
715 return MMSYSERR_NOERROR;
718 /***********************************************************************
719 * MPEG3_StreamSize
722 static LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
724 DWORD nblocks;
726 switch (adss->fdwSize)
728 case ACM_STREAMSIZEF_DESTINATION:
729 /* cbDstLength => cbSrcLength */
730 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
731 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
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;
738 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
739 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
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);
746 else
748 return MMSYSERR_NOTSUPPORTED;
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)
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);
764 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
765 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
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;
775 else
777 return MMSYSERR_NOTSUPPORTED;
779 break;
780 default:
781 WARN("Unsupported query %08x\n", adss->fdwSize);
782 return MMSYSERR_NOTSUPPORTED;
784 return MMSYSERR_NOERROR;
787 /***********************************************************************
788 * MPEG3_StreamConvert
791 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
793 AcmMpeg3Data* aad = (AcmMpeg3Data*)adsi->dwDriver;
794 DWORD nsrc = adsh->cbSrcLength;
795 DWORD ndst = adsh->cbDstLength;
797 if (adsh->fdwConvert &
798 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
799 ACM_STREAMCONVERTF_END|
800 ACM_STREAMCONVERTF_START))
802 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
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
809 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
811 MPEG3_Reset(adsi, aad);
814 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
815 adsh->cbSrcLengthUsed = nsrc;
816 adsh->cbDstLengthUsed = ndst;
818 return MMSYSERR_NOERROR;
821 /**************************************************************************
822 * MPEG3_DriverProc [exported]
824 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
825 LPARAM dwParam1, LPARAM dwParam2)
827 TRACE("(%08lx %p %04x %08lx %08lx);\n",
828 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
830 switch (wMsg)
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;
843 case ACMDM_DRIVER_NOTIFY:
844 /* no caching from other ACM drivers is done so far */
845 return MMSYSERR_NOERROR;
847 case ACMDM_DRIVER_DETAILS:
848 return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
850 case ACMDM_FORMATTAG_DETAILS:
851 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
853 case ACMDM_FORMAT_DETAILS:
854 return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
856 case ACMDM_FORMAT_SUGGEST:
857 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
859 case ACMDM_STREAM_OPEN:
860 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
862 case ACMDM_STREAM_CLOSE:
863 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
865 case ACMDM_STREAM_SIZE:
866 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
868 case ACMDM_STREAM_CONVERT:
869 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
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;
885 default:
886 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);