winemenubuilder: Make initial directories for fd.o MIME type handling.
[wine] / dlls / mciavi32 / mmoutput.c
1 /*
2  * Digital video MCI Wine Driver
3  *
4  * Copyright 1999, 2000 Eric POUECH
5  * Copyright 2003 Dmitry Timoshkov
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "private_mciavi.h"
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(mciavi);
26
27 static BOOL MCIAVI_GetInfoAudio(WINE_MCIAVI* wma, const MMCKINFO* mmckList, MMCKINFO *mmckStream)
28 {
29     MMCKINFO    mmckInfo;
30
31     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(wma->ash_audio.fccType)),
32                                                 HIBYTE(LOWORD(wma->ash_audio.fccType)),
33                                                 LOBYTE(HIWORD(wma->ash_audio.fccType)),
34                                                 HIBYTE(HIWORD(wma->ash_audio.fccType)));
35     if (wma->ash_audio.fccHandler) /* not all streams specify a handler */
36         TRACE("ash.fccHandler='%c%c%c%c'\n",    LOBYTE(LOWORD(wma->ash_audio.fccHandler)),
37                                                 HIBYTE(LOWORD(wma->ash_audio.fccHandler)),
38                                                 LOBYTE(HIWORD(wma->ash_audio.fccHandler)),
39                                                 HIBYTE(HIWORD(wma->ash_audio.fccHandler)));
40     else
41         TRACE("ash.fccHandler=0, no handler specified\n");
42     TRACE("ash.dwFlags=%d\n",                   wma->ash_audio.dwFlags);
43     TRACE("ash.wPriority=%d\n",                 wma->ash_audio.wPriority);
44     TRACE("ash.wLanguage=%d\n",                 wma->ash_audio.wLanguage);
45     TRACE("ash.dwInitialFrames=%d\n",           wma->ash_audio.dwInitialFrames);
46     TRACE("ash.dwScale=%d\n",                   wma->ash_audio.dwScale);
47     TRACE("ash.dwRate=%d\n",                    wma->ash_audio.dwRate);
48     TRACE("ash.dwStart=%d\n",                   wma->ash_audio.dwStart);
49     TRACE("ash.dwLength=%d\n",          wma->ash_audio.dwLength);
50     TRACE("ash.dwSuggestedBufferSize=%d\n",     wma->ash_audio.dwSuggestedBufferSize);
51     TRACE("ash.dwQuality=%d\n",                 wma->ash_audio.dwQuality);
52     TRACE("ash.dwSampleSize=%d\n",              wma->ash_audio.dwSampleSize);
53     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        wma->ash_audio.rcFrame.top, wma->ash_audio.rcFrame.left,
54           wma->ash_audio.rcFrame.bottom, wma->ash_audio.rcFrame.right);
55
56     /* rewind to the start of the stream */
57     mmioAscend(wma->hFile, mmckStream, 0);
58
59     mmckInfo.ckid = ckidSTREAMFORMAT;
60     if (mmioDescend(wma->hFile, &mmckInfo, mmckList, MMIO_FINDCHUNK) != 0) {
61        WARN("Can't find 'strf' chunk\n");
62         return FALSE;
63     }
64     if (mmckInfo.cksize < sizeof(WAVEFORMAT)) {
65         WARN("Size of strf chunk (%d) < audio format struct\n", mmckInfo.cksize);
66         return FALSE;
67     }
68     wma->lpWaveFormat = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
69     if (!wma->lpWaveFormat) {
70         WARN("Can't alloc WaveFormat\n");
71         return FALSE;
72     }
73
74     mmioRead(wma->hFile, (LPSTR)wma->lpWaveFormat, mmckInfo.cksize);
75
76     TRACE("waveFormat.wFormatTag=%d\n",         wma->lpWaveFormat->wFormatTag);
77     TRACE("waveFormat.nChannels=%d\n",          wma->lpWaveFormat->nChannels);
78     TRACE("waveFormat.nSamplesPerSec=%d\n",     wma->lpWaveFormat->nSamplesPerSec);
79     TRACE("waveFormat.nAvgBytesPerSec=%d\n",    wma->lpWaveFormat->nAvgBytesPerSec);
80     TRACE("waveFormat.nBlockAlign=%d\n",        wma->lpWaveFormat->nBlockAlign);
81     TRACE("waveFormat.wBitsPerSample=%d\n",     wma->lpWaveFormat->wBitsPerSample);
82     if (mmckInfo.cksize >= sizeof(WAVEFORMATEX))
83         TRACE("waveFormat.cbSize=%d\n",                 wma->lpWaveFormat->cbSize);
84
85     return TRUE;
86 }
87
88 static BOOL MCIAVI_GetInfoVideo(WINE_MCIAVI* wma, const MMCKINFO* mmckList, MMCKINFO* mmckStream)
89 {
90     MMCKINFO    mmckInfo;
91
92     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(wma->ash_video.fccType)),
93                                                 HIBYTE(LOWORD(wma->ash_video.fccType)),
94                                                 LOBYTE(HIWORD(wma->ash_video.fccType)),
95                                                 HIBYTE(HIWORD(wma->ash_video.fccType)));
96     TRACE("ash.fccHandler='%c%c%c%c'\n",        LOBYTE(LOWORD(wma->ash_video.fccHandler)),
97                                                 HIBYTE(LOWORD(wma->ash_video.fccHandler)),
98                                                 LOBYTE(HIWORD(wma->ash_video.fccHandler)),
99                                                 HIBYTE(HIWORD(wma->ash_video.fccHandler)));
100     TRACE("ash.dwFlags=%d\n",                   wma->ash_video.dwFlags);
101     TRACE("ash.wPriority=%d\n",                 wma->ash_video.wPriority);
102     TRACE("ash.wLanguage=%d\n",                 wma->ash_video.wLanguage);
103     TRACE("ash.dwInitialFrames=%d\n",           wma->ash_video.dwInitialFrames);
104     TRACE("ash.dwScale=%d\n",                   wma->ash_video.dwScale);
105     TRACE("ash.dwRate=%d\n",                    wma->ash_video.dwRate);
106     TRACE("ash.dwStart=%d\n",                   wma->ash_video.dwStart);
107     TRACE("ash.dwLength=%d\n",          wma->ash_video.dwLength);
108     TRACE("ash.dwSuggestedBufferSize=%d\n",     wma->ash_video.dwSuggestedBufferSize);
109     TRACE("ash.dwQuality=%d\n",                 wma->ash_video.dwQuality);
110     TRACE("ash.dwSampleSize=%d\n",              wma->ash_video.dwSampleSize);
111     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        wma->ash_video.rcFrame.top, wma->ash_video.rcFrame.left,
112           wma->ash_video.rcFrame.bottom, wma->ash_video.rcFrame.right);
113
114     /* rewind to the start of the stream */
115     mmioAscend(wma->hFile, mmckStream, 0);
116
117     mmckInfo.ckid = ckidSTREAMFORMAT;
118     if (mmioDescend(wma->hFile, &mmckInfo, mmckList, MMIO_FINDCHUNK) != 0) {
119        WARN("Can't find 'strf' chunk\n");
120         return FALSE;
121     }
122
123     wma->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
124     if (!wma->inbih) {
125         WARN("Can't alloc input BIH\n");
126         return FALSE;
127     }
128
129     mmioRead(wma->hFile, (LPSTR)wma->inbih, mmckInfo.cksize);
130
131     TRACE("bih.biSize=%d\n",            wma->inbih->biSize);
132     TRACE("bih.biWidth=%d\n",           wma->inbih->biWidth);
133     TRACE("bih.biHeight=%d\n",  wma->inbih->biHeight);
134     TRACE("bih.biPlanes=%d\n",          wma->inbih->biPlanes);
135     TRACE("bih.biBitCount=%d\n",        wma->inbih->biBitCount);
136     TRACE("bih.biCompression=%x\n",     wma->inbih->biCompression);
137     TRACE("bih.biSizeImage=%d\n",       wma->inbih->biSizeImage);
138     TRACE("bih.biXPelsPerMeter=%d\n",   wma->inbih->biXPelsPerMeter);
139     TRACE("bih.biYPelsPerMeter=%d\n",   wma->inbih->biYPelsPerMeter);
140     TRACE("bih.biClrUsed=%d\n",         wma->inbih->biClrUsed);
141     TRACE("bih.biClrImportant=%d\n",    wma->inbih->biClrImportant);
142
143     wma->source.left = 0;
144     wma->source.top = 0;
145     wma->source.right = wma->inbih->biWidth;
146     wma->source.bottom = wma->inbih->biHeight;
147
148     wma->dest = wma->source;
149
150     return TRUE;
151 }
152
153 struct AviListBuild {
154     DWORD       numVideoFrames;
155     DWORD       numAudioAllocated;
156     DWORD       numAudioBlocks;
157     DWORD       inVideoSize;
158     DWORD       inAudioSize;
159 };
160
161 static BOOL     MCIAVI_AddFrame(WINE_MCIAVI* wma, LPMMCKINFO mmck,
162                                 struct AviListBuild* alb)
163 {
164     const BYTE *p;
165     DWORD stream_n;
166     DWORD twocc;
167
168     if (mmck->ckid == ckidAVIPADDING) return TRUE;
169
170     p = (const BYTE *)&mmck->ckid;
171
172     if (!isxdigit(p[0]) || !isxdigit(p[1]))
173     {
174         WARN("wrongly encoded stream #\n");
175         return FALSE;
176     }
177
178     stream_n = (p[0] <= '9') ? (p[0] - '0') : (tolower(p[0]) - 'a' + 10);
179     stream_n <<= 4;
180     stream_n |= (p[1] <= '9') ? (p[1] - '0') : (tolower(p[1]) - 'a' + 10);
181
182     TRACE("ckid %4.4s (stream #%d)\n", (LPSTR)&mmck->ckid, stream_n);
183
184     /* Some (rare?) AVI files have video streams name XXYY where XX = stream number and YY = TWOCC
185      * of the last 2 characters of the biCompression member of the BITMAPINFOHEADER structure.
186      * Ex: fccHandler = IV32 & biCompression = IV32 => stream name = XX32
187      *     fccHandler = MSVC & biCompression = CRAM => stream name = XXAM
188      * Another possibility is that these TWOCC are simply ignored.
189      * Default to cktypeDIBcompressed when this case happens.
190      */
191     twocc = TWOCCFromFOURCC(mmck->ckid);
192     if (twocc == TWOCCFromFOURCC(wma->inbih->biCompression))
193         twocc = cktypeDIBcompressed;
194     
195     switch (twocc) {
196     case cktypeDIBbits:
197     case cktypeDIBcompressed:
198     case cktypePALchange:
199         if (stream_n != wma->video_stream_n)
200         {
201             TRACE("data belongs to another video stream #%d\n", stream_n);
202             return FALSE;
203         }
204
205         TRACE("Adding video frame[%d]: %d bytes\n",
206               alb->numVideoFrames, mmck->cksize);
207
208         if (alb->numVideoFrames < wma->dwPlayableVideoFrames) {
209             wma->lpVideoIndex[alb->numVideoFrames].dwOffset = mmck->dwDataOffset;
210             wma->lpVideoIndex[alb->numVideoFrames].dwSize = mmck->cksize;
211             if (alb->inVideoSize < mmck->cksize)
212                 alb->inVideoSize = mmck->cksize;
213             alb->numVideoFrames++;
214         } else {
215             WARN("Too many video frames\n");
216         }
217         break;
218     case cktypeWAVEbytes:
219         if (stream_n != wma->audio_stream_n)
220         {
221             TRACE("data belongs to another audio stream #%d\n", stream_n);
222             return FALSE;
223         }
224
225         TRACE("Adding audio frame[%d]: %d bytes\n",
226               alb->numAudioBlocks, mmck->cksize);
227         if (wma->lpWaveFormat) {
228             if (alb->numAudioBlocks >= alb->numAudioAllocated) {
229                 alb->numAudioAllocated += 32;
230                 if (!wma->lpAudioIndex)
231                     wma->lpAudioIndex = HeapAlloc(GetProcessHeap(), 0,
232                                                   alb->numAudioAllocated * sizeof(struct MMIOPos));
233                 else
234                     wma->lpAudioIndex = HeapReAlloc(GetProcessHeap(), 0, wma->lpAudioIndex,
235                                                     alb->numAudioAllocated * sizeof(struct MMIOPos));
236                 if (!wma->lpAudioIndex) return FALSE;
237             }
238             wma->lpAudioIndex[alb->numAudioBlocks].dwOffset = mmck->dwDataOffset;
239             wma->lpAudioIndex[alb->numAudioBlocks].dwSize = mmck->cksize;
240             if (alb->inAudioSize < mmck->cksize)
241                 alb->inAudioSize = mmck->cksize;
242             alb->numAudioBlocks++;
243         } else {
244             WARN("Wave chunk without wave format... discarding\n");
245         }
246         break;
247     default:
248         WARN("Unknown frame type %4.4s\n", (LPSTR)&mmck->ckid);
249         break;
250     }
251     return TRUE;
252 }
253
254 BOOL MCIAVI_GetInfo(WINE_MCIAVI* wma)
255 {
256     MMCKINFO            ckMainRIFF;
257     MMCKINFO            mmckHead;
258     MMCKINFO            mmckList;
259     MMCKINFO            mmckInfo;
260     AVIStreamHeader strh;
261     struct AviListBuild alb;
262     DWORD stream_n;
263
264     if (mmioDescend(wma->hFile, &ckMainRIFF, NULL, 0) != 0) {
265         WARN("Can't find 'RIFF' chunk\n");
266         return FALSE;
267     }
268
269     if ((ckMainRIFF.ckid != FOURCC_RIFF) || (ckMainRIFF.fccType != formtypeAVI)) {
270         WARN("Can't find 'AVI ' chunk\n");
271         return FALSE;
272     }
273
274     mmckHead.fccType = listtypeAVIHEADER;
275     if (mmioDescend(wma->hFile, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
276         WARN("Can't find 'hdrl' list\n");
277         return FALSE;
278     }
279
280     mmckInfo.ckid = ckidAVIMAINHDR;
281     if (mmioDescend(wma->hFile, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
282         WARN("Can't find 'avih' chunk\n");
283         return FALSE;
284     }
285
286     mmioRead(wma->hFile, (LPSTR)&wma->mah, sizeof(wma->mah));
287
288     TRACE("mah.dwMicroSecPerFrame=%d\n",        wma->mah.dwMicroSecPerFrame);
289     TRACE("mah.dwMaxBytesPerSec=%d\n",  wma->mah.dwMaxBytesPerSec);
290     TRACE("mah.dwPaddingGranularity=%d\n",      wma->mah.dwPaddingGranularity);
291     TRACE("mah.dwFlags=%d\n",                   wma->mah.dwFlags);
292     TRACE("mah.dwTotalFrames=%d\n",             wma->mah.dwTotalFrames);
293     TRACE("mah.dwInitialFrames=%d\n",           wma->mah.dwInitialFrames);
294     TRACE("mah.dwStreams=%d\n",                 wma->mah.dwStreams);
295     TRACE("mah.dwSuggestedBufferSize=%d\n",     wma->mah.dwSuggestedBufferSize);
296     TRACE("mah.dwWidth=%d\n",                   wma->mah.dwWidth);
297     TRACE("mah.dwHeight=%d\n",          wma->mah.dwHeight);
298
299     mmioAscend(wma->hFile, &mmckInfo, 0);
300
301     TRACE("Start of streams\n");
302     wma->video_stream_n = 0;
303     wma->audio_stream_n = 0;
304
305     for (stream_n = 0; stream_n < wma->mah.dwStreams; stream_n++)
306     {
307         MMCKINFO mmckStream;
308
309         mmckList.fccType = listtypeSTREAMHEADER;
310         if (mmioDescend(wma->hFile, &mmckList, &mmckHead, MMIO_FINDLIST) != 0)
311             break;
312
313         mmckStream.ckid = ckidSTREAMHEADER;
314         if (mmioDescend(wma->hFile, &mmckStream, &mmckList, MMIO_FINDCHUNK) != 0)
315         {
316             WARN("Can't find 'strh' chunk\n");
317             continue;
318         }
319
320         mmioRead(wma->hFile, (LPSTR)&strh, sizeof(strh));
321
322         TRACE("Stream #%d fccType %4.4s\n", stream_n, (LPSTR)&strh.fccType);
323
324         if (strh.fccType == streamtypeVIDEO)
325         {
326             TRACE("found video stream\n");
327             if (wma->inbih)
328                 WARN("ignoring another video stream\n");
329             else
330             {
331                 wma->ash_video = strh;
332
333                 if (!MCIAVI_GetInfoVideo(wma, &mmckList, &mmckStream))
334                     return FALSE;
335                 wma->video_stream_n = stream_n;
336             }
337         }
338         else if (strh.fccType == streamtypeAUDIO)
339         {
340             TRACE("found audio stream\n");
341             if (wma->lpWaveFormat)
342                 WARN("ignoring another audio stream\n");
343             else
344             {
345                 wma->ash_audio = strh;
346
347                 if (!MCIAVI_GetInfoAudio(wma, &mmckList, &mmckStream))
348                     return FALSE;
349                 wma->audio_stream_n = stream_n;
350             }
351         }
352         else
353             TRACE("Unsupported stream type %4.4s\n", (LPSTR)&strh.fccType);
354
355         mmioAscend(wma->hFile, &mmckList, 0);
356     }
357
358     TRACE("End of streams\n");
359
360     mmioAscend(wma->hFile, &mmckHead, 0);
361
362     /* no need to read optional JUNK chunk */
363
364     mmckList.fccType = listtypeAVIMOVIE;
365     if (mmioDescend(wma->hFile, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
366         WARN("Can't find 'movi' list\n");
367         return FALSE;
368     }
369
370     wma->dwPlayableVideoFrames = wma->mah.dwTotalFrames;
371     wma->lpVideoIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
372                                   wma->dwPlayableVideoFrames * sizeof(struct MMIOPos));
373     if (!wma->lpVideoIndex) {
374         WARN("Can't alloc video index array\n");
375         return FALSE;
376     }
377     wma->dwPlayableAudioBlocks = 0;
378     wma->lpAudioIndex = NULL;
379
380     alb.numAudioBlocks = alb.numVideoFrames = 0;
381     alb.inVideoSize = alb.inAudioSize = 0;
382     alb.numAudioAllocated = 0;
383
384     while (mmioDescend(wma->hFile, &mmckInfo, &mmckList, 0) == 0) {
385         if (mmckInfo.fccType == listtypeAVIRECORD) {
386             MMCKINFO    tmp;
387
388             while (mmioDescend(wma->hFile, &tmp, &mmckInfo, 0) == 0) {
389                 MCIAVI_AddFrame(wma, &tmp, &alb);
390                 mmioAscend(wma->hFile, &tmp, 0);
391             }
392         } else {
393             MCIAVI_AddFrame(wma, &mmckInfo, &alb);
394         }
395
396         mmioAscend(wma->hFile, &mmckInfo, 0);
397     }
398     if (alb.numVideoFrames != wma->dwPlayableVideoFrames) {
399         WARN("Found %d video frames (/%d), reducing playable frames\n",
400              alb.numVideoFrames, wma->dwPlayableVideoFrames);
401         wma->dwPlayableVideoFrames = alb.numVideoFrames;
402     }
403     wma->dwPlayableAudioBlocks = alb.numAudioBlocks;
404
405     if (alb.inVideoSize > wma->ash_video.dwSuggestedBufferSize) {
406         WARN("inVideoSize=%d suggestedSize=%d\n", alb.inVideoSize, wma->ash_video.dwSuggestedBufferSize);
407         wma->ash_video.dwSuggestedBufferSize = alb.inVideoSize;
408     }
409     if (alb.inAudioSize > wma->ash_audio.dwSuggestedBufferSize) {
410         WARN("inAudioSize=%d suggestedSize=%d\n", alb.inAudioSize, wma->ash_audio.dwSuggestedBufferSize);
411         wma->ash_audio.dwSuggestedBufferSize = alb.inAudioSize;
412     }
413
414     wma->indata = HeapAlloc(GetProcessHeap(), 0, wma->ash_video.dwSuggestedBufferSize);
415     if (!wma->indata) {
416         WARN("Can't alloc input buffer\n");
417         return FALSE;
418     }
419
420     return TRUE;
421 }
422
423 BOOL    MCIAVI_OpenVideo(WINE_MCIAVI* wma)
424 {
425     HDC hDC;
426     DWORD       outSize;
427     FOURCC      fcc = wma->ash_video.fccHandler;
428
429     TRACE("fcc %4.4s\n", (LPSTR)&fcc);
430
431     wma->dwCachedFrame = -1;
432
433     /* get the right handle */
434     if (fcc == mmioFOURCC('C','R','A','M')) fcc = mmioFOURCC('M','S','V','C');
435
436     /* try to get a decompressor for that type */
437     wma->hic = ICLocate(ICTYPE_VIDEO, fcc, wma->inbih, NULL, ICMODE_DECOMPRESS);
438     if (!wma->hic) {
439         /* check for builtin DIB compressions */
440         fcc = wma->inbih->biCompression;
441         if ((fcc == mmioFOURCC('D','I','B',' ')) ||
442             (fcc == mmioFOURCC('R','L','E',' ')) ||
443             (fcc == BI_RGB) || (fcc == BI_RLE8) ||
444             (fcc == BI_RLE4) || (fcc == BI_BITFIELDS))
445             goto paint_frame;
446
447         WARN("Can't locate codec for the file\n");
448         return FALSE;
449     }
450
451     outSize = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
452
453     wma->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
454     if (!wma->outbih) {
455         WARN("Can't alloc output BIH\n");
456         return FALSE;
457     }
458     if (!ICGetDisplayFormat(wma->hic, wma->inbih, wma->outbih, 0, 0, 0)) {
459         WARN("Can't open decompressor\n");
460         return FALSE;
461     }
462
463     TRACE("bih.biSize=%d\n",            wma->outbih->biSize);
464     TRACE("bih.biWidth=%d\n",           wma->outbih->biWidth);
465     TRACE("bih.biHeight=%d\n",  wma->outbih->biHeight);
466     TRACE("bih.biPlanes=%d\n",          wma->outbih->biPlanes);
467     TRACE("bih.biBitCount=%d\n",        wma->outbih->biBitCount);
468     TRACE("bih.biCompression=%x\n",     wma->outbih->biCompression);
469     TRACE("bih.biSizeImage=%d\n",       wma->outbih->biSizeImage);
470     TRACE("bih.biXPelsPerMeter=%d\n",   wma->outbih->biXPelsPerMeter);
471     TRACE("bih.biYPelsPerMeter=%d\n",   wma->outbih->biYPelsPerMeter);
472     TRACE("bih.biClrUsed=%d\n",         wma->outbih->biClrUsed);
473     TRACE("bih.biClrImportant=%d\n",    wma->outbih->biClrImportant);
474
475     wma->outdata = HeapAlloc(GetProcessHeap(), 0, wma->outbih->biSizeImage);
476     if (!wma->outdata) {
477         WARN("Can't alloc output buffer\n");
478         return FALSE;
479     }
480
481     if (ICSendMessage(wma->hic, ICM_DECOMPRESS_BEGIN,
482                       (DWORD_PTR)wma->inbih, (DWORD_PTR)wma->outbih) != ICERR_OK) {
483         WARN("Can't begin decompression\n");
484         return FALSE;
485     }
486
487 paint_frame:
488     hDC = wma->hWndPaint ? GetDC(wma->hWndPaint) : 0;
489     if (hDC)
490     {
491         MCIAVI_PaintFrame(wma, hDC);
492         ReleaseDC(wma->hWndPaint, hDC);
493     }
494     return TRUE;
495 }
496
497 static void CALLBACK MCIAVI_waveCallback(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
498                                         DWORD_PTR dwParam1, DWORD_PTR dwParam2)
499 {
500     WINE_MCIAVI *wma = MCIAVI_mciGetOpenDev(dwInstance);
501
502     if (!wma) return;
503
504     EnterCriticalSection(&wma->cs);
505
506     switch (uMsg) {
507     case WOM_OPEN:
508     case WOM_CLOSE:
509         break;
510     case WOM_DONE:
511         InterlockedIncrement(&wma->dwEventCount);
512         TRACE("Returning waveHdr=%lx\n", dwParam1);
513         SetEvent(wma->hEvent);
514         break;
515     default:
516         ERR("Unknown uMsg=%d\n", uMsg);
517     }
518
519     LeaveCriticalSection(&wma->cs);
520 }
521
522 DWORD MCIAVI_OpenAudio(WINE_MCIAVI* wma, unsigned* nHdr, LPWAVEHDR* pWaveHdr)
523 {
524     DWORD       dwRet;
525     LPWAVEHDR   waveHdr;
526     unsigned    i;
527
528     dwRet = waveOutOpen((HWAVEOUT *)&wma->hWave, WAVE_MAPPER, wma->lpWaveFormat,
529                        (DWORD_PTR)MCIAVI_waveCallback, wma->wDevID, CALLBACK_FUNCTION);
530     if (dwRet != 0) {
531         TRACE("Can't open low level audio device %d\n", dwRet);
532         dwRet = MCIERR_DEVICE_OPEN;
533         wma->hWave = 0;
534         goto cleanUp;
535     }
536
537     /* FIXME: should set up a heuristic to compute the number of wave headers
538      * to be used...
539      */
540     *nHdr = 7;
541     waveHdr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
542                         *nHdr * (sizeof(WAVEHDR) + wma->ash_audio.dwSuggestedBufferSize));
543     if (!waveHdr) {
544         TRACE("Can't alloc wave headers\n");
545         dwRet = MCIERR_DEVICE_OPEN;
546         goto cleanUp;
547     }
548
549     for (i = 0; i < *nHdr; i++) {
550         /* other fields are zero:ed on allocation */
551         waveHdr[i].lpData = (char*)waveHdr +
552             *nHdr * sizeof(WAVEHDR) + i * wma->ash_audio.dwSuggestedBufferSize;
553         waveHdr[i].dwBufferLength = wma->ash_audio.dwSuggestedBufferSize;
554         if (waveOutPrepareHeader(wma->hWave, &waveHdr[i], sizeof(WAVEHDR))) {
555             dwRet = MCIERR_INTERNAL;
556             goto cleanUp;
557         }
558     }
559
560     if (wma->dwCurrVideoFrame != 0 && wma->lpWaveFormat) {
561         FIXME("Should recompute dwCurrAudioBlock, except unsynchronized sound & video\n");
562     }
563     wma->dwCurrAudioBlock = 0;
564
565     wma->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
566     wma->dwEventCount = *nHdr - 1;
567     *pWaveHdr = waveHdr;
568  cleanUp:
569     return dwRet;
570 }
571
572 void MCIAVI_PlayAudioBlocks(WINE_MCIAVI* wma, unsigned nHdr, LPWAVEHDR waveHdr)
573 {
574     if (!wma->lpAudioIndex) 
575         return;
576     TRACE("%d (ec=%u)\n", wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset, wma->dwEventCount);
577
578     /* push as many blocks as possible => audio gets priority */
579     while (wma->dwStatus != MCI_MODE_STOP && wma->dwStatus != MCI_MODE_NOT_READY &&
580            wma->dwCurrAudioBlock < wma->dwPlayableAudioBlocks) {
581         unsigned        whidx = wma->dwCurrAudioBlock % nHdr;
582
583         ResetEvent(wma->hEvent);
584         if (InterlockedDecrement(&wma->dwEventCount) < 0 ||
585             !wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset)
586         {
587             InterlockedIncrement(&wma->dwEventCount);
588             break;
589         }
590
591         mmioSeek(wma->hFile, wma->lpAudioIndex[wma->dwCurrAudioBlock].dwOffset, SEEK_SET);
592         mmioRead(wma->hFile, waveHdr[whidx].lpData, wma->lpAudioIndex[wma->dwCurrAudioBlock].dwSize);
593
594         waveHdr[whidx].dwFlags &= ~WHDR_DONE;
595         waveHdr[whidx].dwBufferLength = wma->lpAudioIndex[wma->dwCurrAudioBlock].dwSize;
596         waveOutWrite(wma->hWave, &waveHdr[whidx], sizeof(WAVEHDR));
597         wma->dwCurrAudioBlock++;
598     }
599 }
600
601 LRESULT MCIAVI_PaintFrame(WINE_MCIAVI* wma, HDC hDC)
602 {
603     void*               pBitmapData;
604     LPBITMAPINFO        pBitmapInfo;
605
606     if (!hDC || !wma->inbih)
607         return TRUE;
608
609     TRACE("Painting frame %u (cached %u)\n", wma->dwCurrVideoFrame, wma->dwCachedFrame);
610
611     if (wma->dwCurrVideoFrame != wma->dwCachedFrame)
612     {
613         if (!wma->lpVideoIndex[wma->dwCurrVideoFrame].dwOffset)
614             return FALSE;
615
616         if (wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize)
617         {
618             mmioSeek(wma->hFile, wma->lpVideoIndex[wma->dwCurrVideoFrame].dwOffset, SEEK_SET);
619             mmioRead(wma->hFile, wma->indata, wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize);
620
621             wma->inbih->biSizeImage = wma->lpVideoIndex[wma->dwCurrVideoFrame].dwSize;
622
623             if (wma->hic && ICDecompress(wma->hic, 0, wma->inbih, wma->indata,
624                                          wma->outbih, wma->outdata) != ICERR_OK)
625             {
626                 WARN("Decompression error\n");
627                 return FALSE;
628             }
629         }
630
631         wma->dwCachedFrame = wma->dwCurrVideoFrame;
632     }
633
634     if (wma->hic) {
635         pBitmapData = wma->outdata;
636         pBitmapInfo = (LPBITMAPINFO)wma->outbih;
637     } else {
638         pBitmapData = wma->indata;
639         pBitmapInfo = (LPBITMAPINFO)wma->inbih;
640     }
641
642     StretchDIBits(hDC,
643                   wma->dest.left, wma->dest.top,
644                   wma->dest.right - wma->dest.left, wma->dest.bottom - wma->dest.top,
645                   wma->source.left, wma->source.top,
646                   wma->source.right - wma->source.left, wma->source.bottom - wma->source.top,
647                   pBitmapData, pBitmapInfo, DIB_RGB_COLORS, SRCCOPY);
648
649     return TRUE;
650 }