msadp32.c 27.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * MS ADPCM handling
 *
 *      Copyright (C) 2002		Eric Pouech
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20 21 22
 */

#include <assert.h>
23
#include <stdarg.h>
24
#include <string.h>
25
#include "windef.h"
26 27 28
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
29
#include "winnls.h"
30
#include "mmsystem.h"
31
#include "mmreg.h"
32
#include "msacm.h"
33
#include "msacmdrv.h"
34 35 36 37 38 39 40 41 42
#include "wine/debug.h"

/* see http://www.pcisys.net/~melanson/codecs/adpcm.txt for the details */

WINE_DEFAULT_DEBUG_CHANNEL(adpcm);

/***********************************************************************
 *           ADPCM_drvOpen
 */
43
static LRESULT ADPCM_drvOpen(LPCSTR str)
44 45 46 47 48 49 50
{
    return 1;
}

/***********************************************************************
 *           ADPCM_drvClose
 */
51
static LRESULT ADPCM_drvClose(DWORD_PTR dwDevID)
52 53 54 55
{
    return 1;
}

56
typedef struct tagAcmAdpcmData
57
{
58
    void (*convert)(const ACMDRVSTREAMINSTANCE *adsi,
59 60 61 62 63 64
		    const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
} AcmAdpcmData;

/* table to list all supported formats... those are the basic ones. this
 * also helps given a unique index to each of the supported formats
 */
65
typedef	struct
66 67 68 69 70 71
{
    int		nChannels;
    int		nBits;
    int		rate;
} Format;

72
static const Format PCM_Formats[] =
73 74 75 76 77 78 79
{
    {1,  8,  8000}, {2,  8,  8000}, {1, 16,  8000}, {2, 16,  8000},
    {1,  8, 11025}, {2,  8, 11025}, {1, 16, 11025}, {2, 16, 11025},
    {1,  8, 22050}, {2,  8, 22050}, {1, 16, 22050}, {2, 16, 22050},
    {1,  8, 44100}, {2,  8, 44100}, {1, 16, 44100}, {2, 16, 44100},
};

80
static const Format ADPCM_Formats[] =
81
{
82
    {1,  4,  8000}, {2,	4,  8000},  {1,  4, 11025}, {2,	 4, 11025},
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    {1,  4, 22050}, {2,	4, 22050},  {1,  4, 44100}, {2,	 4, 44100},
};

static int MS_Delta[] =
{
    230, 230, 230, 230, 307, 409, 512, 614,
    768, 614, 512, 409, 307, 230, 230, 230
};


static ADPCMCOEFSET MSADPCM_CoeffSet[] =
{
    {256, 0}, {512, -256}, {0, 0}, {192, 64}, {240, 0}, {460, -208}, {392, -232}
};

/***********************************************************************
 *           ADPCM_GetFormatIndex
 */
101
static	DWORD	ADPCM_GetFormatIndex(const WAVEFORMATEX* wfx)
102
{
103 104
    int             i, hi;
    const Format*   fmts;
105 106

    switch (wfx->wFormatTag)
107 108
    {
    case WAVE_FORMAT_PCM:
109
	hi = ARRAY_SIZE(PCM_Formats);
110 111 112
	fmts = PCM_Formats;
	break;
    case WAVE_FORMAT_ADPCM:
113
	hi = ARRAY_SIZE(ADPCM_Formats);
114 115 116 117 118
	fmts = ADPCM_Formats;
	break;
    default:
	return 0xFFFFFFFF;
    }
119 120

    for (i = 0; i < hi; i++)
121 122 123 124 125 126
    {
	if (wfx->nChannels == fmts[i].nChannels &&
	    wfx->nSamplesPerSec == fmts[i].rate &&
	    wfx->wBitsPerSample == fmts[i].nBits)
	    return i;
    }
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    switch (wfx->wFormatTag)
    {
    case WAVE_FORMAT_PCM:
	if(3 > wfx->nChannels &&
	   wfx->nChannels > 0 &&
	   wfx->nAvgBytesPerSec == 2 * wfx->nSamplesPerSec * wfx->nChannels &&
	   wfx->nBlockAlign == 2 * wfx->nChannels &&
	   wfx->wBitsPerSample == 16)
	   return hi;
	break;
    case WAVE_FORMAT_ADPCM:
	if(3 > wfx->nChannels &&
	   wfx->nChannels > 0 &&
	   wfx->wBitsPerSample == 4 &&
	   wfx->cbSize == 32)
	   return hi;
	break;
    }

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    return 0xFFFFFFFF;
}

static void     init_wfx_adpcm(ADPCMWAVEFORMAT* awfx)
{
    register WAVEFORMATEX*      pwfx = &awfx->wfx;

    /* we assume wFormatTag, nChannels, nSamplesPerSec and wBitsPerSample
     * have been initialized... */

    if (pwfx->wFormatTag != WAVE_FORMAT_ADPCM) {FIXME("wrong FT\n"); return;}
    if (ADPCM_GetFormatIndex(pwfx) == 0xFFFFFFFF) {FIXME("wrong fmt\n"); return;}

    switch (pwfx->nSamplesPerSec)
    {
162 163 164 165
    case  8000: pwfx->nBlockAlign = 256 * pwfx->nChannels;   break;
    case 11025: pwfx->nBlockAlign = 256 * pwfx->nChannels;   break;
    case 22050: pwfx->nBlockAlign = 512 * pwfx->nChannels;   break;
    case 44100: pwfx->nBlockAlign = 1024 * pwfx->nChannels;  break;
166
    default:                               break;
167 168 169
    }
    pwfx->cbSize = 2 * sizeof(WORD) + 7 * sizeof(ADPCMCOEFSET);
    /* 7 is the size of the block head (which contains two samples) */
170

171
    awfx->wSamplesPerBlock = pwfx->nBlockAlign * 2 / pwfx->nChannels - 12;
172 173 174 175 176 177 178 179
    pwfx->nAvgBytesPerSec = (pwfx->nSamplesPerSec * pwfx->nBlockAlign) / awfx->wSamplesPerBlock;
    awfx->wNumCoef = 7;
    memcpy(awfx->aCoef, MSADPCM_CoeffSet, 7 * sizeof(ADPCMCOEFSET));
}

/***********************************************************************
 *           R16
 *
180
 * Read a 16 bit sample (correctly handles endianness)
181 182 183 184 185 186 187 188 189
 */
static inline short  R16(const unsigned char* src)
{
    return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
}

/***********************************************************************
 *           W16
 *
190
 * Write a 16 bit sample (correctly handles endianness)
191 192 193 194 195 196 197 198 199 200 201 202 203
 */
static inline void  W16(unsigned char* dst, short s)
{
    dst[0] = LOBYTE(s);
    dst[1] = HIBYTE(s);
}

static inline void clamp_sample(int* sample)
{
    if (*sample < -32768) *sample = -32768;
    if (*sample >  32767) *sample =  32767;
}

204
static inline void process_nibble(unsigned nibble, int* idelta,
205 206 207 208 209 210 211 212
                                  int* sample1, int* sample2,
                                  const ADPCMCOEFSET* coeff)
{
    int sample;
    int snibble;

    /* nibble is in fact a signed 4 bit integer => propagate sign if needed */
    snibble = (nibble & 0x08) ? (nibble - 16) : nibble;
213
    sample = ((*sample1 * coeff->iCoef1) + (*sample2 * coeff->iCoef2)) / 256 +
214 215 216 217 218 219 220 221 222
        snibble * *idelta;
    clamp_sample(&sample);

    *sample2 = *sample1;
    *sample1 = sample;
    *idelta = ((MS_Delta[nibble] * *idelta) / 256);
    if (*idelta < 16) *idelta = 16;
}

223 224 225 226 227
static inline unsigned char C168(short s)
{
    return HIBYTE(s) ^ (unsigned char)0x80;
}

228
static	void cvtSSms16K(const ACMDRVSTREAMINSTANCE *adsi,
229
                        const unsigned char* src, LPDWORD nsrc,
230 231 232 233 234 235 236 237
                        unsigned char* dst, LPDWORD ndst)
{
    int                 ideltaL, ideltaR;
    int                 sample1L, sample2L;
    int                 sample1R, sample2R;
    ADPCMCOEFSET        coeffL, coeffR;
    int                 nsamp;
    int		        nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
238
    DWORD	        nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
239
                                     *ndst / (nsamp_blk * adsi->pwfxDst->nBlockAlign));
240

241
    *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
242
    *ndst = nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
243 244

    nsamp_blk -= 2; /* see below for samples from block head */
245
    for (; nblock > 0; nblock--)
246 247
    {
        const unsigned char*    in_src = src;
248

249 250 251 252 253 254 255 256 257 258
        /* Catch a problem from Tomb Raider III (bug 21000) where it passes
         * invalid data after a valid sequence of blocks */
        if (*src > 6 || *(src + 1) > 6)
        {
            /* Recalculate the amount of used output buffer. We are not changing
             * nsrc, let's assume the bad data was parsed */
            *ndst -= nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
            WARN("Invalid ADPCM data, stopping conversion\n");
            break;
        }
259 260 261 262 263 264 265 266 267 268
        coeffL = MSADPCM_CoeffSet[*src++];
        coeffR = MSADPCM_CoeffSet[*src++];

        ideltaL  = R16(src);    src += 2;
        ideltaR  = R16(src);    src += 2;
        sample1L = R16(src);    src += 2;
        sample1R = R16(src);    src += 2;
        sample2L = R16(src);    src += 2;
        sample2R = R16(src);    src += 2;

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        if(adsi->pwfxDst->wBitsPerSample == 8){
            /* store samples from block head */
            *dst = C168(sample2L);      ++dst;
            *dst = C168(sample2R);      ++dst;
            *dst = C168(sample1L);      ++dst;
            *dst = C168(sample1R);      ++dst;

            for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
            {
                process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
                *dst = C168(sample1L); ++dst;
                process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
                *dst = C168(sample1R); ++dst;
            }
        }else if(adsi->pwfxDst->wBitsPerSample == 16){
            /* store samples from block head */
            W16(dst, sample2L);      dst += 2;
            W16(dst, sample2R);      dst += 2;
            W16(dst, sample1L);      dst += 2;
            W16(dst, sample1R);      dst += 2;

            for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
            {
                process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
                W16(dst, sample1L); dst += 2;
                process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
                W16(dst, sample1R); dst += 2;
            }
297
        }
298 299 300 301
        src = in_src + adsi->pwfxSrc->nBlockAlign;
    }
}

302
static	void cvtMMms16K(const ACMDRVSTREAMINSTANCE *adsi,
303
                        const unsigned char* src, LPDWORD nsrc,
304 305 306 307 308 309 310
                        unsigned char* dst, LPDWORD ndst)
{
    int                 idelta;
    int                 sample1, sample2;
    ADPCMCOEFSET        coeff;
    int                 nsamp;
    int		        nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
311
    DWORD	        nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
312
                                     *ndst / (nsamp_blk * adsi->pwfxDst->nBlockAlign));
313

314
    *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
315
    *ndst = nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
316 317

    nsamp_blk -= 2; /* see below for samples from block head */
318
    for (; nblock > 0; nblock--)
319 320
    {
        const unsigned char*    in_src = src;
321

322 323 324 325 326 327 328 329
        assert(*src <= 6);
        coeff = MSADPCM_CoeffSet[*src++];

        idelta =  R16(src);     src += 2;
        sample1 = R16(src);     src += 2;
        sample2 = R16(src);     src += 2;

        /* store samples from block head */
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        if(adsi->pwfxDst->wBitsPerSample == 8){
            *dst = C168(sample2);    ++dst;
            *dst = C168(sample1);    ++dst;

            for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
            {
                process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
                *dst = C168(sample1); ++dst;
                process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
                *dst = C168(sample1); ++dst;
            }
        }else if(adsi->pwfxDst->wBitsPerSample == 16){
            W16(dst, sample2);      dst += 2;
            W16(dst, sample1);      dst += 2;

            for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
            {
                process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
                W16(dst, sample1); dst += 2;
                process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
                W16(dst, sample1); dst += 2;
            }
352
        }
353

354 355 356 357 358
        src = in_src + adsi->pwfxSrc->nBlockAlign;
    }
}

#if 0
359 360
static	void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi,
                        const unsigned char* src, LPDWORD nsrc,
361 362 363 364
                        unsigned char* dst, LPDWORD ndst)
{
}

365 366
static	void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi,
                        const unsigned char* src, LPDWORD nsrc,
367 368 369 370 371 372 373 374 375 376 377 378 379
                        unsigned char* dst, LPDWORD ndst)
{
}
#endif

/***********************************************************************
 *           ADPCM_DriverDetails
 *
 */
static	LRESULT ADPCM_DriverDetails(PACMDRIVERDETAILSW add)
{
    add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
    add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
380 381
    add->wMid = MM_MICROSOFT;
    add->wPid = MM_MSFT_ACM_MSADPCM;
382 383 384 385 386
    add->vdwACM = 0x01000000;
    add->vdwDriver = 0x01000000;
    add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
    add->cFormatTags = 2; /* PCM, MS ADPCM */
    add->cFilterTags = 0;
387
    add->hicon = NULL;
388
    MultiByteToWideChar( CP_ACP, 0, "MS-ADPCM", -1,
389
                         add->szShortName, ARRAY_SIZE( add->szShortName ));
390
    MultiByteToWideChar( CP_ACP, 0, "Wine MS ADPCM converter", -1,
391
                         add->szLongName, ARRAY_SIZE( add->szLongName ));
392
    MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
393
                         add->szCopyright, ARRAY_SIZE( add->szCopyright ));
394
    MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
395
                         add->szLicensing, ARRAY_SIZE( add->szLicensing ));
396
    add->szFeatures[0] = 0;
397

398 399 400 401 402 403 404 405 406
    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_FormatTagDetails
 *
 */
static	LRESULT	ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
{
407
    static const WCHAR szPcm[]={'P','C','M',0};
408
    static const WCHAR szMsAdPcm[]={'M','i','c','r','o','s','o','f','t',' ','A','D','P','C','M',0};
409 410

    switch (dwQuery)
411 412 413 414 415
    {
    case ACM_FORMATTAGDETAILSF_INDEX:
	if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
	break;
    case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
416
	if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
417 418 419 420
        {
            aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
	    break;
	}
421
	/* fall through */
422 423
    case ACM_FORMATTAGDETAILSF_FORMATTAG:
	switch (aftd->dwFormatTag)
424 425 426 427 428 429 430
        {
	case WAVE_FORMAT_PCM:	aftd->dwFormatTagIndex = 0; break;
	case WAVE_FORMAT_ADPCM: aftd->dwFormatTagIndex = 1; break;
	default:		return ACMERR_NOTPOSSIBLE;
	}
	break;
    default:
431
	WARN("Unsupported query %08x\n", dwQuery);
432 433
	return MMSYSERR_NOTSUPPORTED;
    }
434

435
    aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
436
    switch (aftd->dwFormatTagIndex)
437 438 439 440
    {
    case 0:
	aftd->dwFormatTag = WAVE_FORMAT_PCM;
	aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
441
	aftd->cStandardFormats = ARRAY_SIZE(PCM_Formats);
442 443 444 445 446
        lstrcpyW(aftd->szFormatTag, szPcm);
        break;
    case 1:
	aftd->dwFormatTag = WAVE_FORMAT_ADPCM;
	aftd->cbFormatSize = sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET);
447
	aftd->cStandardFormats = ARRAY_SIZE(ADPCM_Formats);
448 449 450 451 452 453 454 455 456 457 458 459
        lstrcpyW(aftd->szFormatTag, szMsAdPcm);
	break;
    }
    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_FormatDetails
 *
 */
static	LRESULT	ADPCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
{
460
    switch (dwQuery)
461 462 463 464 465 466
    {
    case ACM_FORMATDETAILSF_FORMAT:
	if (ADPCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
	break;
    case ACM_FORMATDETAILSF_INDEX:
	afd->pwfx->wFormatTag = afd->dwFormatTag;
467
	switch (afd->dwFormatTag)
468 469
        {
	case WAVE_FORMAT_PCM:
470
	    if (afd->dwFormatIndex >= ARRAY_SIZE(PCM_Formats)) return ACMERR_NOTPOSSIBLE;
471 472 473 474
	    afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
	    afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
	    afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
	    /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
475
	     * afd->pwfx->cbSize = 0;
476
	     */
477
	    afd->pwfx->nBlockAlign =
478
		(afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
479
	    afd->pwfx->nAvgBytesPerSec =
480 481 482
		afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
	    break;
	case WAVE_FORMAT_ADPCM:
483
	    if (afd->dwFormatIndex >= ARRAY_SIZE(ADPCM_Formats)) return ACMERR_NOTPOSSIBLE;
484 485 486 487 488 489 490 491
            if (afd->cbwfx < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
                return ACMERR_NOTPOSSIBLE;
	    afd->pwfx->nChannels = ADPCM_Formats[afd->dwFormatIndex].nChannels;
	    afd->pwfx->nSamplesPerSec = ADPCM_Formats[afd->dwFormatIndex].rate;
	    afd->pwfx->wBitsPerSample = ADPCM_Formats[afd->dwFormatIndex].nBits;
            init_wfx_adpcm((ADPCMWAVEFORMAT*)afd->pwfx);
	    break;
	default:
492
            WARN("Unsupported tag %08x\n", afd->dwFormatTag);
493 494 495 496
	    return MMSYSERR_INVALPARAM;
	}
	break;
    default:
497
	WARN("Unsupported query %08x\n", dwQuery);
498
	return MMSYSERR_NOTSUPPORTED;
499 500 501
    }
    afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
    afd->szFormat[0] = 0; /* let MSACM format this for us... */
502

503 504 505 506 507 508 509 510 511 512 513 514
    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_FormatSuggest
 *
 */
static	LRESULT	ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
{
    /* some tests ... */
    if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
	adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
515
	adfs->pwfxSrc->wFormatTag == adfs->pwfxDst->wFormatTag ||
516 517 518 519 520 521 522 523 524
	ADPCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
    /* FIXME: should do those tests against the real size (according to format tag */

    /* If no suggestion for destination, then copy source value */
    if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
	adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
    if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
        adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;

525
    if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
526
    {
527
	if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
528 529 530 531
            adfs->pwfxDst->wBitsPerSample = 4;
        else
            adfs->pwfxDst->wBitsPerSample = 16;
    }
532
    if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
533
    {
534
	if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
535 536 537 538
            adfs->pwfxDst->wFormatTag = WAVE_FORMAT_ADPCM;
        else
            adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
    }
539

540 541 542 543 544 545
    /* recompute other values */
    switch (adfs->pwfxDst->wFormatTag)
    {
    case WAVE_FORMAT_PCM:
        adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
        adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
546 547
        /* check if result is ok */
        if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
548 549
        break;
    case WAVE_FORMAT_ADPCM:
550 551
        if (adfs->cbwfxDst < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
            return ACMERR_NOTPOSSIBLE;
552
        init_wfx_adpcm((ADPCMWAVEFORMAT*)adfs->pwfxDst);
553 554
        /* check if result is ok */
        if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
555 556
        break;
    default:
557
        return ACMERR_NOTPOSSIBLE;
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    }

    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_Reset
 *
 */
static	void	ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi, AcmAdpcmData* aad)
{
}

/***********************************************************************
 *           ADPCM_StreamOpen
 *
 */
static	LRESULT	ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
{
    AcmAdpcmData*	aad;

    assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
580

581 582 583
    if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
	ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
	return ACMERR_NOTPOSSIBLE;
584

585 586
    aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
    if (aad == 0) return MMSYSERR_NOMEM;
587

588
    adsi->dwDriver = (DWORD_PTR)aad;
589

590
    if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
591
	adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
592 593 594 595
    {
	goto theEnd;
    }
    else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
596
             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
597
    {
598
	/* resampling or mono <=> stereo not available */
599
	if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
600
	    adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels)
601 602
	    goto theEnd;

Alexandre Julliard's avatar
Alexandre Julliard committed
603 604 605 606 607 608 609
#if 0
        {
            unsigned int nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
            FIXME("spb=%u\n", nspb);

            /* we check that in a block, after the header, samples are present on
             * 4-sample packet pattern
Austin English's avatar
Austin English committed
610
             * we also check that the block alignment is bigger than the expected size
Alexandre Julliard's avatar
Alexandre Julliard committed
611 612 613 614 615
             */
            if (((nspb - 1) & 3) != 0) goto theEnd;
            if ((((nspb - 1) / 2) + 4) * adsi->pwfxSrc->nChannels < adsi->pwfxSrc->nBlockAlign)
                goto theEnd;
        }
616 617 618
#endif

	/* adpcm decoding... */
619
	if (adsi->pwfxDst->nChannels == 2)
620
	    aad->convert = cvtSSms16K;
621
	else if (adsi->pwfxDst->nChannels == 1)
622 623 624
	    aad->convert = cvtMMms16K;
    }
    else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
625
             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
626 627 628 629 630
    {
	if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
	    adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
            adsi->pwfxSrc->wBitsPerSample != 16)
	    goto theEnd;
631
#if 0
632 633 634 635 636
        nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxDst)->wSamplesPerBlock;
        FIXME("spb=%u\n", nspb);

        /* we check that in a block, after the header, samples are present on
         * 4-sample packet pattern
Austin English's avatar
Austin English committed
637
         * we also check that the block alignment is bigger than the expected size
638 639 640 641 642 643 644
         */
        if (((nspb - 1) & 3) != 0) goto theEnd;
        if ((((nspb - 1) / 2) + 4) * adsi->pwfxDst->nChannels < adsi->pwfxDst->nBlockAlign)
            goto theEnd;
#endif
#if 0
	/* adpcm coding... */
645
	if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 2)
646
	    aad->convert = cvtSS16msK;
647
	if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 1)
648 649 650 651 652 653 654 655 656
	    aad->convert = cvtMM16msK;
#endif
        FIXME("We don't support encoding yet\n");
        goto theEnd;
    }
    else goto theEnd;
    ADPCM_Reset(adsi, aad);

    return MMSYSERR_NOERROR;
657

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
 theEnd:
    HeapFree(GetProcessHeap(), 0, aad);
    adsi->dwDriver = 0L;
    return MMSYSERR_NOTSUPPORTED;
}

/***********************************************************************
 *           ADPCM_StreamClose
 *
 */
static	LRESULT	ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
{
    HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_StreamSize
 *
 */
678
static	LRESULT ADPCM_StreamSize(const ACMDRVSTREAMINSTANCE *adsi, PACMDRVSTREAMSIZE adss)
679
{
680 681 682
    DWORD nblocks;
    WORD wSamplesPerBlock;
    /* wSamplesPerBlock formula comes from MSDN ADPCMWAVEFORMAT page.*/
683
    switch (adss->fdwSize)
684 685 686 687
    {
    case ACM_STREAMSIZEF_DESTINATION:
	/* cbDstLength => cbSrcLength */
	if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
688
	    adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
689
        {
690 691 692 693 694
	    wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
	    nblocks = adss->cbDstLength / adsi->pwfxDst->nBlockAlign;
	    if (nblocks == 0)
		return ACMERR_NOTPOSSIBLE;
	    adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock;
695 696
	}
        else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
697
                 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
698
        {
699 700 701 702 703
	    wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
	    nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * wSamplesPerBlock);
	    if (nblocks == 0)
		return ACMERR_NOTPOSSIBLE;
	    adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign;
704
	}
705
        else
706 707 708 709 710 711 712
        {
	    return MMSYSERR_NOTSUPPORTED;
	}
	break;
    case ACM_STREAMSIZEF_SOURCE:
	/* cbSrcLength => cbDstLength */
	if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
713
	    adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
714
        {
715 716 717 718 719 720 721
	    wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
	    nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock);
	    if (nblocks == 0)
		return ACMERR_NOTPOSSIBLE;
	    if (adss->cbSrcLength % (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock))
		/* Round block count up. */
		nblocks++;
722
	    adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign;
723
	}
724
        else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
725
                 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
726
        {
727 728 729 730 731 732 733
	    wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
	    nblocks = adss->cbSrcLength / adsi->pwfxSrc->nBlockAlign;
	    if (nblocks == 0)
		return ACMERR_NOTPOSSIBLE;
	    if (adss->cbSrcLength % adsi->pwfxSrc->nBlockAlign)
		/* Round block count up. */
		nblocks++;
734
	    adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign * wSamplesPerBlock;
735
	}
736
        else
737 738 739 740 741
        {
	    return MMSYSERR_NOTSUPPORTED;
	}
	break;
    default:
742
	WARN("Unsupported query %08x\n", adss->fdwSize);
743
	return MMSYSERR_NOTSUPPORTED;
744 745 746 747 748 749 750 751 752 753 754 755 756
    }
    return MMSYSERR_NOERROR;
}

/***********************************************************************
 *           ADPCM_StreamConvert
 *
 */
static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
{
    AcmAdpcmData*	aad = (AcmAdpcmData*)adsi->dwDriver;
    DWORD		nsrc = adsh->cbSrcLength;
    DWORD		ndst = adsh->cbDstLength;
757 758

    if (adsh->fdwConvert &
759 760
	~(ACM_STREAMCONVERTF_BLOCKALIGN|
	  ACM_STREAMCONVERTF_END|
761
	  ACM_STREAMCONVERTF_START))
762
    {
763
	FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
764 765 766 767 768 769
    }
    /* ACM_STREAMCONVERTF_BLOCKALIGN
     *	currently all conversions are block aligned, so do nothing for this flag
     * ACM_STREAMCONVERTF_END
     *	no pending data, so do nothing for this flag
     */
770
    if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
771 772 773 774 775 776 777
    {
	ADPCM_Reset(adsi, aad);
    }

    aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
    adsh->cbSrcLengthUsed = nsrc;
    adsh->cbDstLengthUsed = ndst;
778

779 780 781 782 783 784
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			ADPCM_DriverProc			[exported]
 */
785
LRESULT CALLBACK ADPCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
786 787
					 LPARAM dwParam1, LPARAM dwParam2)
{
788 789
    TRACE("(%08lx %p %04x %08lx %08lx);\n",
          dwDevID, hDriv, wMsg, dwParam1, dwParam2);
790 791

    switch (wMsg)
792 793 794 795 796
    {
    case DRV_LOAD:		return 1;
    case DRV_FREE:		return 1;
    case DRV_OPEN:		return ADPCM_drvOpen((LPSTR)dwParam1);
    case DRV_CLOSE:		return ADPCM_drvClose(dwDevID);
797
    case DRV_ENABLE:		return 1;
798 799 800 801 802
    case DRV_DISABLE:		return 1;
    case DRV_QUERYCONFIGURE:	return 1;
    case DRV_CONFIGURE:		MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK); return 1;
    case DRV_INSTALL:		return DRVCNF_RESTART;
    case DRV_REMOVE:		return DRVCNF_RESTART;
803

804 805 806
    case ACMDM_DRIVER_NOTIFY:
	/* no caching from other ACM drivers is done so far */
	return MMSYSERR_NOERROR;
807

808 809
    case ACMDM_DRIVER_DETAILS:
	return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
810

811 812
    case ACMDM_FORMATTAG_DETAILS:
	return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
813

814 815
    case ACMDM_FORMAT_DETAILS:
	return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
816

817 818
    case ACMDM_FORMAT_SUGGEST:
	return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
819

820 821
    case ACMDM_STREAM_OPEN:
	return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
822

823 824
    case ACMDM_STREAM_CLOSE:
	return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
825

826 827
    case ACMDM_STREAM_SIZE:
	return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
828

829 830
    case ACMDM_STREAM_CONVERT:
	return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
831

832 833 834 835 836 837 838 839 840 841 842 843 844
    case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
    case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
	/* this converter is not a hardware driver */
    case ACMDM_FILTERTAG_DETAILS:
    case ACMDM_FILTER_DETAILS:
	/* this converter is not a filter */
    case ACMDM_STREAM_RESET:
	/* only needed for asynchronous driver... we aren't, so just say it */
	return MMSYSERR_NOTSUPPORTED;
    case ACMDM_STREAM_PREPARE:
    case ACMDM_STREAM_UNPREPARE:
	/* nothing special to do here... so don't do anything */
	return MMSYSERR_NOERROR;
845

846 847 848 849
    default:
	return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
    }
}