2 * MPEG Layer 3 handling
4 * Copyright (C) 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
41 /***********************************************************************
44 static LRESULT MPEG3_drvOpen(LPCSTR str)
49 /***********************************************************************
52 static LRESULT MPEG3_drvClose(DWORD_PTR dwDevID)
57 typedef struct tagAcmMpeg3Data
59 void (*convert)(PACMDRVSTREAMINSTANCE adsi,
60 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
64 /* table to list all supported formats... those are the basic ones. this
65 * also helps given a unique index to each of the supported formats
74 static const Format PCM_Formats[] =
76 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
77 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
78 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
79 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
82 static const Format MPEG3_Formats[] =
84 {1, 0, 8000}, {2, 0, 8000}, {1, 0, 11025}, {2, 0, 11025},
85 {1, 0, 22050}, {2, 0, 22050}, {1, 0, 44100}, {2, 0, 44100},
88 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
89 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
91 /***********************************************************************
92 * MPEG3_GetFormatIndex
94 static DWORD MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
99 switch (wfx->wFormatTag)
101 case WAVE_FORMAT_PCM:
102 hi = NUM_PCM_FORMATS;
105 case WAVE_FORMAT_MPEGLAYER3:
106 hi = NUM_MPEG3_FORMATS;
107 fmts = MPEG3_Formats;
113 for (i = 0; i < hi; i++)
115 if (wfx->nChannels == fmts[i].nChannels &&
116 wfx->nSamplesPerSec == fmts[i].rate &&
117 wfx->wBitsPerSample == fmts[i].nBits)
124 static DWORD get_num_buffered_bytes(struct mpstr *mp)
127 struct buf * p = mp->tail;
129 numBuff += p->size - p->pos;
135 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
136 const unsigned char* src, LPDWORD nsrc,
137 unsigned char* dst, LPDWORD ndst)
139 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
142 DWORD buffered_before;
143 DWORD buffered_during;
144 DWORD buffered_after;
146 buffered_before = get_num_buffered_bytes(&amd->mp);
147 ret = decodeMP3(&amd->mp, src, *nsrc, dst, *ndst, &size);
148 buffered_during = get_num_buffered_bytes(&amd->mp);
156 if (*ndst - dpos < 4608) break;
157 ret = decodeMP3(&amd->mp, NULL, 0,
158 dst + dpos, *ndst - dpos, &size);
159 } while (ret == MP3_OK);
162 buffered_after = get_num_buffered_bytes(&amd->mp);
163 TRACE("before %d put %d during %d after %d\n", buffered_before, *nsrc, buffered_during, buffered_after);
166 /***********************************************************************
167 * MPEG3_DriverDetails
170 static LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
172 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
173 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
176 add->vdwACM = 0x01000000;
177 add->vdwDriver = 0x01000000;
178 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
179 add->cFormatTags = 2; /* PCM, MPEG3 */
180 add->cFilterTags = 0;
182 MultiByteToWideChar( CP_ACP, 0, "WINE-MPEG3", -1,
183 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
184 MultiByteToWideChar( CP_ACP, 0, "Wine MPEG3 decoder", -1,
185 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
186 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team (based on mpglib by Michael Hipp)...", -1,
187 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
188 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
189 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
190 add->szFeatures[0] = 0;
192 return MMSYSERR_NOERROR;
195 /***********************************************************************
196 * MPEG3_FormatTagDetails
199 static LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
201 static const WCHAR szPcm[]={'P','C','M',0};
202 static const WCHAR szMpeg3[]={'M','P','e','g','3',0};
206 case ACM_FORMATTAGDETAILSF_INDEX:
207 if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
209 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
210 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
212 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_MPEGLAYER3 is bigger than PCM */
216 case ACM_FORMATTAGDETAILSF_FORMATTAG:
217 switch (aftd->dwFormatTag)
219 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
220 case WAVE_FORMAT_MPEGLAYER3: aftd->dwFormatTagIndex = 1; break;
221 default: return ACMERR_NOTPOSSIBLE;
225 WARN("Unsupported query %08x\n", dwQuery);
226 return MMSYSERR_NOTSUPPORTED;
229 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
230 switch (aftd->dwFormatTagIndex)
233 aftd->dwFormatTag = WAVE_FORMAT_PCM;
234 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
235 aftd->cStandardFormats = NUM_PCM_FORMATS;
236 lstrcpyW(aftd->szFormatTag, szPcm);
239 aftd->dwFormatTag = WAVE_FORMAT_MPEGLAYER3;
240 aftd->cbFormatSize = sizeof(MPEGLAYER3WAVEFORMAT);
241 aftd->cStandardFormats = NUM_MPEG3_FORMATS;
242 lstrcpyW(aftd->szFormatTag, szMpeg3);
245 return MMSYSERR_NOERROR;
248 static void fill_in_wfx(unsigned cbwfx, WAVEFORMATEX* wfx, unsigned bit_rate)
250 MPEGLAYER3WAVEFORMAT* mp3wfx = (MPEGLAYER3WAVEFORMAT*)wfx;
252 wfx->nAvgBytesPerSec = bit_rate / 8;
253 if (cbwfx >= sizeof(WAVEFORMATEX))
254 wfx->cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);
255 if (cbwfx >= sizeof(MPEGLAYER3WAVEFORMAT))
257 mp3wfx->wID = MPEGLAYER3_ID_MPEG;
258 mp3wfx->fdwFlags = MPEGLAYER3_FLAG_PADDING_OFF;
259 mp3wfx->nBlockSize = (bit_rate * 144) / wfx->nSamplesPerSec;
260 mp3wfx->nFramesPerBlock = 1;
261 mp3wfx->nCodecDelay = 0x0571;
265 /***********************************************************************
266 * MPEG3_FormatDetails
269 static LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
273 case ACM_FORMATDETAILSF_FORMAT:
274 if (MPEG3_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
276 case ACM_FORMATDETAILSF_INDEX:
277 afd->pwfx->wFormatTag = afd->dwFormatTag;
278 switch (afd->dwFormatTag)
280 case WAVE_FORMAT_PCM:
281 if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
282 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
283 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
284 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
285 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
286 * afd->pwfx->cbSize = 0;
288 afd->pwfx->nBlockAlign =
289 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
290 afd->pwfx->nAvgBytesPerSec =
291 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
293 case WAVE_FORMAT_MPEGLAYER3:
294 if (afd->dwFormatIndex >= NUM_MPEG3_FORMATS) return ACMERR_NOTPOSSIBLE;
295 afd->pwfx->nChannels = MPEG3_Formats[afd->dwFormatIndex].nChannels;
296 afd->pwfx->nSamplesPerSec = MPEG3_Formats[afd->dwFormatIndex].rate;
297 afd->pwfx->wBitsPerSample = MPEG3_Formats[afd->dwFormatIndex].nBits;
298 afd->pwfx->nBlockAlign = 1;
299 fill_in_wfx(afd->cbwfx, afd->pwfx, 192000);
302 WARN("Unsupported tag %08x\n", afd->dwFormatTag);
303 return MMSYSERR_INVALPARAM;
307 WARN("Unsupported query %08x\n", dwQuery);
308 return MMSYSERR_NOTSUPPORTED;
310 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
311 afd->szFormat[0] = 0; /* let MSACM format this for us... */
313 return MMSYSERR_NOERROR;
316 /***********************************************************************
317 * MPEG3_FormatSuggest
320 static LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
323 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
324 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
325 MPEG3_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
326 /* FIXME: should do those tests against the real size (according to format tag */
328 /* If no suggestion for destination, then copy source value */
329 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
330 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
331 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
332 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
334 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
336 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
337 adfs->pwfxDst->wBitsPerSample = 4;
339 adfs->pwfxDst->wBitsPerSample = 16;
341 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
343 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
344 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
346 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
349 /* check if result is ok */
350 if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
352 /* recompute other values */
353 switch (adfs->pwfxDst->wFormatTag)
355 case WAVE_FORMAT_PCM:
356 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
357 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
359 case WAVE_FORMAT_MPEGLAYER3:
360 adfs->pwfxDst->nBlockAlign = 1;
361 fill_in_wfx(adfs->cbwfxDst, adfs->pwfxDst, 192000);
368 return MMSYSERR_NOERROR;
371 /***********************************************************************
375 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
379 /***********************************************************************
383 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
387 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
389 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
390 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
391 return ACMERR_NOTPOSSIBLE;
393 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
394 if (aad == 0) return MMSYSERR_NOMEM;
396 adsi->dwDriver = (DWORD)aad;
398 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
399 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
403 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
404 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
406 /* resampling or mono <=> stereo not available
407 * MPEG3 algo only define 16 bit per sample output
409 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
410 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
411 adsi->pwfxDst->wBitsPerSample != 16)
413 aad->convert = mp3_horse;
417 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
418 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
421 MPEG3_Reset(adsi, aad);
423 return MMSYSERR_NOERROR;
426 HeapFree(GetProcessHeap(), 0, aad);
428 return MMSYSERR_NOTSUPPORTED;
431 /***********************************************************************
435 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
437 ExitMP3(&((AcmMpeg3Data*)adsi->dwDriver)->mp);
438 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
439 return MMSYSERR_NOERROR;
442 /***********************************************************************
446 static LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
448 switch (adss->fdwSize)
450 case ACM_STREAMSIZEF_DESTINATION:
451 /* cbDstLength => cbSrcLength */
452 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
453 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
455 /* don't take block overhead into account, doesn't matter too much */
456 adss->cbSrcLength = adss->cbDstLength * 12;
458 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
459 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
461 FIXME("misses the block header overhead\n");
462 adss->cbSrcLength = 256 + adss->cbDstLength / 12;
466 return MMSYSERR_NOTSUPPORTED;
469 case ACM_STREAMSIZEF_SOURCE:
470 /* cbSrcLength => cbDstLength */
471 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
472 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
474 FIXME("misses the block header overhead\n");
475 adss->cbDstLength = 256 + adss->cbSrcLength / 12;
477 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
478 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
480 /* don't take block overhead into account, doesn't matter too much */
481 adss->cbDstLength = adss->cbSrcLength * 12;
485 return MMSYSERR_NOTSUPPORTED;
489 WARN("Unsupported query %08x\n", adss->fdwSize);
490 return MMSYSERR_NOTSUPPORTED;
492 return MMSYSERR_NOERROR;
495 /***********************************************************************
496 * MPEG3_StreamConvert
499 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
501 AcmMpeg3Data* aad = (AcmMpeg3Data*)adsi->dwDriver;
502 DWORD nsrc = adsh->cbSrcLength;
503 DWORD ndst = adsh->cbDstLength;
505 if (adsh->fdwConvert &
506 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
507 ACM_STREAMCONVERTF_END|
508 ACM_STREAMCONVERTF_START))
510 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
512 /* ACM_STREAMCONVERTF_BLOCKALIGN
513 * currently all conversions are block aligned, so do nothing for this flag
514 * ACM_STREAMCONVERTF_END
515 * no pending data, so do nothing for this flag
517 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
519 MPEG3_Reset(adsi, aad);
522 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
523 adsh->cbSrcLengthUsed = nsrc;
524 adsh->cbDstLengthUsed = ndst;
526 return MMSYSERR_NOERROR;
529 /**************************************************************************
530 * MPEG3_DriverProc [exported]
532 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
533 LPARAM dwParam1, LPARAM dwParam2)
535 TRACE("(%08lx %p %04x %08lx %08lx);\n",
536 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
540 case DRV_LOAD: return 1;
541 case DRV_FREE: return 1;
542 case DRV_OPEN: return MPEG3_drvOpen((LPSTR)dwParam1);
543 case DRV_CLOSE: return MPEG3_drvClose(dwDevID);
544 case DRV_ENABLE: return 1;
545 case DRV_DISABLE: return 1;
546 case DRV_QUERYCONFIGURE: return 1;
547 case DRV_CONFIGURE: MessageBoxA(0, "MPEG3 filter !", "Wine Driver", MB_OK); return 1;
548 case DRV_INSTALL: return DRVCNF_RESTART;
549 case DRV_REMOVE: return DRVCNF_RESTART;
551 case ACMDM_DRIVER_NOTIFY:
552 /* no caching from other ACM drivers is done so far */
553 return MMSYSERR_NOERROR;
555 case ACMDM_DRIVER_DETAILS:
556 return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
558 case ACMDM_FORMATTAG_DETAILS:
559 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
561 case ACMDM_FORMAT_DETAILS:
562 return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
564 case ACMDM_FORMAT_SUGGEST:
565 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
567 case ACMDM_STREAM_OPEN:
568 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
570 case ACMDM_STREAM_CLOSE:
571 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
573 case ACMDM_STREAM_SIZE:
574 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
576 case ACMDM_STREAM_CONVERT:
577 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
579 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
580 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
581 /* this converter is not a hardware driver */
582 case ACMDM_FILTERTAG_DETAILS:
583 case ACMDM_FILTER_DETAILS:
584 /* this converter is not a filter */
585 case ACMDM_STREAM_RESET:
586 /* only needed for asynchronous driver... we aren't, so just say it */
587 return MMSYSERR_NOTSUPPORTED;
588 case ACMDM_STREAM_PREPARE:
589 case ACMDM_STREAM_UNPREPARE:
590 /* nothing special to do here... so don't do anything */
591 return MMSYSERR_NOERROR;
594 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);