winemp3.acm: Rename to l3codeca.acm.
[wine.git] / dlls / l3codeca.acm / mpegl3.c
blob0e419bd66dc38b5ed2af077ca4a2669670f01ec6
1 /*
2 * MPEG Layer 3 handling
4 * Copyright (C) 2002 Eric Pouech
5 * Copyright (C) 2009 CodeWeavers, Aric Stewart
6 * Copyright (C) 2010 Kristofer Henriksson
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <string.h>
31 #ifdef HAVE_MPG123_H
32 # include <mpg123.h>
33 #else
34 # ifdef HAVE_COREAUDIO_COREAUDIO_H
35 # include <CoreFoundation/CoreFoundation.h>
36 # include <CoreAudio/CoreAudio.h>
37 # endif
38 # ifdef HAVE_AUDIOTOOLBOX_AUDIOCONVERTER_H
39 # include <AudioToolbox/AudioConverter.h>
40 # endif
41 #endif
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wingdi.h"
46 #include "winuser.h"
47 #include "winnls.h"
48 #include "mmsystem.h"
49 #include "mmreg.h"
50 #include "msacm.h"
51 #include "msacmdrv.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
56 /* table to list all supported formats... those are the basic ones. this
57 * also helps given a unique index to each of the supported formats
59 typedef struct
61 int nChannels;
62 int nBits;
63 int rate;
64 } Format;
66 static const Format PCM_Formats[] =
68 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
69 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
70 {1, 8, 12000}, {2, 8, 12000}, {1, 16, 12000}, {2, 16, 12000},
71 {1, 8, 16000}, {2, 8, 16000}, {1, 16, 16000}, {2, 16, 16000},
72 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
73 {1, 8, 24000}, {2, 8, 24000}, {1, 16, 24000}, {2, 16, 24000},
74 {1, 8, 32000}, {2, 8, 32000}, {1, 16, 32000}, {2, 16, 32000},
75 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
76 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}
79 static const Format MPEG3_Formats[] =
81 {1, 0, 8000}, {2, 0, 8000},
82 {1, 0, 11025}, {2, 0, 11025},
83 {1, 0, 12000}, {2, 0, 12000},
84 {1, 0, 16000}, {2, 0, 16000},
85 {1, 0, 22050}, {2, 0, 22050},
86 {1, 0, 24000}, {2, 0, 24000},
87 {1, 0, 32000}, {2, 0, 32000},
88 {1, 0, 44100}, {2, 0, 44100},
89 {1, 0, 48000}, {2, 0, 48000}
92 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
93 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
95 /***********************************************************************
96 * MPEG3_GetFormatIndex
98 static DWORD MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
100 int i, hi;
101 const Format *fmts;
103 switch (wfx->wFormatTag)
105 case WAVE_FORMAT_PCM:
106 hi = NUM_PCM_FORMATS;
107 fmts = PCM_Formats;
108 break;
109 case WAVE_FORMAT_MPEG:
110 case WAVE_FORMAT_MPEGLAYER3:
111 hi = NUM_MPEG3_FORMATS;
112 fmts = MPEG3_Formats;
113 break;
114 default:
115 return 0xFFFFFFFF;
118 for (i = 0; i < hi; i++)
120 if (wfx->nChannels == fmts[i].nChannels &&
121 wfx->nSamplesPerSec == fmts[i].rate &&
122 (wfx->wBitsPerSample == fmts[i].nBits || !fmts[i].nBits))
123 return i;
126 return 0xFFFFFFFF;
129 #ifdef HAVE_MPG123_H
131 typedef struct tagAcmMpeg3Data
133 void (*convert)(PACMDRVSTREAMINSTANCE adsi,
134 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
135 mpg123_handle *mh;
136 } AcmMpeg3Data;
138 /***********************************************************************
139 * MPEG3_drvOpen
141 static LRESULT MPEG3_drvOpen(LPCSTR str)
143 mpg123_init();
144 return 1;
147 /***********************************************************************
148 * MPEG3_drvClose
150 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
152 mpg123_exit();
153 return 1;
157 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
158 const unsigned char* src, LPDWORD nsrc,
159 unsigned char* dst, LPDWORD ndst)
161 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
162 int ret;
163 size_t size;
164 DWORD dpos = 0;
167 if (*nsrc > 0)
169 ret = mpg123_feed(amd->mh, src, *nsrc);
170 if (ret != MPG123_OK)
172 ERR("Error feeding data\n");
173 *ndst = *nsrc = 0;
174 return;
178 do {
179 size = 0;
180 ret = mpg123_read(amd->mh, dst + dpos, *ndst - dpos, &size);
181 if (ret == MPG123_ERR)
183 FIXME("Error occurred during decoding!\n");
184 *ndst = *nsrc = 0;
185 return;
188 if (ret == MPG123_NEW_FORMAT)
190 long rate;
191 int channels, enc;
192 mpg123_getformat(amd->mh, &rate, &channels, &enc);
193 TRACE("New format: %li Hz, %i channels, encoding value %i\n", rate, channels, enc);
195 dpos += size;
196 if (dpos >= *ndst) break;
197 } while (ret != MPG123_ERR && ret != MPG123_NEED_MORE);
198 *ndst = dpos;
201 /***********************************************************************
202 * MPEG3_Reset
205 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
207 mpg123_feedseek(aad->mh, 0, SEEK_SET, NULL);
208 mpg123_close(aad->mh);
209 mpg123_open_feed(aad->mh);
212 /***********************************************************************
213 * MPEG3_StreamOpen
216 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
218 AcmMpeg3Data* aad;
219 int err;
221 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
223 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
224 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
225 return ACMERR_NOTPOSSIBLE;
227 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
228 if (aad == 0) return MMSYSERR_NOMEM;
230 adsi->dwDriver = (DWORD_PTR)aad;
232 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
233 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
235 goto theEnd;
237 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
238 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
239 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
241 /* resampling or mono <=> stereo not available
242 * MPEG3 algo only define 16 bit per sample output
244 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
245 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
246 adsi->pwfxDst->wBitsPerSample != 16)
247 goto theEnd;
248 aad->convert = mp3_horse;
249 aad->mh = mpg123_new(NULL,&err);
250 mpg123_open_feed(aad->mh);
252 #if MPG123_API_VERSION >= 31 /* needed for MPG123_IGNORE_FRAMEINFO enum value */
253 /* mpg123 may find a XING header in the mp3 and use that information
254 * to ask for seeks in order to read specific frames in the file.
255 * We cannot allow that since the caller application is feeding us.
256 * This fixes problems for mp3 files encoded with LAME (bug 42361)
258 mpg123_param(aad->mh, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0);
259 #endif
261 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
262 (adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
263 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEG))
265 WARN("Encoding to MPEG is not supported\n");
266 goto theEnd;
268 else goto theEnd;
269 MPEG3_Reset(adsi, aad);
271 return MMSYSERR_NOERROR;
273 theEnd:
274 HeapFree(GetProcessHeap(), 0, aad);
275 adsi->dwDriver = 0L;
276 return MMSYSERR_NOTSUPPORTED;
279 /***********************************************************************
280 * MPEG3_StreamClose
283 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
285 mpg123_close(((AcmMpeg3Data*)adsi->dwDriver)->mh);
286 mpg123_delete(((AcmMpeg3Data*)adsi->dwDriver)->mh);
287 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
288 return MMSYSERR_NOERROR;
291 #elif defined(HAVE_AUDIOTOOLBOX_AUDIOCONVERTER_H)
293 static const unsigned short Mp3BitRates[2][16] =
295 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0},
296 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
299 static const unsigned short Mp3SampleRates[2][4] =
301 {44100, 48000, 32000, 0},
302 {22050, 24000, 16000, 0}
305 typedef struct tagAcmMpeg3Data
307 LRESULT (*convert)(PACMDRVSTREAMINSTANCE adsi, unsigned char*,
308 LPDWORD, unsigned char*, LPDWORD);
309 AudioConverterRef acr;
310 AudioStreamBasicDescription in,out;
312 AudioBufferList outBuffer;
313 AudioBuffer inBuffer;
315 SInt32 tagBytesLeft;
317 UInt32 NumberPackets;
318 AudioStreamPacketDescription *PacketDescriptions;
319 } AcmMpeg3Data;
321 /***********************************************************************
322 * MPEG3_drvOpen
324 static LRESULT MPEG3_drvOpen(LPCSTR str)
326 return 1;
329 /***********************************************************************
330 * MPEG3_drvClose
332 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
334 return 1;
338 When it asks for data, give it all we have. If we have no data, we assume
339 we will in the future, so give it no packets and return an error, which
340 signals that we will have more later.
342 static OSStatus Mp3AudioConverterComplexInputDataProc(
343 AudioConverterRef inAudioConverter,
344 UInt32 *ioNumberDataPackets,
345 AudioBufferList *ioData,
346 AudioStreamPacketDescription **outDataPacketDescription,
347 void *inUserData
350 AcmMpeg3Data *amd = (AcmMpeg3Data*)inUserData;
352 if (amd->inBuffer.mDataByteSize > 0)
354 *ioNumberDataPackets = amd->NumberPackets;
355 ioData->mNumberBuffers = 1;
356 ioData->mBuffers[0] = amd->inBuffer;
357 amd->inBuffer.mDataByteSize = 0;
358 if (outDataPacketDescription)
359 *outDataPacketDescription = amd->PacketDescriptions;
360 return noErr;
362 else
364 *ioNumberDataPackets = 0;
365 return -74;
370 Get the length of the current frame. We need to be at the start of a
371 frame now. The buffer must have at least the four bytes for the header.
373 static SInt32 Mp3GetPacketLength(const unsigned char* src)
375 unsigned char mpegv;
376 unsigned short brate, srate;
377 unsigned int size;
380 Check that our position looks like an MP3 header and see which type
381 of MP3 file we have.
383 if (src[0] == 0xff && src[1] >> 1 == 0x7d) mpegv = 0; /* MPEG-1 File */
384 else if (src[0] == 0xff && src[1] >> 1 == 0x79) mpegv = 1; /* MPEG-2 File */
385 else return -1;
387 /* Fill in bit rate and sample rate. */
388 brate = Mp3BitRates[mpegv][(src[2] & 0xf0) >> 4];
389 srate = Mp3SampleRates[mpegv][(src[2] & 0xc) >> 2];
391 /* Certain values for bit rate and sample rate are invalid. */
392 if (brate == 0 || srate == 0) return -1;
394 /* Compute frame size, round down */
395 size = 72 * (2 - mpegv) * brate * 1000 / srate;
397 /* If there is padding, add one byte */
398 if (src[2] & 0x2) return size + 1;
399 else return size;
403 Apple's AudioFileStream does weird things so we deal with parsing the
404 file ourselves. It was also designed for a different use case, so this
405 is not unexpected. We expect to have MP3 data as input (i.e. we can only
406 deal with MPEG-1 or MPEG-2 Layer III), which simplifies parsing a bit. We
407 understand the ID3v2 header and skip over it. Whenever we have data we
408 want to skip at the beginning of the input, we do this by setting *ndst=0
409 and *nsrc to the length of the unwanted data and return no error.
411 static LRESULT mp3_leopard_horse(PACMDRVSTREAMINSTANCE adsi,
412 unsigned char* src, LPDWORD nsrc,
413 unsigned char* dst, LPDWORD ndst)
415 OSStatus err;
416 UInt32 size, aspdi, synci, syncSkip;
417 short framelen[4];
418 const unsigned char* psrc;
419 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
421 TRACE("ndst %u %p <- %u %p\n", *ndst, dst, *nsrc, src);
423 TRACE("First 16 bytes to input: %s\n", wine_dbgstr_an((const char *)src, 16));
425 /* Parse ID3 tag */
426 if (!memcmp(src, "ID3", 3) && amd->tagBytesLeft == -1)
428 amd->tagBytesLeft = (src[6] << 21) + (src[7] << 14) + (src[8] << 7) + src[9];
429 if (src[5] & 0x10) amd->tagBytesLeft += 20; /* There is a footer */
430 else amd->tagBytesLeft += 10;
433 /* Consume the tag */
434 if (amd->tagBytesLeft >= (SInt32)*nsrc)
436 *ndst = 0;
437 amd->tagBytesLeft -= *nsrc;
439 TRACE("All %d bytes of source data is ID3 tag\n", *nsrc);
440 return MMSYSERR_NOERROR;
442 else if (amd->tagBytesLeft > 0)
444 src += amd->tagBytesLeft;
445 *nsrc -= amd->tagBytesLeft;
446 TRACE("Skipping %ld for ID3 tag\n", amd->tagBytesLeft);
450 Sync to initial MP3 frame. The largest possible MP3 frame is 1440.
451 Thus, in the first 1440 bytes we must find the beginning of 3 valid
452 frames in a row unless we reach the end of the file first.
454 syncSkip = 0;
455 for (psrc = src; psrc <= src + *nsrc - 4 && psrc < src + 1440; psrc++)
457 framelen[0] = 0;
458 for (synci = 1;
459 synci < 4 && psrc + framelen[synci-1] < src + *nsrc - 4;
460 synci++)
462 framelen[synci] = Mp3GetPacketLength(psrc + framelen[synci-1]);
463 if (framelen[synci] == -1)
465 synci = 0;
466 break;
468 framelen[synci] += framelen[synci-1];
470 if (synci > 0) /* We synced successfully */
472 if (psrc - src > 0)
474 syncSkip = psrc - src;
475 src += syncSkip;
476 *nsrc -= syncSkip;
477 TRACE("Skipping %ld for frame sync\n", syncSkip);
479 break;
483 if (Mp3GetPacketLength(src) == -1)
485 *ndst = *nsrc = 0;
486 ERR("Frame sync failed. Cannot play file.\n");
487 return MMSYSERR_ERROR;
491 Fill in frame descriptions for all frames. We use an extra pointer
492 to keep track of our position in the input.
495 amd->NumberPackets = 25; /* This is the initial array capacity */
496 amd->PacketDescriptions = HeapAlloc(GetProcessHeap(), 0, amd->NumberPackets * sizeof(AudioStreamPacketDescription));
497 if (amd->PacketDescriptions == 0) return MMSYSERR_NOMEM;
499 for (aspdi = 0, psrc = src;
500 psrc <= src + *nsrc - 4;
501 psrc += amd->PacketDescriptions[aspdi].mDataByteSize, aspdi++)
503 /* Return an error if we can't read the frame header */
504 if (Mp3GetPacketLength(psrc) == -1)
506 *ndst = *nsrc = 0;
507 ERR("Invalid header at %p.\n", psrc);
508 HeapFree(GetProcessHeap(), 0, amd->PacketDescriptions);
509 return MMSYSERR_ERROR;
512 /* If we run out of space, double size and reallocate */
513 if (aspdi >= amd->NumberPackets)
515 amd->NumberPackets *= 2;
516 amd->PacketDescriptions = HeapReAlloc(GetProcessHeap(), 0, amd->PacketDescriptions, amd->NumberPackets * sizeof(AudioStreamPacketDescription));
517 if (amd->PacketDescriptions == 0) return MMSYSERR_NOMEM;
520 /* Fill in packet data */
521 amd->PacketDescriptions[aspdi].mStartOffset = psrc - src;
522 amd->PacketDescriptions[aspdi].mVariableFramesInPacket = 0;
523 amd->PacketDescriptions[aspdi].mDataByteSize = Mp3GetPacketLength(psrc);
525 /* If this brings us past the end, the last one doesn't count */
526 if (psrc + amd->PacketDescriptions[aspdi].mDataByteSize > src + *nsrc) break;
529 /* Fill in correct number of frames */
530 amd->NumberPackets = aspdi;
532 /* Adjust nsrc to only include full frames */
533 *nsrc = psrc - src;
535 amd->inBuffer.mDataByteSize = *nsrc;
536 amd->inBuffer.mData = src;
537 amd->inBuffer.mNumberChannels = amd->in.mChannelsPerFrame;
539 amd->outBuffer.mNumberBuffers = 1;
540 amd->outBuffer.mBuffers[0].mDataByteSize = *ndst;
541 amd->outBuffer.mBuffers[0].mData = dst;
542 amd->outBuffer.mBuffers[0].mNumberChannels = amd->out.mChannelsPerFrame;
544 /* Convert the data */
545 size = amd->outBuffer.mBuffers[0].mDataByteSize / amd->out.mBytesPerPacket;
546 err = AudioConverterFillComplexBuffer(amd->acr, Mp3AudioConverterComplexInputDataProc, amd, &size, &amd->outBuffer, 0);
548 HeapFree(GetProcessHeap(), 0, amd->PacketDescriptions);
550 /* Add skipped bytes back into *nsrc */
551 if (amd->tagBytesLeft > 0)
553 *nsrc += amd->tagBytesLeft;
554 amd->tagBytesLeft = 0;
556 *nsrc += syncSkip;
558 if (err != noErr && err != -74)
560 *ndst = *nsrc = 0;
561 ERR("Feed Error: %ld\n", err);
562 return MMSYSERR_ERROR;
565 *ndst = amd->outBuffer.mBuffers[0].mDataByteSize;
567 TRACE("convert %d -> %d\n", *nsrc, *ndst);
569 return MMSYSERR_NOERROR;
572 /***********************************************************************
573 * MPEG3_Reset
576 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
578 AudioConverterReset(aad->acr);
581 /***********************************************************************
582 * MPEG3_StreamOpen
585 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
587 AcmMpeg3Data* aad;
589 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
591 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
592 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
593 return ACMERR_NOTPOSSIBLE;
595 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
596 if (aad == 0) return MMSYSERR_NOMEM;
598 adsi->dwDriver = (DWORD_PTR)aad;
600 if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
601 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
602 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
604 OSStatus err;
606 aad->in.mSampleRate = adsi->pwfxSrc->nSamplesPerSec;
607 aad->out.mSampleRate = adsi->pwfxDst->nSamplesPerSec;
608 aad->in.mBitsPerChannel = adsi->pwfxSrc->wBitsPerSample;
609 aad->out.mBitsPerChannel = adsi->pwfxDst->wBitsPerSample;
610 aad->in.mFormatID = kAudioFormatMPEGLayer3;
611 aad->out.mFormatID = kAudioFormatLinearPCM;
612 aad->in.mChannelsPerFrame = adsi->pwfxSrc->nChannels;
613 aad->out.mChannelsPerFrame = adsi->pwfxDst->nChannels;
614 aad->in.mFormatFlags = 0;
615 aad->out.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
616 aad->in.mBytesPerFrame = 0;
617 aad->out.mBytesPerFrame = (aad->out.mBitsPerChannel * aad->out.mChannelsPerFrame) / 8;
618 aad->in.mBytesPerPacket = 0;
619 aad->out.mBytesPerPacket = aad->out.mBytesPerFrame;
620 aad->in.mFramesPerPacket = 0;
621 aad->out.mFramesPerPacket = 1;
622 aad->in.mReserved = aad->out.mReserved = 0;
624 aad->tagBytesLeft = -1;
626 aad->convert = mp3_leopard_horse;
628 err = AudioConverterNew(&aad->in, &aad->out, &aad->acr);
629 if (err != noErr)
631 ERR("Create failed: %ld\n", err);
633 else
635 MPEG3_Reset(adsi, aad);
637 return MMSYSERR_NOERROR;
641 HeapFree(GetProcessHeap(), 0, aad);
642 adsi->dwDriver = 0;
644 return MMSYSERR_NOTSUPPORTED;
647 /***********************************************************************
648 * MPEG3_StreamClose
651 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
653 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
655 AudioConverterDispose(amd->acr);
657 HeapFree(GetProcessHeap(), 0, amd);
658 adsi->dwDriver = 0;
660 return MMSYSERR_NOERROR;
663 #endif
665 /***********************************************************************
666 * MPEG3_DriverDetails
669 static LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
671 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
672 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
673 add->wMid = MM_FRAUNHOFER_IIS;
674 add->wPid = MM_FHGIIS_MPEGLAYER3_DECODE;
675 add->vdwACM = 0x01000000;
676 add->vdwDriver = 0x01000000;
677 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
678 add->cFormatTags = 3; /* PCM, MPEG3 */
679 add->cFilterTags = 0;
680 add->hicon = NULL;
681 MultiByteToWideChar( CP_ACP, 0, "MPEG Layer-3 Codec", -1,
682 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
683 MultiByteToWideChar( CP_ACP, 0, "Wine MPEG3 decoder", -1,
684 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
685 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
686 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
687 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
688 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
689 add->szFeatures[0] = 0;
691 return MMSYSERR_NOERROR;
694 /***********************************************************************
695 * MPEG3_FormatTagDetails
698 static LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
700 static const WCHAR szPcm[]={'P','C','M',0};
701 static const WCHAR szMpeg3[]={'M','P','e','g','3',0};
702 static const WCHAR szMpeg[]={'M','P','e','g',0};
704 switch (dwQuery)
706 case ACM_FORMATTAGDETAILSF_INDEX:
707 if (aftd->dwFormatTagIndex > 2) return ACMERR_NOTPOSSIBLE;
708 break;
709 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
710 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
712 aftd->dwFormatTagIndex = 2; /* WAVE_FORMAT_MPEG is biggest */
713 break;
715 /* fall through */
716 case ACM_FORMATTAGDETAILSF_FORMATTAG:
717 switch (aftd->dwFormatTag)
719 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
720 case WAVE_FORMAT_MPEGLAYER3: aftd->dwFormatTagIndex = 1; break;
721 case WAVE_FORMAT_MPEG: aftd->dwFormatTagIndex = 2; break;
722 default: return ACMERR_NOTPOSSIBLE;
724 break;
725 default:
726 WARN("Unsupported query %08x\n", dwQuery);
727 return MMSYSERR_NOTSUPPORTED;
730 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
731 switch (aftd->dwFormatTagIndex)
733 case 0:
734 aftd->dwFormatTag = WAVE_FORMAT_PCM;
735 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
736 aftd->cStandardFormats = NUM_PCM_FORMATS;
737 lstrcpyW(aftd->szFormatTag, szPcm);
738 break;
739 case 1:
740 aftd->dwFormatTag = WAVE_FORMAT_MPEGLAYER3;
741 aftd->cbFormatSize = sizeof(MPEGLAYER3WAVEFORMAT);
742 aftd->cStandardFormats = 0;
743 lstrcpyW(aftd->szFormatTag, szMpeg3);
744 break;
745 case 2:
746 aftd->dwFormatTag = WAVE_FORMAT_MPEG;
747 aftd->cbFormatSize = sizeof(MPEG1WAVEFORMAT);
748 aftd->cStandardFormats = 0;
749 lstrcpyW(aftd->szFormatTag, szMpeg);
750 break;
752 return MMSYSERR_NOERROR;
755 /***********************************************************************
756 * MPEG3_FormatDetails
759 static LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
761 switch (dwQuery)
763 case ACM_FORMATDETAILSF_FORMAT:
764 if (MPEG3_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
765 break;
766 case ACM_FORMATDETAILSF_INDEX:
767 afd->pwfx->wFormatTag = afd->dwFormatTag;
768 switch (afd->dwFormatTag)
770 case WAVE_FORMAT_PCM:
771 if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
772 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
773 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
774 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
775 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
776 * afd->pwfx->cbSize = 0;
778 afd->pwfx->nBlockAlign =
779 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
780 afd->pwfx->nAvgBytesPerSec =
781 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
782 break;
783 case WAVE_FORMAT_MPEGLAYER3:
784 case WAVE_FORMAT_MPEG:
785 WARN("Encoding to MPEG is not supported\n");
786 return ACMERR_NOTPOSSIBLE;
787 default:
788 WARN("Unsupported tag %08x\n", afd->dwFormatTag);
789 return MMSYSERR_INVALPARAM;
791 break;
792 default:
793 WARN("Unsupported query %08x\n", dwQuery);
794 return MMSYSERR_NOTSUPPORTED;
796 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
797 afd->szFormat[0] = 0; /* let MSACM format this for us... */
799 return MMSYSERR_NOERROR;
802 /***********************************************************************
803 * MPEG3_FormatSuggest
806 static LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
808 /* some tests ... */
809 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
810 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
811 MPEG3_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
812 /* FIXME: should do those tests against the real size (according to format tag */
814 /* If no suggestion for destination, then copy source value */
815 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
816 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
817 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
818 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
819 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
820 adfs->pwfxDst->wBitsPerSample = 16;
821 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
823 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
825 WARN("Encoding to MPEG is not supported\n");
826 return ACMERR_NOTPOSSIBLE;
828 else
829 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
832 /* check if result is ok */
833 if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
835 /* recompute other values */
836 switch (adfs->pwfxDst->wFormatTag)
838 case WAVE_FORMAT_PCM:
839 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
840 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
841 break;
842 case WAVE_FORMAT_MPEG:
843 case WAVE_FORMAT_MPEGLAYER3:
844 WARN("Encoding to MPEG is not supported\n");
845 return ACMERR_NOTPOSSIBLE;
846 break;
847 default:
848 FIXME("\n");
849 break;
852 return MMSYSERR_NOERROR;
855 /***********************************************************************
856 * MPEG3_StreamSize
859 static LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
861 DWORD nblocks;
863 switch (adss->fdwSize)
865 case ACM_STREAMSIZEF_DESTINATION:
866 /* cbDstLength => cbSrcLength */
867 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
868 (adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
869 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEG))
871 nblocks = (adss->cbDstLength - 3000) / (DWORD)(adsi->pwfxDst->nAvgBytesPerSec * 1152 / adsi->pwfxDst->nSamplesPerSec + 0.5);
872 if (nblocks == 0)
873 return ACMERR_NOTPOSSIBLE;
874 adss->cbSrcLength = nblocks * 1152 * adsi->pwfxSrc->nBlockAlign;
876 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
877 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
878 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
880 nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * 1152);
881 if (nblocks == 0)
882 return ACMERR_NOTPOSSIBLE;
883 adss->cbSrcLength = nblocks * (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec);
885 else
887 return MMSYSERR_NOTSUPPORTED;
889 break;
890 case ACM_STREAMSIZEF_SOURCE:
891 /* cbSrcLength => cbDstLength */
892 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
893 (adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
894 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEG))
896 nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * 1152);
897 if (adss->cbSrcLength % (DWORD)(adsi->pwfxSrc->nBlockAlign * 1152))
898 /* Round block count up. */
899 nblocks++;
900 if (nblocks == 0)
901 return ACMERR_NOTPOSSIBLE;
902 adss->cbDstLength = 3000 + nblocks * (DWORD)(adsi->pwfxDst->nAvgBytesPerSec * 1152 / adsi->pwfxDst->nSamplesPerSec + 0.5);
904 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
905 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
906 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
908 nblocks = adss->cbSrcLength / (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec);
909 if (adss->cbSrcLength % (DWORD)(adsi->pwfxSrc->nAvgBytesPerSec * 1152 / adsi->pwfxSrc->nSamplesPerSec))
910 /* Round block count up. */
911 nblocks++;
912 if (nblocks == 0)
913 return ACMERR_NOTPOSSIBLE;
914 adss->cbDstLength = nblocks * 1152 * adsi->pwfxDst->nBlockAlign;
916 else
918 return MMSYSERR_NOTSUPPORTED;
920 break;
921 default:
922 WARN("Unsupported query %08x\n", adss->fdwSize);
923 return MMSYSERR_NOTSUPPORTED;
925 return MMSYSERR_NOERROR;
928 /***********************************************************************
929 * MPEG3_StreamConvert
932 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
934 AcmMpeg3Data* aad = (AcmMpeg3Data*)adsi->dwDriver;
935 DWORD nsrc = adsh->cbSrcLength;
936 DWORD ndst = adsh->cbDstLength;
938 if (adsh->fdwConvert &
939 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
940 ACM_STREAMCONVERTF_END|
941 ACM_STREAMCONVERTF_START))
943 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
945 /* ACM_STREAMCONVERTF_BLOCKALIGN
946 * currently all conversions are block aligned, so do nothing for this flag
947 * ACM_STREAMCONVERTF_END
948 * no pending data, so do nothing for this flag
950 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
952 MPEG3_Reset(adsi, aad);
955 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
956 adsh->cbSrcLengthUsed = nsrc;
957 adsh->cbDstLengthUsed = ndst;
959 return MMSYSERR_NOERROR;
962 /**************************************************************************
963 * MPEG3_DriverProc [exported]
965 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
966 LPARAM dwParam1, LPARAM dwParam2)
968 TRACE("(%08lx %p %04x %08lx %08lx);\n",
969 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
971 switch (wMsg)
973 case DRV_LOAD: return 1;
974 case DRV_FREE: return 1;
975 case DRV_OPEN: return MPEG3_drvOpen((LPSTR)dwParam1);
976 case DRV_CLOSE: return MPEG3_drvClose(dwDevID);
977 case DRV_ENABLE: return 1;
978 case DRV_DISABLE: return 1;
979 case DRV_QUERYCONFIGURE: return 1;
980 case DRV_CONFIGURE: MessageBoxA(0, "MPEG3 filter !", "Wine Driver", MB_OK); return 1;
981 case DRV_INSTALL: return DRVCNF_RESTART;
982 case DRV_REMOVE: return DRVCNF_RESTART;
984 case ACMDM_DRIVER_NOTIFY:
985 /* no caching from other ACM drivers is done so far */
986 return MMSYSERR_NOERROR;
988 case ACMDM_DRIVER_DETAILS:
989 return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
991 case ACMDM_FORMATTAG_DETAILS:
992 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
994 case ACMDM_FORMAT_DETAILS:
995 return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
997 case ACMDM_FORMAT_SUGGEST:
998 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
1000 case ACMDM_STREAM_OPEN:
1001 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1003 case ACMDM_STREAM_CLOSE:
1004 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1006 case ACMDM_STREAM_SIZE:
1007 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1009 case ACMDM_STREAM_CONVERT:
1010 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1012 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
1013 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
1014 /* this converter is not a hardware driver */
1015 case ACMDM_FILTERTAG_DETAILS:
1016 case ACMDM_FILTER_DETAILS:
1017 /* this converter is not a filter */
1018 case ACMDM_STREAM_RESET:
1019 /* only needed for asynchronous driver... we aren't, so just say it */
1020 return MMSYSERR_NOTSUPPORTED;
1021 case ACMDM_STREAM_PREPARE:
1022 case ACMDM_STREAM_UNPREPARE:
1023 /* nothing special to do here... so don't do anything */
1024 return MMSYSERR_NOERROR;
1026 default:
1027 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);