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},
80 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000},
83 static const Format MPEG3_Formats[] =
85 {1, 0, 8000}, {2, 0, 8000}, {1, 0, 11025}, {2, 0, 11025},
86 {1, 0, 22050}, {2, 0, 22050}, {1, 0, 44100}, {2, 0, 44100},
87 {1, 0, 48000}, {2, 0, 48000},
90 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
91 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
93 /***********************************************************************
94 * MPEG3_GetFormatIndex
96 static DWORD MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx)
101 switch (wfx->wFormatTag)
103 case WAVE_FORMAT_PCM:
104 hi = NUM_PCM_FORMATS;
107 case WAVE_FORMAT_MPEGLAYER3:
108 hi = NUM_MPEG3_FORMATS;
109 fmts = MPEG3_Formats;
115 for (i = 0; i < hi; i++)
117 if (wfx->nChannels == fmts[i].nChannels &&
118 wfx->nSamplesPerSec == fmts[i].rate &&
119 wfx->wBitsPerSample == fmts[i].nBits)
126 static DWORD get_num_buffered_bytes(struct mpstr *mp)
129 struct buf * p = mp->tail;
131 numBuff += p->size - p->pos;
137 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi,
138 const unsigned char* src, LPDWORD nsrc,
139 unsigned char* dst, LPDWORD ndst)
141 AcmMpeg3Data* amd = (AcmMpeg3Data*)adsi->dwDriver;
144 DWORD buffered_before;
145 DWORD buffered_during;
146 DWORD buffered_after;
148 /* Skip leading ID v3 header */
149 if (amd->mp.fsizeold == -1 && !strncmp("ID3", (char*)src, 3))
152 const char *header = (char *)src;
154 TRACE("Found ID3 v2.%d.%d\n", header[3], header[4]);
155 length += (header[6] & 0x7F) << 21;
156 length += (header[7] & 0x7F) << 14;
157 length += (header[8] & 0x7F) << 7;
158 length += (header[9] & 0x7F);
159 TRACE("Length: %u\n", length);
165 buffered_before = get_num_buffered_bytes(&amd->mp);
166 ret = decodeMP3(&amd->mp, src, *nsrc, dst, *ndst, &size);
167 buffered_during = get_num_buffered_bytes(&amd->mp);
175 if (*ndst - dpos < 4608) break;
176 ret = decodeMP3(&amd->mp, NULL, 0,
177 dst + dpos, *ndst - dpos, &size);
178 } while (ret == MP3_OK);
181 buffered_after = get_num_buffered_bytes(&amd->mp);
182 TRACE("before %d put %d during %d after %d\n", buffered_before, *nsrc, buffered_during, buffered_after);
184 *nsrc -= buffered_after;
185 ClearMP3Buffer(&amd->mp);
188 /***********************************************************************
189 * MPEG3_DriverDetails
192 static LRESULT MPEG3_DriverDetails(PACMDRIVERDETAILSW add)
194 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
195 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
198 add->vdwACM = 0x01000000;
199 add->vdwDriver = 0x01000000;
200 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
201 add->cFormatTags = 2; /* PCM, MPEG3 */
202 add->cFilterTags = 0;
204 MultiByteToWideChar( CP_ACP, 0, "WINE-MPEG3", -1,
205 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
206 MultiByteToWideChar( CP_ACP, 0, "Wine MPEG3 decoder", -1,
207 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
208 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team (based on mpglib by Michael Hipp)...", -1,
209 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
210 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
211 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
212 add->szFeatures[0] = 0;
214 return MMSYSERR_NOERROR;
217 /***********************************************************************
218 * MPEG3_FormatTagDetails
221 static LRESULT MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
223 static const WCHAR szPcm[]={'P','C','M',0};
224 static const WCHAR szMpeg3[]={'M','P','e','g','3',0};
228 case ACM_FORMATTAGDETAILSF_INDEX:
229 if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
231 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
232 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
234 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_MPEGLAYER3 is bigger than PCM */
238 case ACM_FORMATTAGDETAILSF_FORMATTAG:
239 switch (aftd->dwFormatTag)
241 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
242 case WAVE_FORMAT_MPEGLAYER3: aftd->dwFormatTagIndex = 1; break;
243 default: return ACMERR_NOTPOSSIBLE;
247 WARN("Unsupported query %08x\n", dwQuery);
248 return MMSYSERR_NOTSUPPORTED;
251 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
252 switch (aftd->dwFormatTagIndex)
255 aftd->dwFormatTag = WAVE_FORMAT_PCM;
256 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
257 aftd->cStandardFormats = NUM_PCM_FORMATS;
258 lstrcpyW(aftd->szFormatTag, szPcm);
261 aftd->dwFormatTag = WAVE_FORMAT_MPEGLAYER3;
262 aftd->cbFormatSize = sizeof(MPEGLAYER3WAVEFORMAT);
263 aftd->cStandardFormats = NUM_MPEG3_FORMATS;
264 lstrcpyW(aftd->szFormatTag, szMpeg3);
267 return MMSYSERR_NOERROR;
270 static void fill_in_wfx(unsigned cbwfx, WAVEFORMATEX* wfx, unsigned bit_rate)
272 MPEGLAYER3WAVEFORMAT* mp3wfx = (MPEGLAYER3WAVEFORMAT*)wfx;
274 wfx->nAvgBytesPerSec = bit_rate / 8;
275 if (cbwfx >= sizeof(WAVEFORMATEX))
276 wfx->cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);
277 if (cbwfx >= sizeof(MPEGLAYER3WAVEFORMAT))
279 mp3wfx->wID = MPEGLAYER3_ID_MPEG;
280 mp3wfx->fdwFlags = MPEGLAYER3_FLAG_PADDING_OFF;
281 mp3wfx->nBlockSize = (bit_rate * 144) / wfx->nSamplesPerSec;
282 mp3wfx->nFramesPerBlock = 1;
283 mp3wfx->nCodecDelay = 0x0571;
287 /***********************************************************************
288 * MPEG3_FormatDetails
291 static LRESULT MPEG3_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
295 case ACM_FORMATDETAILSF_FORMAT:
296 if (MPEG3_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
298 case ACM_FORMATDETAILSF_INDEX:
299 afd->pwfx->wFormatTag = afd->dwFormatTag;
300 switch (afd->dwFormatTag)
302 case WAVE_FORMAT_PCM:
303 if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
304 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
305 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
306 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
307 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
308 * afd->pwfx->cbSize = 0;
310 afd->pwfx->nBlockAlign =
311 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
312 afd->pwfx->nAvgBytesPerSec =
313 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
315 case WAVE_FORMAT_MPEGLAYER3:
316 if (afd->dwFormatIndex >= NUM_MPEG3_FORMATS) return ACMERR_NOTPOSSIBLE;
317 afd->pwfx->nChannels = MPEG3_Formats[afd->dwFormatIndex].nChannels;
318 afd->pwfx->nSamplesPerSec = MPEG3_Formats[afd->dwFormatIndex].rate;
319 afd->pwfx->wBitsPerSample = MPEG3_Formats[afd->dwFormatIndex].nBits;
320 afd->pwfx->nBlockAlign = 1;
321 fill_in_wfx(afd->cbwfx, afd->pwfx, 192000);
324 WARN("Unsupported tag %08x\n", afd->dwFormatTag);
325 return MMSYSERR_INVALPARAM;
329 WARN("Unsupported query %08x\n", dwQuery);
330 return MMSYSERR_NOTSUPPORTED;
332 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
333 afd->szFormat[0] = 0; /* let MSACM format this for us... */
335 return MMSYSERR_NOERROR;
338 /***********************************************************************
339 * MPEG3_FormatSuggest
342 static LRESULT MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
345 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
346 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
347 MPEG3_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
348 /* FIXME: should do those tests against the real size (according to format tag */
350 /* If no suggestion for destination, then copy source value */
351 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
352 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
353 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
354 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
356 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
358 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
359 adfs->pwfxDst->wBitsPerSample = 4;
361 adfs->pwfxDst->wBitsPerSample = 16;
363 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
365 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
366 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
368 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
371 /* check if result is ok */
372 if (MPEG3_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
374 /* recompute other values */
375 switch (adfs->pwfxDst->wFormatTag)
377 case WAVE_FORMAT_PCM:
378 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
379 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
381 case WAVE_FORMAT_MPEGLAYER3:
382 adfs->pwfxDst->nBlockAlign = 1;
383 fill_in_wfx(adfs->cbwfxDst, adfs->pwfxDst, 192000);
390 return MMSYSERR_NOERROR;
393 /***********************************************************************
397 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi, AcmMpeg3Data* aad)
399 ClearMP3Buffer(&aad->mp);
403 /***********************************************************************
407 static LRESULT MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
411 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
413 if (MPEG3_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
414 MPEG3_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
415 return ACMERR_NOTPOSSIBLE;
417 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data));
418 if (aad == 0) return MMSYSERR_NOMEM;
420 adsi->dwDriver = (DWORD)aad;
422 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
423 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
427 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
428 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
430 /* resampling or mono <=> stereo not available
431 * MPEG3 algo only define 16 bit per sample output
433 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
434 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
435 adsi->pwfxDst->wBitsPerSample != 16)
437 aad->convert = mp3_horse;
441 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
442 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
445 MPEG3_Reset(adsi, aad);
447 return MMSYSERR_NOERROR;
450 HeapFree(GetProcessHeap(), 0, aad);
452 return MMSYSERR_NOTSUPPORTED;
455 /***********************************************************************
459 static LRESULT MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi)
461 ClearMP3Buffer(&((AcmMpeg3Data*)adsi->dwDriver)->mp);
462 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
463 return MMSYSERR_NOERROR;
466 /***********************************************************************
470 static LRESULT MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
472 switch (adss->fdwSize)
474 case ACM_STREAMSIZEF_DESTINATION:
475 /* cbDstLength => cbSrcLength */
476 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
477 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
479 /* don't take block overhead into account, doesn't matter too much */
480 adss->cbSrcLength = adss->cbDstLength * 12;
482 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
483 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
485 FIXME("misses the block header overhead\n");
486 adss->cbSrcLength = 256 + adss->cbDstLength / 12;
490 return MMSYSERR_NOTSUPPORTED;
493 case ACM_STREAMSIZEF_SOURCE:
494 /* cbSrcLength => cbDstLength */
495 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
496 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
498 FIXME("misses the block header overhead\n");
499 adss->cbDstLength = 256 + adss->cbSrcLength / 12;
501 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_MPEGLAYER3 &&
502 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
504 /* don't take block overhead into account, doesn't matter too much */
505 adss->cbDstLength = adss->cbSrcLength * 12;
509 return MMSYSERR_NOTSUPPORTED;
513 WARN("Unsupported query %08x\n", adss->fdwSize);
514 return MMSYSERR_NOTSUPPORTED;
516 return MMSYSERR_NOERROR;
519 /***********************************************************************
520 * MPEG3_StreamConvert
523 static LRESULT MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
525 AcmMpeg3Data* aad = (AcmMpeg3Data*)adsi->dwDriver;
526 DWORD nsrc = adsh->cbSrcLength;
527 DWORD ndst = adsh->cbDstLength;
529 if (adsh->fdwConvert &
530 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
531 ACM_STREAMCONVERTF_END|
532 ACM_STREAMCONVERTF_START))
534 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
536 /* ACM_STREAMCONVERTF_BLOCKALIGN
537 * currently all conversions are block aligned, so do nothing for this flag
538 * ACM_STREAMCONVERTF_END
539 * no pending data, so do nothing for this flag
541 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
543 MPEG3_Reset(adsi, aad);
546 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
547 adsh->cbSrcLengthUsed = nsrc;
548 adsh->cbDstLengthUsed = ndst;
550 return MMSYSERR_NOERROR;
553 /**************************************************************************
554 * MPEG3_DriverProc [exported]
556 LRESULT CALLBACK MPEG3_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
557 LPARAM dwParam1, LPARAM dwParam2)
559 TRACE("(%08lx %p %04x %08lx %08lx);\n",
560 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
564 case DRV_LOAD: return 1;
565 case DRV_FREE: return 1;
566 case DRV_OPEN: return MPEG3_drvOpen((LPSTR)dwParam1);
567 case DRV_CLOSE: return MPEG3_drvClose(dwDevID);
568 case DRV_ENABLE: return 1;
569 case DRV_DISABLE: return 1;
570 case DRV_QUERYCONFIGURE: return 1;
571 case DRV_CONFIGURE: MessageBoxA(0, "MPEG3 filter !", "Wine Driver", MB_OK); return 1;
572 case DRV_INSTALL: return DRVCNF_RESTART;
573 case DRV_REMOVE: return DRVCNF_RESTART;
575 case ACMDM_DRIVER_NOTIFY:
576 /* no caching from other ACM drivers is done so far */
577 return MMSYSERR_NOERROR;
579 case ACMDM_DRIVER_DETAILS:
580 return MPEG3_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
582 case ACMDM_FORMATTAG_DETAILS:
583 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
585 case ACMDM_FORMAT_DETAILS:
586 return MPEG3_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
588 case ACMDM_FORMAT_SUGGEST:
589 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
591 case ACMDM_STREAM_OPEN:
592 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
594 case ACMDM_STREAM_CLOSE:
595 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
597 case ACMDM_STREAM_SIZE:
598 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
600 case ACMDM_STREAM_CONVERT:
601 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
603 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
604 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
605 /* this converter is not a hardware driver */
606 case ACMDM_FILTERTAG_DETAILS:
607 case ACMDM_FILTER_DETAILS:
608 /* this converter is not a filter */
609 case ACMDM_STREAM_RESET:
610 /* only needed for asynchronous driver... we aren't, so just say it */
611 return MMSYSERR_NOTSUPPORTED;
612 case ACMDM_STREAM_PREPARE:
613 case ACMDM_STREAM_UNPREPARE:
614 /* nothing special to do here... so don't do anything */
615 return MMSYSERR_NOERROR;
618 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);