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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "../msacmdrv.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3
);
38 /***********************************************************************
41 static DWORD
MPEG3_drvOpen(LPCSTR str
)
46 /***********************************************************************
49 static DWORD
MPEG3_drvClose(DWORD dwDevID
)
54 typedef struct tagAcmMpeg3Data
56 void (*convert
)(PACMDRVSTREAMINSTANCE adsi
,
57 const unsigned char*, LPDWORD
, unsigned char*, LPDWORD
);
61 /* table to list all supported formats... those are the basic ones. this
62 * also helps given a unique index to each of the supported formats
71 static Format PCM_Formats
[] =
73 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000},
74 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025},
75 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050},
76 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100},
79 static Format MPEG3_Formats
[] =
81 {1, 0, 8000}, {2, 0, 8000}, {1, 0, 11025}, {2, 0, 11025},
82 {1, 0, 22050}, {2, 0, 22050}, {1, 0, 44100}, {2, 0, 44100},
85 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
86 #define NUM_MPEG3_FORMATS (sizeof(MPEG3_Formats) / sizeof(MPEG3_Formats[0]))
88 /***********************************************************************
89 * MPEG3_GetFormatIndex
91 static DWORD
MPEG3_GetFormatIndex(LPWAVEFORMATEX wfx
)
96 switch (wfx
->wFormatTag
)
102 case WAVE_FORMAT_MPEGLAYER3
:
103 hi
= NUM_MPEG3_FORMATS
;
104 fmts
= MPEG3_Formats
;
110 for (i
= 0; i
< hi
; i
++)
112 if (wfx
->nChannels
== fmts
[i
].nChannels
&&
113 wfx
->nSamplesPerSec
== fmts
[i
].rate
&&
114 wfx
->wBitsPerSample
== fmts
[i
].nBits
)
121 /***********************************************************************
124 * Read a 16 bit sample (correctly handles endianess)
126 static inline short R16(const unsigned char* src
)
128 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
131 /***********************************************************************
134 * Write a 16 bit sample (correctly handles endianess)
136 static inline void W16(unsigned char* dst
, short s
)
142 static void mp3_horse(PACMDRVSTREAMINSTANCE adsi
,
143 const unsigned char* src
, LPDWORD nsrc
,
144 unsigned char* dst
, LPDWORD ndst
)
146 AcmMpeg3Data
* amd
= (AcmMpeg3Data
*)adsi
->dwDriver
;
150 ret
= decodeMP3(&amd
->mp
, (unsigned char*)src
, *nsrc
, dst
, *ndst
, &size
);
158 if (*ndst
- dpos
< 4608) break;
159 ret
= decodeMP3(&amd
->mp
, NULL
, 0,
160 dst
+ dpos
, *ndst
- dpos
, &size
);
161 } while (ret
== MP3_OK
);
165 /***********************************************************************
166 * MPEG3_DriverDetails
169 static LRESULT
MPEG3_DriverDetails(PACMDRIVERDETAILSW add
)
171 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
172 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
175 add
->vdwACM
= 0x01000000;
176 add
->vdwDriver
= 0x01000000;
177 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
178 add
->cFormatTags
= 2; /* PCM, MPEG3 */
179 add
->cFilterTags
= 0;
181 MultiByteToWideChar( CP_ACP
, 0, "WINE-MPEG3", -1,
182 add
->szShortName
, sizeof(add
->szShortName
)/sizeof(WCHAR
) );
183 MultiByteToWideChar( CP_ACP
, 0, "Wine MPEG3 decoder", -1,
184 add
->szLongName
, sizeof(add
->szLongName
)/sizeof(WCHAR
) );
185 MultiByteToWideChar( CP_ACP
, 0, "Brought to you by the Wine team (based on mpglib by Michael Hipp)...", -1,
186 add
->szCopyright
, sizeof(add
->szCopyright
)/sizeof(WCHAR
) );
187 MultiByteToWideChar( CP_ACP
, 0, "Refer to LICENSE file", -1,
188 add
->szLicensing
, sizeof(add
->szLicensing
)/sizeof(WCHAR
) );
189 add
->szFeatures
[0] = 0;
191 return MMSYSERR_NOERROR
;
194 /***********************************************************************
195 * MPEG3_FormatTagDetails
198 static LRESULT
MPEG3_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
200 static WCHAR szPcm
[]={'P','C','M',0};
201 static WCHAR szMpeg3
[]={'M','P','e','g','3',0};
205 case ACM_FORMATTAGDETAILSF_INDEX
:
206 if (aftd
->dwFormatTagIndex
>= 2) return ACMERR_NOTPOSSIBLE
;
208 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
209 if (aftd
->dwFormatTag
== WAVE_FORMAT_UNKNOWN
)
211 aftd
->dwFormatTagIndex
= 1; /* WAVE_FORMAT_MPEGLAYER3 is bigger than PCM */
215 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
216 switch (aftd
->dwFormatTag
)
218 case WAVE_FORMAT_PCM
: aftd
->dwFormatTagIndex
= 0; break;
219 case WAVE_FORMAT_MPEGLAYER3
: aftd
->dwFormatTagIndex
= 1; break;
220 default: return ACMERR_NOTPOSSIBLE
;
224 WARN("Unsupported query %08lx\n", dwQuery
);
225 return MMSYSERR_NOTSUPPORTED
;
228 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
229 switch (aftd
->dwFormatTagIndex
)
232 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
233 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
234 aftd
->cStandardFormats
= NUM_PCM_FORMATS
;
235 lstrcpyW(aftd
->szFormatTag
, szPcm
);
238 aftd
->dwFormatTag
= WAVE_FORMAT_MPEGLAYER3
;
239 aftd
->cbFormatSize
= sizeof(MPEGLAYER3WAVEFORMAT
);
240 aftd
->cStandardFormats
= NUM_MPEG3_FORMATS
;
241 lstrcpyW(aftd
->szFormatTag
, szMpeg3
);
244 return MMSYSERR_NOERROR
;
247 static void fill_in_wfx(unsigned cbwfx
, WAVEFORMATEX
* wfx
, unsigned bit_rate
)
249 MPEGLAYER3WAVEFORMAT
* mp3wfx
= (MPEGLAYER3WAVEFORMAT
*)wfx
;
251 wfx
->nAvgBytesPerSec
= bit_rate
/ 8;
252 if (cbwfx
>= sizeof(WAVEFORMATEX
))
253 wfx
->cbSize
= sizeof(MPEGLAYER3WAVEFORMAT
) - sizeof(WAVEFORMATEX
);
254 if (cbwfx
>= sizeof(MPEGLAYER3WAVEFORMAT
))
256 mp3wfx
->wID
= MPEGLAYER3_ID_MPEG
;
257 mp3wfx
->fdwFlags
= MPEGLAYER3_FLAG_PADDING_OFF
;
258 mp3wfx
->nBlockSize
= (bit_rate
* 144) / wfx
->nSamplesPerSec
;
259 mp3wfx
->nFramesPerBlock
= 1;
260 mp3wfx
->nCodecDelay
= 0x0571;
264 /***********************************************************************
265 * MPEG3_FormatDetails
268 static LRESULT
MPEG3_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
272 case ACM_FORMATDETAILSF_FORMAT
:
273 if (MPEG3_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
275 case ACM_FORMATDETAILSF_INDEX
:
276 afd
->pwfx
->wFormatTag
= afd
->dwFormatTag
;
277 switch (afd
->dwFormatTag
)
279 case WAVE_FORMAT_PCM
:
280 if (afd
->dwFormatIndex
>= NUM_PCM_FORMATS
) return ACMERR_NOTPOSSIBLE
;
281 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
282 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
283 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
284 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
285 * afd->pwfx->cbSize = 0;
287 afd
->pwfx
->nBlockAlign
=
288 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
289 afd
->pwfx
->nAvgBytesPerSec
=
290 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
292 case WAVE_FORMAT_MPEGLAYER3
:
293 if (afd
->dwFormatIndex
>= NUM_MPEG3_FORMATS
) return ACMERR_NOTPOSSIBLE
;
294 afd
->pwfx
->nChannels
= MPEG3_Formats
[afd
->dwFormatIndex
].nChannels
;
295 afd
->pwfx
->nSamplesPerSec
= MPEG3_Formats
[afd
->dwFormatIndex
].rate
;
296 afd
->pwfx
->wBitsPerSample
= MPEG3_Formats
[afd
->dwFormatIndex
].nBits
;
297 afd
->pwfx
->nBlockAlign
= 1;
298 fill_in_wfx(afd
->cbwfx
, afd
->pwfx
, 192000);
301 WARN("Unsupported tag %08lx\n", afd
->dwFormatTag
);
302 return MMSYSERR_INVALPARAM
;
306 WARN("Unsupported query %08lx\n", dwQuery
);
307 return MMSYSERR_NOTSUPPORTED
;
309 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
310 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
312 return MMSYSERR_NOERROR
;
315 /***********************************************************************
316 * MPEG3_FormatSuggest
319 static LRESULT
MPEG3_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
322 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
323 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
324 MPEG3_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
325 /* FIXME: should do those tests against the real size (according to format tag */
327 /* If no suggestion for destination, then copy source value */
328 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
))
329 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
330 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
))
331 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
333 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
))
335 if (adfs
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
)
336 adfs
->pwfxDst
->wBitsPerSample
= 4;
338 adfs
->pwfxDst
->wBitsPerSample
= 16;
340 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
))
342 if (adfs
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
)
343 adfs
->pwfxDst
->wFormatTag
= WAVE_FORMAT_MPEGLAYER3
;
345 adfs
->pwfxDst
->wFormatTag
= WAVE_FORMAT_PCM
;
348 /* check if result is ok */
349 if (MPEG3_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
351 /* recompute other values */
352 switch (adfs
->pwfxDst
->wFormatTag
)
354 case WAVE_FORMAT_PCM
:
355 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
356 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
358 case WAVE_FORMAT_MPEGLAYER3
:
359 adfs
->pwfxDst
->nBlockAlign
= 1;
360 fill_in_wfx(adfs
->cbwfxDst
, adfs
->pwfxDst
, 192000);
367 return MMSYSERR_NOERROR
;
370 /***********************************************************************
374 static void MPEG3_Reset(PACMDRVSTREAMINSTANCE adsi
, AcmMpeg3Data
* aad
)
378 /***********************************************************************
382 static LRESULT
MPEG3_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
386 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
388 if (MPEG3_GetFormatIndex(adsi
->pwfxSrc
) == 0xFFFFFFFF ||
389 MPEG3_GetFormatIndex(adsi
->pwfxDst
) == 0xFFFFFFFF)
390 return ACMERR_NOTPOSSIBLE
;
392 aad
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmMpeg3Data
));
393 if (aad
== 0) return MMSYSERR_NOMEM
;
395 adsi
->dwDriver
= (DWORD
)aad
;
397 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
398 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
402 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_MPEGLAYER3
&&
403 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
405 /* resampling or mono <=> stereo not available
406 * MPEG3 algo only define 16 bit per sample output
408 if (adsi
->pwfxSrc
->nSamplesPerSec
!= adsi
->pwfxDst
->nSamplesPerSec
||
409 adsi
->pwfxSrc
->nChannels
!= adsi
->pwfxDst
->nChannels
||
410 adsi
->pwfxDst
->wBitsPerSample
!= 16)
412 aad
->convert
= mp3_horse
;
416 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
417 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_MPEGLAYER3)
420 MPEG3_Reset(adsi
, aad
);
422 return MMSYSERR_NOERROR
;
425 HeapFree(GetProcessHeap(), 0, aad
);
427 return MMSYSERR_NOTSUPPORTED
;
430 /***********************************************************************
434 static LRESULT
MPEG3_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
436 ExitMP3(&((AcmMpeg3Data
*)adsi
->dwDriver
)->mp
);
437 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
438 return MMSYSERR_NOERROR
;
441 /***********************************************************************
445 static inline DWORD
MPEG3_round(DWORD a
, DWORD b
, DWORD c
)
448 /* to be sure, always return an entire number of c... */
449 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
452 /***********************************************************************
456 static LRESULT
MPEG3_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
458 switch (adss
->fdwSize
)
460 case ACM_STREAMSIZEF_DESTINATION
:
461 /* cbDstLength => cbSrcLength */
462 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
463 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_MPEGLAYER3
)
465 /* don't take block overhead into account, doesn't matter too much */
466 adss
->cbSrcLength
= adss
->cbDstLength
* 4;
468 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_MPEGLAYER3
&&
469 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
471 FIXME("misses the block header overhead\n");
472 adss
->cbSrcLength
= 256 + adss
->cbDstLength
/ 4;
476 return MMSYSERR_NOTSUPPORTED
;
479 case ACM_STREAMSIZEF_SOURCE
:
480 /* cbSrcLength => cbDstLength */
481 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
482 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_MPEGLAYER3
)
484 FIXME("misses the block header overhead\n");
485 adss
->cbDstLength
= 256 + adss
->cbSrcLength
/ 4;
487 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_MPEGLAYER3
&&
488 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
490 /* don't take block overhead into account, doesn't matter too much */
491 adss
->cbDstLength
= adss
->cbSrcLength
* 4;
495 return MMSYSERR_NOTSUPPORTED
;
499 WARN("Unsupported query %08lx\n", adss
->fdwSize
);
500 return MMSYSERR_NOTSUPPORTED
;
502 return MMSYSERR_NOERROR
;
505 /***********************************************************************
506 * MPEG3_StreamConvert
509 static LRESULT
MPEG3_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
511 AcmMpeg3Data
* aad
= (AcmMpeg3Data
*)adsi
->dwDriver
;
512 DWORD nsrc
= adsh
->cbSrcLength
;
513 DWORD ndst
= adsh
->cbDstLength
;
515 if (adsh
->fdwConvert
&
516 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
517 ACM_STREAMCONVERTF_END
|
518 ACM_STREAMCONVERTF_START
))
520 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh
->fdwConvert
);
522 /* ACM_STREAMCONVERTF_BLOCKALIGN
523 * currently all conversions are block aligned, so do nothing for this flag
524 * ACM_STREAMCONVERTF_END
525 * no pending data, so do nothing for this flag
527 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
))
529 MPEG3_Reset(adsi
, aad
);
532 aad
->convert(adsi
, adsh
->pbSrc
, &nsrc
, adsh
->pbDst
, &ndst
);
533 adsh
->cbSrcLengthUsed
= nsrc
;
534 adsh
->cbDstLengthUsed
= ndst
;
536 return MMSYSERR_NOERROR
;
539 /**************************************************************************
540 * MPEG3_DriverProc [exported]
542 LRESULT CALLBACK
MPEG3_DriverProc(DWORD dwDevID
, HDRVR hDriv
, UINT wMsg
,
543 LPARAM dwParam1
, LPARAM dwParam2
)
545 TRACE("(%08lx %08lx %04x %08lx %08lx);\n",
546 dwDevID
, (DWORD
)hDriv
, wMsg
, dwParam1
, dwParam2
);
550 case DRV_LOAD
: return 1;
551 case DRV_FREE
: return 1;
552 case DRV_OPEN
: return MPEG3_drvOpen((LPSTR
)dwParam1
);
553 case DRV_CLOSE
: return MPEG3_drvClose(dwDevID
);
554 case DRV_ENABLE
: return 1;
555 case DRV_DISABLE
: return 1;
556 case DRV_QUERYCONFIGURE
: return 1;
557 case DRV_CONFIGURE
: MessageBoxA(0, "MPEG3 filter !", "Wine Driver", MB_OK
); return 1;
558 case DRV_INSTALL
: return DRVCNF_RESTART
;
559 case DRV_REMOVE
: return DRVCNF_RESTART
;
561 case ACMDM_DRIVER_NOTIFY
:
562 /* no caching from other ACM drivers is done so far */
563 return MMSYSERR_NOERROR
;
565 case ACMDM_DRIVER_DETAILS
:
566 return MPEG3_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
568 case ACMDM_FORMATTAG_DETAILS
:
569 return MPEG3_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
571 case ACMDM_FORMAT_DETAILS
:
572 return MPEG3_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
574 case ACMDM_FORMAT_SUGGEST
:
575 return MPEG3_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
577 case ACMDM_STREAM_OPEN
:
578 return MPEG3_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
580 case ACMDM_STREAM_CLOSE
:
581 return MPEG3_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
583 case ACMDM_STREAM_SIZE
:
584 return MPEG3_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
586 case ACMDM_STREAM_CONVERT
:
587 return MPEG3_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
589 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
590 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
591 /* this converter is not a hardware driver */
592 case ACMDM_FILTERTAG_DETAILS
:
593 case ACMDM_FILTER_DETAILS
:
594 /* this converter is not a filter */
595 case ACMDM_STREAM_RESET
:
596 /* only needed for asynchronous driver... we aren't, so just say it */
597 return MMSYSERR_NOTSUPPORTED
;
598 case ACMDM_STREAM_PREPARE
:
599 case ACMDM_STREAM_UNPREPARE
:
600 /* nothing special to do here... so don't do anything */
601 return MMSYSERR_NOERROR
;
604 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);