[WINEMP3.ACM] Sync with Wine 3.0. CORE-14225
[reactos.git] / dll / win32 / 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 * Copyright (C) 2010 Kristofer Henriksson
7 *
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include <config.h>
25 //#include "wine/port.h"
26
27 #include <assert.h>
28 #include <stdarg.h>
29 //#include <string.h>
30
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
42
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>
53
54 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
55
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
58 */
59 typedef struct
60 {
61 int nChannels;
62 int nBits;
63 int rate;
64 } Format;
65
66 static const Format PCM_Formats[] =
67 {
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}
77 };
78
79 static const Format MPEG3_Formats[] =
80 {
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}
90 };
91
92 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
93 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
94
95 /***********************************************************************
96 * MPEG3_GetFormatIndex
97 */
98 static DWORD MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
99 {
100 int i, hi;
101 const Format *fmts;
102
103 switch (wfx->wFormatTag)
104 {
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;
116 }
117
118 for (i = 0; i < hi; i++)
119 {
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;
124 }
125
126 return 0xFFFFFFFF;
127 }
128
129 #ifdef HAVE_MPG123_H
130
131 typedef struct tagAcmMpeg3Data
132 {
133 void (*convert)(PACMDRVSTREAMINSTANCE adsi,
134 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
135 mpg123_handle *mh;
136 } AcmMpeg3Data;
137
138 /***********************************************************************
139 * MPEG3_drvOpen
140 */
141 static LRESULT MPEG3_drvOpen(LPCSTR str)
142 {
143 mpg123_init();
144 return 1;
145 }
146
147 /***********************************************************************
148 * MPEG3_drvClose
149 */
150 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
151 {
152 mpg123_exit();
153 return 1;
154 }
155
156
157 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
158 const unsigned char* src, LPDWORD nsrc,
159 unsigned char* dst, LPDWORD ndst)
160 {
161 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
162 int ret;
163 size_t size;
164 DWORD dpos = 0;
165
166
167 if (*nsrc > 0)
168 {
169 ret = mpg123_feed(amd->mh, src, *nsrc);
170 if (ret != MPG123_OK)
171 {
172 ERR("Error feeding data\n");
173 *ndst = *nsrc = 0;
174 return;
175 }
176 }
177
178 do {
179 size = 0;
180 ret = mpg123_read(amd->mh, dst + dpos, *ndst - dpos, &size);
181 if (ret == MPG123_ERR)
182 {
183 FIXME("Error occurred during decoding!\n");
184 *ndst = *nsrc = 0;
185 return;
186 }
187
188 if (ret == MPG123_NEW_FORMAT)
189 {
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);
194 }
195 dpos += size;
196 if (dpos >= *ndst) break;
197 } while (ret != MPG123_ERR && ret != MPG123_NEED_MORE);
198 *ndst = dpos;
199 }
200
201 /***********************************************************************
202 * MPEG3_Reset
203 *
204 */
205 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
206 {
207 mpg123_feedseek(aad->mh, 0, SEEK_SET, NULL);
208 mpg123_close(aad->mh);
209 mpg123_open_feed(aad->mh);
210 }
211
212 /***********************************************************************
213 * MPEG3_StreamOpen
214 *
215 */
216 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
217 {
218 AcmMpeg3Data* aad;
219 int err;
220
221 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
222
223 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
224 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
225 return ACMERR_NOTPOSSIBLE;
226
227 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
228 if (aad == 0) return MMSYSERR_NOMEM;
229
230 adsi->dwDriver = (DWORD_PTR)aad;
231
232 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
233 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
234 {
235 goto theEnd;
236 }
237 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
238 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
239 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
240 {
241 /* resampling or mono <=> stereo not available
242 * MPEG3 algo only define 16 bit per sample output
243 */
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);
251
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)
257 */
258 mpg123_param(aad->mh, MPG123_ADD_FLAGS, MPG123_IGNORE_INFOFRAME, 0);
259 #endif
260 }
261 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
262 (adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
263 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEG))
264 {
265 WARN("Encoding to MPEG is not supported\n");
266 goto theEnd;
267 }
268 else goto theEnd;
269 MPEG3_Reset(adsi, aad);
270
271 return MMSYSERR_NOERROR;
272
273 theEnd:
274 HeapFree(GetProcessHeap(), 0, aad);
275 adsi->dwDriver = 0L;
276 return MMSYSERR_NOTSUPPORTED;
277 }
278
279 /***********************************************************************
280 * MPEG3_StreamClose
281 *
282 */
283 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
284 {
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;
289 }
290
291 #elif defined(HAVE_AUDIOTOOLBOX_AUDIOCONVERTER_H)
292
293 static const unsigned short Mp3BitRates[2][16] =
294 {
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}
297 };
298
299 static const unsigned short Mp3SampleRates[2][4] =
300 {
301 {44100, 48000, 32000, 0},
302 {22050, 24000, 16000, 0}
303 };
304
305 typedef struct tagAcmMpeg3Data
306 {
307 LRESULT (*convert)(PACMDRVSTREAMINSTANCE adsi, unsigned char*,
308 LPDWORD, unsigned char*, LPDWORD);
309 AudioConverterRef acr;
310 AudioStreamBasicDescription in,out;
311
312 AudioBufferList outBuffer;
313 AudioBuffer inBuffer;
314
315 SInt32 tagBytesLeft;
316
317 UInt32 NumberPackets;
318 AudioStreamPacketDescription *PacketDescriptions;
319 } AcmMpeg3Data;
320
321 /***********************************************************************
322 * MPEG3_drvOpen
323 */
324 static LRESULT MPEG3_drvOpen(LPCSTR str)
325 {
326 return 1;
327 }
328
329 /***********************************************************************
330 * MPEG3_drvClose
331 */
332 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
333 {
334 return 1;
335 }
336
337 /*
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.
341 */
342 static OSStatus Mp3AudioConverterComplexInputDataProc(
343 AudioConverterRef inAudioConverter,
344 UInt32 *ioNumberDataPackets,
345 AudioBufferList *ioData,
346 AudioStreamPacketDescription **outDataPacketDescription,
347 void *inUserData
348 )
349 {
350 AcmMpeg3Data *amd = (AcmMpeg3Data*)inUserData;
351
352 if (amd->inBuffer.mDataByteSize > 0)
353 {
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;
361 }
362 else
363 {
364 *ioNumberDataPackets = 0;
365 return -74;
366 }
367 }
368
369 /*
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.
372 */
373 static SInt32 Mp3GetPacketLength(const unsigned char* src)
374 {
375 unsigned char mpegv;
376 unsigned short brate, srate;
377 unsigned int size;
378
379 /*
380 Check that our position looks like an MP3 header and see which type
381 of MP3 file we have.
382 */
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;
386
387 /* Fill in bit rate and sample rate. */
388 brate = Mp3BitRates[mpegv][(src[2] & 0xf0) >> 4];
389 srate = Mp3SampleRates[mpegv][(src[2] & 0xc) >> 2];
390
391 /* Certain values for bit rate and sample rate are invalid. */
392 if (brate == 0 || srate == 0) return -1;
393
394 /* Compute frame size, round down */
395 size = 72 * (2 - mpegv) * brate * 1000 / srate;
396
397 /* If there is padding, add one byte */
398 if (src[2] & 0x2) return size + 1;
399 else return size;
400 }
401
402 /*
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.
410 */
411 static LRESULT mp3_leopard_horse(PACMDRVSTREAMINSTANCE adsi,
412 unsigned char* src, LPDWORD nsrc,
413 unsigned char* dst, LPDWORD ndst)
414 {
415 OSStatus err;
416 UInt32 size, aspdi, synci, syncSkip;
417 short framelen[4];
418 const unsigned char* psrc;
419 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
420
421 TRACE("ndst %u %p <- %u %p\n", *ndst, dst, *nsrc, src);
422
423 TRACE("First 16 bytes to input: %s\n", wine_dbgstr_an((const char *)src, 16));
424
425 /* Parse ID3 tag */
426 if (!memcmp(src, "ID3", 3) && amd->tagBytesLeft == -1)
427 {
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;
431 }
432
433 /* Consume the tag */
434 if (amd->tagBytesLeft >= (SInt32)*nsrc)
435 {
436 *ndst = 0;
437 amd->tagBytesLeft -= *nsrc;
438
439 TRACE("All %d bytes of source data is ID3 tag\n", *nsrc);
440 return MMSYSERR_NOERROR;
441 }
442 else if (amd->tagBytesLeft > 0)
443 {
444 src += amd->tagBytesLeft;
445 *nsrc -= amd->tagBytesLeft;
446 TRACE("Skipping %ld for ID3 tag\n", amd->tagBytesLeft);
447 }
448
449 /*
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.
453 */
454 syncSkip = 0;
455 for (psrc = src; psrc <= src + *nsrc - 4 && psrc < src + 1440; psrc++)
456 {
457 framelen[0] = 0;
458 for (synci = 1;
459 synci < 4 && psrc + framelen[synci-1] < src + *nsrc - 4;
460 synci++)
461 {
462 framelen[synci] = Mp3GetPacketLength(psrc + framelen[synci-1]);
463 if (framelen[synci] == -1)
464 {
465 synci = 0;
466 break;
467 }
468 framelen[synci] += framelen[synci-1];
469 }
470 if (synci > 0) /* We synced successfully */
471 {
472 if (psrc - src > 0)
473 {
474 syncSkip = psrc - src;
475 src += syncSkip;
476 *nsrc -= syncSkip;
477 TRACE("Skipping %ld for frame sync\n", syncSkip);
478 }
479 break;
480 }
481 }
482
483 if (Mp3GetPacketLength(src) == -1)
484 {
485 *ndst = *nsrc = 0;
486 ERR("Frame sync failed. Cannot play file.\n");
487 return MMSYSERR_ERROR;
488 }
489
490 /*
491 Fill in frame descriptions for all frames. We use an extra pointer
492 to keep track of our position in the input.
493 */
494
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;
498
499 for (aspdi = 0, psrc = src;
500 psrc <= src + *nsrc - 4;
501 psrc += amd->PacketDescriptions[aspdi].mDataByteSize, aspdi++)
502 {
503 /* Return an error if we can't read the frame header */
504 if (Mp3GetPacketLength(psrc) == -1)
505 {
506 *ndst = *nsrc = 0;
507 ERR("Invalid header at %p.\n", psrc);
508 HeapFree(GetProcessHeap(), 0, amd->PacketDescriptions);
509 return MMSYSERR_ERROR;
510 }
511
512 /* If we run out of space, double size and reallocate */
513 if (aspdi >= amd->NumberPackets)
514 {
515 amd->NumberPackets *= 2;
516 amd->PacketDescriptions = HeapReAlloc(GetProcessHeap(), 0, amd->PacketDescriptions, amd->NumberPackets * sizeof(AudioStreamPacketDescription));
517 if (amd->PacketDescriptions == 0) return MMSYSERR_NOMEM;
518 }
519
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);
524
525 /* If this brings us past the end, the last one doesn't count */
526 if (psrc + amd->PacketDescriptions[aspdi].mDataByteSize > src + *nsrc) break;
527 }
528
529 /* Fill in correct number of frames */
530 amd->NumberPackets = aspdi;
531
532 /* Adjust nsrc to only include full frames */
533 *nsrc = psrc - src;
534
535 amd->inBuffer.mDataByteSize = *nsrc;
536 amd->inBuffer.mData = src;
537 amd->inBuffer.mNumberChannels = amd->in.mChannelsPerFrame;
538
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;
543
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);
547
548 HeapFree(GetProcessHeap(), 0, amd->PacketDescriptions);
549
550 /* Add skipped bytes back into *nsrc */
551 if (amd->tagBytesLeft > 0)
552 {
553 *nsrc += amd->tagBytesLeft;
554 amd->tagBytesLeft = 0;
555 }
556 *nsrc += syncSkip;
557
558 if (err != noErr && err != -74)
559 {
560 *ndst = *nsrc = 0;
561 ERR("Feed Error: %ld\n", err);
562 return MMSYSERR_ERROR;
563 }
564
565 *ndst = amd->outBuffer.mBuffers[0].mDataByteSize;
566
567 TRACE("convert %d -> %d\n", *nsrc, *ndst);
568
569 return MMSYSERR_NOERROR;
570 }
571
572 /***********************************************************************
573 * MPEG3_Reset
574 *
575 */
576 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
577 {
578 AudioConverterReset(aad->acr);
579 }
580
581 /***********************************************************************
582 * MPEG3_StreamOpen
583 *
584 */
585 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
586 {
587 AcmMpeg3Data* aad;
588
589 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
590
591 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
592 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
593 return ACMERR_NOTPOSSIBLE;
594
595 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
596 if (aad == 0) return MMSYSERR_NOMEM;
597
598 adsi->dwDriver = (DWORD_PTR)aad;
599
600 if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
601 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
602 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
603 {
604 OSStatus err;
605
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;
623
624 aad->tagBytesLeft = -1;
625
626 aad->convert = mp3_leopard_horse;
627
628 err = AudioConverterNew(&aad->in, &aad->out, &aad->acr);
629 if (err != noErr)
630 {
631 ERR("Create failed: %ld\n", err);
632 }
633 else
634 {
635 MPEG3_Reset(adsi, aad);
636
637 return MMSYSERR_NOERROR;
638 }
639 }
640
641 HeapFree(GetProcessHeap(), 0, aad);
642 adsi->dwDriver = 0;
643
644 return MMSYSERR_NOTSUPPORTED;
645 }
646
647 /***********************************************************************
648 * MPEG3_StreamClose
649 *
650 */
651 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
652 {
653 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
654
655 AudioConverterDispose(amd->acr);
656
657 HeapFree(GetProcessHeap(), 0, amd);
658 adsi->dwDriver = 0;
659
660 return MMSYSERR_NOERROR;
661 }
662
663 #endif
664
665 /***********************************************************************
666 * MPEG3_DriverDetails
667 *
668 */
669 static LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
670 {
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;
690
691 return MMSYSERR_NOERROR;
692 }
693
694 /***********************************************************************
695 * MPEG3_FormatTagDetails
696 *
697 */
698 static LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
699 {
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};
703
704 switch (dwQuery)
705 {
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)
711 {
712 aftd->dwFormatTagIndex = 2; /* WAVE_FORMAT_MPEG is biggest */
713 break;
714 }
715 /* fall through */
716 case ACM_FORMATTAGDETAILSF_FORMATTAG:
717 switch (aftd->dwFormatTag)
718 {
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;
723 }
724 break;
725 default:
726 WARN("Unsupported query %08x\n", dwQuery);
727 return MMSYSERR_NOTSUPPORTED;
728 }
729
730 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
731 switch (aftd->dwFormatTagIndex)
732 {
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;
751 }
752 return MMSYSERR_NOERROR;
753 }
754
755 /***********************************************************************
756 * MPEG3_FormatDetails
757 *
758 */
759 static LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
760 {
761 switch (dwQuery)
762 {
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)
769 {
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;
777 */
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;
790 }
791 break;
792 default:
793 WARN("Unsupported query %08x\n", dwQuery);
794 return MMSYSERR_NOTSUPPORTED;
795 }
796 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
797 afd->szFormat[0] = 0; /* let MSACM format this for us... */
798
799 return MMSYSERR_NOERROR;
800 }
801
802 /***********************************************************************
803 * MPEG3_FormatSuggest
804 *
805 */
806 static LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
807 {
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 */
813
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))
822 {
823 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
824 {
825 WARN("Encoding to MPEG is not supported\n");
826 return ACMERR_NOTPOSSIBLE;
827 }
828 else
829 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
830 }
831
832 /* check if result is ok */
833 if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
834
835 /* recompute other values */
836 switch (adfs->pwfxDst->wFormatTag)
837 {
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;
850 }
851
852 return MMSYSERR_NOERROR;
853 }
854
855 /***********************************************************************
856 * MPEG3_StreamSize
857 *
858 */
859 static LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
860 {
861 DWORD nblocks;
862
863 switch (adss->fdwSize)
864 {
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))
870 {
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;
875 }
876 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
877 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
878 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
879 {
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);
884 }
885 else
886 {
887 return MMSYSERR_NOTSUPPORTED;
888 }
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))
895 {
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);
903 }
904 else if ((adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 ||
905 adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEG) &&
906 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
907 {
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;
915 }
916 else
917 {
918 return MMSYSERR_NOTSUPPORTED;
919 }
920 break;
921 default:
922 WARN("Unsupported query %08x\n", adss->fdwSize);
923 return MMSYSERR_NOTSUPPORTED;
924 }
925 return MMSYSERR_NOERROR;
926 }
927
928 /***********************************************************************
929 * MPEG3_StreamConvert
930 *
931 */
932 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
933 {
934 AcmMpeg3Data* aad = (AcmMpeg3Data*)adsi->dwDriver;
935 DWORD nsrc = adsh->cbSrcLength;
936 DWORD ndst = adsh->cbDstLength;
937
938 if (adsh->fdwConvert &
939 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
940 ACM_STREAMCONVERTF_END|
941 ACM_STREAMCONVERTF_START))
942 {
943 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
944 }
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
949 */
950 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
951 {
952 MPEG3_Reset(adsi, aad);
953 }
954
955 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
956 adsh->cbSrcLengthUsed = nsrc;
957 adsh->cbDstLengthUsed = ndst;
958
959 return MMSYSERR_NOERROR;
960 }
961
962 /**************************************************************************
963 * MPEG3_DriverProc [exported]
964 */
965 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
966 LPARAM dwParam1, LPARAM dwParam2)
967 {
968 TRACE("(%08lx %p %04x %08lx %08lx);\n",
969 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
970
971 switch (wMsg)
972 {
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;
983
984 case ACMDM_DRIVER_NOTIFY:
985 /* no caching from other ACM drivers is done so far */
986 return MMSYSERR_NOERROR;
987
988 case ACMDM_DRIVER_DETAILS:
989 return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
990
991 case ACMDM_FORMATTAG_DETAILS:
992 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
993
994 case ACMDM_FORMAT_DETAILS:
995 return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
996
997 case ACMDM_FORMAT_SUGGEST:
998 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
999
1000 case ACMDM_STREAM_OPEN:
1001 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
1002
1003 case ACMDM_STREAM_CLOSE:
1004 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
1005
1006 case ACMDM_STREAM_SIZE:
1007 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
1008
1009 case ACMDM_STREAM_CONVERT:
1010 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
1011
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;
1025
1026 default:
1027 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1028 }
1029 }